Allow any files to be rendered as jinja templates

by including a comment ".. jinja" anywhere in the file. By convention,
this should be at the top.

os.getenv will use this so it can render a 'supported boards' list.
This commit is contained in:
Jeff Epler 2023-07-20 10:51:11 -05:00
parent 4a21e05ab6
commit 8ea0835ff6
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE

View File

@ -1,6 +1,15 @@
# Derived from code on Eric Holscher's blog, found at: # Derived from code on Eric Holscher's blog, found at:
# https://www.ericholscher.com/blog/2016/jul/25/integrating-jinja-rst-sphinx/ # https://www.ericholscher.com/blog/2016/jul/25/integrating-jinja-rst-sphinx/
import re
def render_with_jinja(docname, source):
if "shared-bindings/support_matrix" in docname:
return True
if re.search('^\s+.. jinja$', source[0], re.M):
return True
return False
def rstjinja(app, docname, source): def rstjinja(app, docname, source):
""" """
Render our pages as a jinja template for fancy templating goodness. Render our pages as a jinja template for fancy templating goodness.
@ -9,12 +18,12 @@ def rstjinja(app, docname, source):
if app.builder.format not in ("html", "latex"): if app.builder.format not in ("html", "latex"):
return return
# we only want our one jinja template to run through this func # we only want specific files to run through this func
if "shared-bindings/support_matrix" not in docname: if not render_with_jinja(docname, source):
return return
src = rendered = source[0] src = rendered = source[0]
print(docname) print(f"rendering {docname} as jinja templates")
if app.builder.format == "html": if app.builder.format == "html":
rendered = app.builder.templates.render_string( rendered = app.builder.templates.render_string(