render_markdown_path: Refactor pure_markdown rendering logic.

The changes in this commit were suggested by Tim Abbott in #10252.
This commit is contained in:
Eeshan Garg 2018-08-09 18:15:54 -02:30 committed by Tim Abbott
parent 9e2a369e42
commit 01bd55bbcb

View File

@ -118,26 +118,25 @@ def render_markdown_path(markdown_file_path: str,
jinja = engines['Jinja2']
if pure_markdown:
# For files such as /etc/zulip/terms.md where we don't intend
# to use Jinja2 template variables, we still try to load the
# template using Jinja2 (in case the file path isn't absolute
# and does happen to be in Jinja's recognized template
# directories), and if that fails, we try to load it directly
# from disk.
try:
markdown_string = jinja.env.loader.get_source(jinja.env, markdown_file_path)[0]
except TemplateNotFound:
with open(markdown_file_path) as fp:
markdown_string = fp.read()
rendered_html = md_engine.convert(markdown_string)
else:
try:
# By default, we do both Jinja2 templating and markdown
# processing on the file, to make it easy to use both Jinja2
# context variables and markdown includes in the file.
markdown_string = jinja.env.loader.get_source(jinja.env, markdown_file_path)[0]
html = md_engine.convert(markdown_string)
rendered_html = jinja.from_string(html).render(context)
except TemplateNotFound as e:
if pure_markdown:
# For files such as /etc/zulip/terms.md where we don't intend
# to use Jinja2 template variables, we still try to load the
# template using Jinja2 (in case the file path isn't absolute
# and does happen to be in Jinja's recognized template
# directories), and if that fails, we try to load it directly
# from disk.
with open(markdown_file_path) as fp:
markdown_string = fp.read()
else:
raise e
html = md_engine.convert(markdown_string)
rendered_html = jinja.from_string(html).render(context)
return mark_safe(rendered_html)