A clean book theme for scientific explanations and documentation with Sphinx
Functions that enhance page rendering with metadata, asset management, and template processing. These functions are connected to Sphinx events to modify page context and improve performance.
Adds contextual metadata to pages during rendering, including titles, descriptions, and translation support.
def add_metadata_to_page(app, pagename, templatename, context, doctree):
"""
Add metadata to page context for enhanced rendering.
This function:
- Adds root document title to navbar context
- Sets page title from document structure
- Generates page description from content (160 chars)
- Adds author information if configured
- Sets up translation context
- Configures search bar text
Parameters:
- app: Sphinx application instance
- pagename: Name of the page being processed
- templatename: Template being used for rendering
- context: Template context dictionary (modified in-place)
- doctree: Document tree for the page
"""Manages asset hashing for cache busting, ensuring updated assets are properly loaded.
def hash_html_assets(app, pagename, templatename, context, doctree):
"""
Add digest hashes to CSS and JS assets for cache busting.
Processes theme-specific assets and adds hash-based query parameters
to ensure browsers reload updated files.
Parameters:
- app: Sphinx application instance
- pagename: Name of the page being processed
- templatename: Template being used
- context: Template context (modified to include hashed assets)
- doctree: Document tree for the page
"""
def hash_assets_for_files(assets: list, theme_static: Path, context, app):
"""
Generate hashes for specific asset files and update context.
Parameters:
- assets: List of asset paths relative to theme static folder
- theme_static: Path to theme's static asset directory
- context: Template context containing css_files and script_files
- app: Sphinx application for adding assets with digests
"""Updates template configurations and processes template sections for flexible content layout.
def update_templates(app, pagename, templatename, context, doctree):
"""
Update template names and process template sections.
Processes template sections like 'theme_footer_content_items',
handling comma-separated strings and adding .html extensions
to template names without suffixes.
Parameters:
- app: Sphinx application instance
- pagename: Name of the page being processed
- templatename: Template being used
- context: Template context (modified for template processing)
- doctree: Document tree for the page
"""def _gen_hash(path: str) -> str:
"""
Generate SHA1 digest hash for a file path.
Uses LRU cache for performance optimization.
Parameters:
- path: File path to hash
Returns:
SHA1 hexdigest of file contents
"""from sphinx_book_theme import add_metadata_to_page
# In a Sphinx event handler
def my_page_handler(app, pagename, templatename, context, doctree):
# Add theme metadata
add_metadata_to_page(app, pagename, templatename, context, doctree)
# Access added metadata
print(f"Page title: {context.get('pagetitle')}")
print(f"Page description: {context.get('page_description')}")
print(f"Root title: {context.get('root_title')}")from sphinx_book_theme import hash_html_assets, get_html_theme_path
# Hash theme assets for a page
def process_assets(app, pagename, templatename, context, doctree):
hash_html_assets(app, pagename, templatename, context, doctree)
# Assets now have digest hashes:
# styles/sphinx-book-theme.css?digest=abc123
# scripts/sphinx-book-theme.js?digest=def456from sphinx_book_theme import hash_assets_for_files, get_html_theme_path
# Hash custom assets
custom_assets = ["styles/custom.css", "scripts/custom.js"]
theme_static = get_html_theme_path() / "static"
hash_assets_for_files(custom_assets, theme_static, context, app)Install with Tessl CLI
npx tessl i tessl/pypi-sphinx-book-theme