22# Distributed under the terms of the MIT License.
33# SPDX-License-Identifier: MIT
44"""Render outputs with Jinja templates."""
5+ from pathlib import Path
6+
57import jinja2
68import mdit_py_plugins .anchors
79import mdit_py_plugins .footnote
810from markdown_it import MarkdownIt
911
1012
11- def make_jinja_env (templates_dir ):
13+ def make_jinja_envs (templates_dir ):
1214 """
13- Create the default Jinja environment given the template folder.
15+ Create the default Jinja environments given the template folder.
1416
1517 Parameters
1618 ----------
@@ -19,15 +21,40 @@ def make_jinja_env(templates_dir):
1921
2022 Returns
2123 -------
22- env : jinja2.Environment
23- The environment used to render the templates.
24+ envs : dict
25+ The keys are the file types supported as templates and values are the
26+ corresponding environments used to render the templates for those file
27+ types.
2428 """
25- # Need to add the current dir so we can use the Markdown files as templates
26- env = jinja2 .Environment (
27- loader = jinja2 .FileSystemLoader ([str (templates_dir ), "." ], followlinks = True ),
28- extensions = ["jinja2.ext.do" , "jinja2.ext.loopcontrols" ],
29- )
30- return env
29+ envs = {
30+ "markdown" : jinja2 .Environment (
31+ loader = jinja2 .FileSystemLoader ([str (templates_dir )], followlinks = True ),
32+ extensions = ["jinja2.ext.do" , "jinja2.ext.loopcontrols" ],
33+ ),
34+ "html" : jinja2 .Environment (
35+ loader = jinja2 .FileSystemLoader ([str (templates_dir )], followlinks = True ),
36+ extensions = ["jinja2.ext.do" , "jinja2.ext.loopcontrols" ],
37+ ),
38+ "latex" : jinja2 .Environment (
39+ loader = jinja2 .FileSystemLoader ([str (templates_dir )], followlinks = True ),
40+ extensions = ["jinja2.ext.do" , "jinja2.ext.loopcontrols" ],
41+ # Define custom syntax that is compatible with LaTeX
42+ # Based on jtex by curvenote (MIT license):
43+ # https://github.com/curvenote/jtex/blob/2778c9fc51cd2cbbe8d4b7deedd637e9dd59f662/jtex/TemplateRenderer.py#L36
44+ block_start_string = r"[#" ,
45+ block_end_string = "#]" ,
46+ variable_start_string = r"[-" ,
47+ variable_end_string = "-]" ,
48+ line_comment_prefix = r"%%" ,
49+ comment_start_string = r"%#" ,
50+ comment_end_string = "#%" ,
51+ trim_blocks = True ,
52+ autoescape = False ,
53+ auto_reload = True ,
54+ keep_trailing_newline = True ,
55+ ),
56+ }
57+ return envs
3158
3259
3360def markdown_to_html (page ):
@@ -68,7 +95,7 @@ def _render_footnote_block_open(self, tokens, idx, options, env):
6895 return "\n " .join (lines )
6996
7097
71- def render_markdown (page , config , site , build , jinja_env ):
98+ def render_markdown (page , config , site , build , jinja_envs ):
7299 """
73100 Render the templates in Markdown content of the page.
74101
@@ -83,20 +110,20 @@ def render_markdown(page, config, site, build, jinja_env):
83110 Dictionary with the entire site content so far.
84111 build : dict
85112 Dictionary with information about the build environment.
86- jinja_env
87- A Jinja2 environment for loading templates.
113+ jinja_envs
114+ A dictionary of Jinja2 environments for loading templates.
88115
89116 Returns
90117 -------
91118 markdown : str
92119 The rendered Markdown content for the page.
93120 """
94- template = jinja_env .from_string (page ["markdown" ])
121+ template = jinja_envs [ "markdown" ] .from_string (page ["markdown" ])
95122 markdown = template .render (page = page , config = config , site = site , build = build )
96123 return markdown
97124
98125
99- def render_output (page , config , site , build , jinja_env ):
126+ def render_output (page , config , site , build , jinja_envs ):
100127 """
101128 Render the full template output for a page.
102129
@@ -112,15 +139,17 @@ def render_output(page, config, site, build, jinja_env):
112139 Dictionary with the entire site content so far.
113140 build : dict
114141 Dictionary with information about the build environment.
115- jinja_env
116- A Jinja2 environment for loading templates.
142+ jinja_envs
143+ A dictionary of Jinja2 environments for loading templates.
117144
118145 Returns
119146 -------
120147 html : str
121148 The converted HTML.
122149
123150 """
124- template = jinja_env .get_template (page ["template" ])
151+ types = {".html" : "html" , ".tex" : "latex" }
152+ template_type = types [Path (page ["template" ]).suffix ]
153+ template = jinja_envs [template_type ].get_template (page ["template" ])
125154 html = template .render (page = page , config = config , site = site , build = build )
126155 return html
0 commit comments