From 09b7cddcc2b1440406b591e705e260631269480d Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 8 Jun 2017 09:48:18 +0100 Subject: [PATCH 1/3] Add unified helper for jinja rendering --- md_pdf/build/jinja.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 md_pdf/build/jinja.py diff --git a/md_pdf/build/jinja.py b/md_pdf/build/jinja.py new file mode 100644 index 0000000..ed8f9cc --- /dev/null +++ b/md_pdf/build/jinja.py @@ -0,0 +1,15 @@ +from jinja2 import Environment + + +def render_content(content, context): + env = Environment( + trim_blocks=True, + lstrip_blocks=True, + extensions=[ + 'jinja2.ext.with_', + 'jinja2.ext.loopcontrols' + ] + + ) + template = env.from_string(content) + return template.render(**context) From 126ea6447abb20148f1e62df468d200eb252ba7c Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 8 Jun 2017 09:48:35 +0100 Subject: [PATCH 2/3] Use the unified renderer --- md_pdf/build/content.py | 5 ++--- md_pdf/build/templates.py | 9 ++++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/md_pdf/build/content.py b/md_pdf/build/content.py index 27e03a3..8433485 100644 --- a/md_pdf/build/content.py +++ b/md_pdf/build/content.py @@ -1,8 +1,8 @@ -from jinja2 import Template from bs4 import BeautifulSoup import os import logging from md_pdf.build.context import get_context +from md_pdf.build.jinja import render_content logger = logging.getLogger(__file__) @@ -38,9 +38,8 @@ def add_body_class(doc, config): def render_template(html, config): logger.debug("Rendering Template...") - template = Template(html) context = get_context(config, html) - return template.render(context) + return render_content(html, context) def parse_template(doc, config): diff --git a/md_pdf/build/templates.py b/md_pdf/build/templates.py index 0266672..e0cf1f0 100644 --- a/md_pdf/build/templates.py +++ b/md_pdf/build/templates.py @@ -1,6 +1,6 @@ -from jinja2 import Template from md_pdf.consts import TEMPLATES_DIR, INTERNAL_TEMPLATES_DIR from md_pdf.build.context import get_context +from md_pdf.build.jinja import render_content import os import logging @@ -14,11 +14,10 @@ TEMPLATE_FORMAT = os.path.join(INTERNAL_TEMPLATES_DIR, "{}-template.html") def render_page(input_file, output_file, context): logger.debug("Rendering {}...".format(os.path.splitext(os.path.basename(output_file))[0].title())) with open(input_file) as f: - template = Template(f.read()) + content = render_content(f.read(), context) with open(output_file, "w") as f: - cover = template.render(context) - f.write(cover) - return cover + f.write(content) + return content def render_templates(config, content): From 5e41cabdcf169e070d0fa5b851b639d79b88e565 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 8 Jun 2017 10:25:23 +0100 Subject: [PATCH 3/3] Autoescape --- md_pdf/build/jinja.py | 1 + 1 file changed, 1 insertion(+) diff --git a/md_pdf/build/jinja.py b/md_pdf/build/jinja.py index ed8f9cc..d5f0687 100644 --- a/md_pdf/build/jinja.py +++ b/md_pdf/build/jinja.py @@ -3,6 +3,7 @@ from jinja2 import Environment def render_content(content, context): env = Environment( + autoescape=True, trim_blocks=True, lstrip_blocks=True, extensions=[