2019-07-04 02:19:56 -04:00
|
|
|
# Derived from code on Eric Holscher's blog, found at:
|
|
|
|
# https://www.ericholscher.com/blog/2016/jul/25/integrating-jinja-rst-sphinx/
|
|
|
|
|
2023-07-20 11:51:11 -04:00
|
|
|
import re
|
|
|
|
|
|
|
|
def render_with_jinja(docname, source):
|
2023-08-21 22:34:14 -04:00
|
|
|
if re.search('^\s*.. jinja$', source[0], re.M):
|
2023-07-20 11:51:11 -04:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2019-07-04 02:19:56 -04:00
|
|
|
def rstjinja(app, docname, source):
|
|
|
|
"""
|
|
|
|
Render our pages as a jinja template for fancy templating goodness.
|
|
|
|
"""
|
|
|
|
# Make sure we're outputting HTML
|
2020-08-15 15:29:09 -04:00
|
|
|
if app.builder.format not in ("html", "latex"):
|
2019-07-04 02:19:56 -04:00
|
|
|
return
|
|
|
|
|
2023-07-20 11:51:11 -04:00
|
|
|
# we only want specific files to run through this func
|
|
|
|
if not render_with_jinja(docname, source):
|
2019-07-04 02:19:56 -04:00
|
|
|
return
|
|
|
|
|
2020-08-15 15:29:09 -04:00
|
|
|
src = rendered = source[0]
|
2023-07-20 11:51:11 -04:00
|
|
|
print(f"rendering {docname} as jinja templates")
|
2020-08-15 15:29:09 -04:00
|
|
|
|
|
|
|
if app.builder.format == "html":
|
|
|
|
rendered = app.builder.templates.render_string(
|
|
|
|
src, app.config.html_context
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
from sphinx.util.template import BaseRenderer
|
|
|
|
renderer = BaseRenderer()
|
|
|
|
rendered = renderer.render_string(
|
|
|
|
src,
|
|
|
|
app.config.html_context
|
|
|
|
)
|
|
|
|
|
2019-07-04 02:19:56 -04:00
|
|
|
source[0] = rendered
|
|
|
|
|
|
|
|
def setup(app):
|
|
|
|
app.connect("source-read", rstjinja)
|