Sphinx Contrib WebSupport
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
- breaking The `websupport` module was moved from core Sphinx to the separate `sphinxcontrib-websupport` package. Old code importing `from sphinx.websupport import WebSupport` will fail.
- breaking Version 2.0.0 completely removed `SQLAlchemy` support and the built-in storage backend. Users who relied on the default storage mechanism will need to implement their own storage solution.
- breaking Several deprecated APIs were removed in version 2.0.0, including the `storage` and `json_serializer` arguments to the `WebSupport` constructor, and methods like `get_comments()` and `add_comment()`.
- gotcha The `builddir` argument provided to `WebSupport` must exist and be writable before calling `build_all()`. `WebSupport` does not automatically create this directory.
Install
-
pip install sphinxcontrib-websupport
Imports
- WebSupport
from sphinxcontrib.websupport import WebSupport
Quickstart
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)