app_filters: Render HTML to render Jinja2 syntax within Markdown macros.

If a Markdown macro contains Jinja2 template code, it isn't rendered
because render_markdown_path calls template.render on the including
.md file before the macro has been included. And then the including
.md file is converted to HTML. Therefore, the template code within
a Markdown macro (if any) never gets rendered and is returned as it is.

Now, after the source .md file is converted to HTML,
render_markdown_path renders the resulting HTML so that any template
code within included macros (if any) is finally rendered.
This commit is contained in:
Eeshan Garg 2017-05-17 22:59:31 -02:30 committed by Tim Abbott
parent df7eaa8868
commit baff121115
3 changed files with 12 additions and 4 deletions

View File

@ -1,7 +1,7 @@
from typing import Dict, Optional, Any
from django.conf import settings
from django.template import Library, loader
from django.template import Library, loader, engines
from django.utils.safestring import mark_safe
from django.utils.lru_cache import lru_cache
@ -82,7 +82,8 @@ def render_markdown_path(markdown_file_path, context=None):
if context is None:
context = {}
template = loader.get_template(markdown_file_path)
markdown_string = template.render(context)
jinja = engines['Jinja2']
markdown_string = jinja.env.loader.get_source(jinja.env, markdown_file_path)[0]
html = md_engine.convert(markdown_string)
return mark_safe(html)
html_template = jinja.from_string(html)
return mark_safe(html_template.render(context))

View File

@ -46,6 +46,12 @@ class Jinja2(django_jinja2.Jinja2):
six.reraise(TemplateSyntaxError, TemplateSyntaxError(exc.args),
sys.exc_info()[2])
def from_string(self, template_code):
# type: (str) -> Template
return Template(self.env.from_string(template_code),
self.context_processors,
self.debug)
class Template(django_jinja2.Template):
"""Context processors aware Template.

View File

@ -298,6 +298,7 @@ if PRODUCTION:
TEMPLATES = [
{
'NAME': 'Jinja2',
'BACKEND': 'zproject.jinja2.backends.Jinja2',
'DIRS': [
os.path.join(DEPLOY_ROOT, 'templates'),