An awesome theme for the Sphinx documentation generator with enhanced features, custom code highlighting, and modern responsive design
Custom Jinja2 functions and template modifications that enhance template rendering with canonical URL fixes, directory builder support, and improved template context management for better SEO and navigation.
Main function that registers custom template functions and fixes builder-specific issues.
def setup_jinja(app: Sphinx, pagename: str, templatename: str,
context: dict[str, Any], doctree: Node) -> None:
"""
Register a function as a Jinja2 filter and fix canonical URLs.
Handles the html-page-context event to:
- Fix canonical URL generation for dirhtml builder
- Register custom template functions
- Enhance template context with theme-specific data
Parameters:
- app (Sphinx): The Sphinx application instance
- pagename (str): Current page name being rendered
- templatename (str): Name of the template being used
- context (dict[str, Any]): Template context dictionary
- doctree (Node): Document tree node for the page
"""Internal function that creates proper canonical URLs for directory-based builders.
def _make_canonical(app: Sphinx, pagename: str) -> str:
"""
Turn a filepath into the correct canonical link.
Fixes canonical URL generation for the dirhtml builder, which
upstream Sphinx builds incorrectly. Ensures proper SEO and
URL structure for directory-based site organization.
Parameters:
- app (Sphinx): The Sphinx application instance
- pagename (str): Page name to convert to canonical URL
Returns:
str: Properly formatted canonical URL with trailing slash
"""The Sphinx dirhtml builder generates URLs like /page/ instead of /page.html, but the canonical URL generation doesn't account for this properly. The template functions fix this issue.
# Sphinx generates incorrect canonical URLs for dirhtml builder
canonical = "https://example.com/docs/installation.html" # Wrong for dirhtml# Template function generates correct canonical URLs
canonical = "https://example.com/docs/installation/" # Correct for dirhtmlThe template functions enhance the Jinja2 context with corrected values:
# For dirhtml builder with html_baseurl configured
if app.builder.name == "dirhtml" and app.config.html_baseurl:
context["pageurl"] = _make_canonical(app, pagename)This ensures templates have access to properly formatted page URLs for:
<!-- In base template -->
{% if pageurl %}
<link rel="canonical" href="{{ pageurl }}">
{% endif %}<!-- Open Graph tags -->
<meta property="og:url" content="{{ pageurl }}">
<meta property="og:type" content="website">
<!-- Twitter Card tags -->
<meta name="twitter:url" content="{{ pageurl }}"><!-- JSON-LD structured data -->
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "WebPage",
"url": "{{ pageurl }}",
"name": "{{ title }}"
}
</script>Standard HTML builder generates files like page.html:
Directory HTML builder generates directory structure:
page/index.html/page/# In conf.py
html_baseurl = "https://example.com/docs/"
# For dirhtml builder
extensions = [
# ... other extensions
]
# The template functions automatically detect dirhtml builder
# and fix canonical URLs when html_baseurl is configuredCorrect canonical URLs help search engines:
Consistent URL patterns improve:
The template functions integrate with Sphinx's event system:
# In theme setup
app.connect("html-page-context", setup_jinja)This ensures the functions run for every page during the build process, providing consistent URL handling across the entire site.
The template functions include robust error handling:
# Safe canonical URL generation
if app.builder.name == "dirhtml" and app.config.html_baseurl:
# Only modify context if conditions are met
context["pageurl"] = _make_canonical(app, pagename)This prevents issues when:
html_baseurl is not configuredInstall with Tessl CLI
npx tessl i tessl/pypi-sphinxawesome-theme