diff --git a/templates/zerver/privacy.html b/templates/zerver/privacy.html index 253d29cc7a..d31a4b43ed 100644 --- a/templates/zerver/privacy.html +++ b/templates/zerver/privacy.html @@ -21,7 +21,7 @@
{% if privacy_policy %} - {{ render_markdown_path(privacy_policy) }} + {{ render_markdown_path(privacy_policy, pure_markdown=True) }} {% else %} {% trans %} This installation of Zulip does not have a configured privacy policy. diff --git a/templates/zerver/terms.html b/templates/zerver/terms.html index 0027e5858b..fb7e15331f 100644 --- a/templates/zerver/terms.html +++ b/templates/zerver/terms.html @@ -20,7 +20,7 @@
{% if terms_of_service %} - {{ render_markdown_path(terms_of_service) }} + {{ render_markdown_path(terms_of_service, pure_markdown=True) }} {% else %} {% trans %} This installation of Zulip does not have a configured terms of service. diff --git a/zerver/templatetags/app_filters.py b/zerver/templatetags/app_filters.py index b7f24605e9..5330f4914f 100644 --- a/zerver/templatetags/app_filters.py +++ b/zerver/templatetags/app_filters.py @@ -10,6 +10,7 @@ import markdown_include.include from django.conf import settings from django.template import Library, engines, loader from django.utils.safestring import mark_safe +from jinja2.exceptions import TemplateNotFound import zerver.lib.bugdown.fenced_code import zerver.lib.bugdown.api_arguments_table_generator @@ -71,7 +72,9 @@ docs_without_macros = [ # the results when called if none of the arguments are unhashable. @ignore_unhashable_lru_cache(512) @register.filter(name='render_markdown_path', is_safe=True) -def render_markdown_path(markdown_file_path: str, context: Optional[Dict[Any, Any]]=None) -> str: +def render_markdown_path(markdown_file_path: str, + context: Optional[Dict[Any, Any]]=None, + pure_markdown: Optional[bool]=False) -> str: """Given a path to a markdown file, return the rendered html. Note that this assumes that any HTML in the markdown file is @@ -114,7 +117,27 @@ def render_markdown_path(markdown_file_path: str, context: Optional[Dict[Any, An md_engine.reset() jinja = engines['Jinja2'] - markdown_string = jinja.env.loader.get_source(jinja.env, markdown_file_path)[0] - html = md_engine.convert(markdown_string) - html_template = jinja.from_string(html) - return mark_safe(html_template.render(context)) + + 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: + # 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) + + return mark_safe(rendered_html)