Skip to content

Commit dab06e0

Browse files
authored
Add a custom environment for LaTeX templates (#38)
The default syntax doesn't work with latex because of all the curly braces in the language. Use an environment with custom syntax (`[# #]` for blocks and `[- -]` for variables) to get around that if the template is a latex file. Based on jtex by Curvenote (MIT license).
1 parent b7692af commit dab06e0

File tree

2 files changed

+51
-22
lines changed

2 files changed

+51
-22
lines changed

nene/_api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from .crawling import crawl
1313
from .parsing import load_config, load_data, load_jupyter_notebook, load_markdown
1414
from .printing import make_console, print_dict, print_file_stats
15-
from .rendering import make_jinja_env, markdown_to_html, render_markdown, render_output
15+
from .rendering import make_jinja_envs, markdown_to_html, render_markdown, render_output
1616
from .utils import capture_build_info
1717

1818

@@ -173,12 +173,12 @@ def render(site, config, build, console=None, style=""):
173173
if console is None:
174174
console, style = make_console(verbose=False)
175175

176-
jinja_env = make_jinja_env(config["templates_dir"])
176+
jinja_envs = make_jinja_envs(config["templates_dir"])
177177

178178
console.print(":art: Rendering templates in Markdown content:", style=style)
179179
for page in site.values():
180180
console.print(f" {page['source']}")
181-
page["markdown"] = render_markdown(page, config, site, build, jinja_env)
181+
page["markdown"] = render_markdown(page, config, site, build, jinja_envs)
182182

183183
console.print(":art: Converting Markdown content to HTML:", style=style)
184184
for page in site.values():
@@ -189,7 +189,7 @@ def render(site, config, build, console=None, style=""):
189189
console.print(":art: Rendering templates for final outputs:", style=style)
190190
for page in site.values():
191191
console.print(f" {page['path']}{page['template']}")
192-
page["output"] = render_output(page, config, site, build, jinja_env)
192+
page["output"] = render_output(page, config, site, build, jinja_envs)
193193

194194

195195
def export(site, files_to_copy, output_dir, console=None, style=""):

nene/rendering.py

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
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+
57
import jinja2
68
import mdit_py_plugins.anchors
79
import mdit_py_plugins.footnote
810
from 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

3360
def 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

Comments
 (0)