Sphinx Contrib WebSupport

2.0.0 · active · verified Sat Apr 11

sphinxcontrib-websupport provides a Python API to easily integrate Sphinx documentation into your Web application. It facilitates building Sphinx documentation in a web-friendly format and retrieving content. The current version is 2.0.0, and releases are infrequent but can include significant breaking changes between major versions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `WebSupport`, build Sphinx documentation, and retrieve generated content. It creates a dummy Sphinx project, builds it using `WebSupport.build_all()`, and then fetches the content of the 'index' page.

import os
from pathlib import Path
from sphinxcontrib.websupport import WebSupport

# Create dummy Sphinx project directories for the example
src_dir = Path('./docs-src')
build_dir = Path('./docs-build')

src_dir.mkdir(exist_ok=True)
build_dir.mkdir(exist_ok=True)

# Create a minimal conf.py and index.rst
(src_dir / 'conf.py').write_text(
    """project = 'My Web Docs'\ncopyright = '2023, Me'\nhtml_theme = 'alabaster'\n"""
)
(src_dir / 'index.rst').write_text(
    """Welcome to My Web Docs\n=======================

.. toctree::
   :maxdepth: 2

   intro
"""
)
(src_dir / 'intro.rst').write_text("""Introduction\n============\nThis is an introduction."""
)

try:
    # Initialize WebSupport with source and build directories
    # The build_dir must exist and be writable.
    support = WebSupport(srcdir=str(src_dir), builddir=str(build_dir))

    # Build all documents
    print("Building documents...")
    support.build_all()
    print(f"Documents built to: {build_dir}")

    # Retrieve a specific document (e.g., 'index')
    print("Retrieving 'index' document...")
    document = support.get_document('index')

    print("\n--- Document HTML Snippet ---")
    print(document.get('body', '')[:200] + '...') # Print first 200 chars of body
    print("\n--- Document Title ---")
    print(document.get('title'))

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Clean up dummy directories
    import shutil
    if src_dir.exists():
        shutil.rmtree(src_dir)
    if build_dir.exists():
        shutil.rmtree(build_dir)

view raw JSON →