Merge remote-tracking branch 'origin' into tests

This commit is contained in:
Jake Howard 2017-06-09 22:40:02 +01:00
commit f0d7900fc2
Signed by: jake
GPG key ID: 57AFB45680EDD477
3 changed files with 22 additions and 8 deletions

View file

@ -1,8 +1,8 @@
from jinja2 import Template
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import os import os
import logging import logging
from md_pdf.build.context import get_context from md_pdf.build.context import get_context
from md_pdf.build.jinja import render_content
logger = logging.getLogger(__file__) logger = logging.getLogger(__file__)
@ -40,9 +40,8 @@ def add_body_class(doc, config):
def render_template(html, config): def render_template(html, config):
logger.debug("Rendering Template...") logger.debug("Rendering Template...")
template = Template(html)
context = get_context(config, html) context = get_context(config, html)
return template.render(context) return render_content(html, context)
def parse_template(doc, config): def parse_template(doc, config):

16
md_pdf/build/jinja.py Normal file
View file

@ -0,0 +1,16 @@
from jinja2 import Environment
def render_content(content, context):
env = Environment(
autoescape=True,
trim_blocks=True,
lstrip_blocks=True,
extensions=[
'jinja2.ext.with_',
'jinja2.ext.loopcontrols'
]
)
template = env.from_string(content)
return template.render(**context)

View file

@ -1,6 +1,6 @@
from jinja2 import Template
from md_pdf.consts import TEMPLATES_DIR, INTERNAL_TEMPLATES_DIR from md_pdf.consts import TEMPLATES_DIR, INTERNAL_TEMPLATES_DIR
from md_pdf.build.context import get_context from md_pdf.build.context import get_context
from md_pdf.build.jinja import render_content
import os import os
import logging import logging
@ -14,11 +14,10 @@ TEMPLATE_FORMAT = os.path.join(INTERNAL_TEMPLATES_DIR, "{}-template.html")
def render_page(input_file, output_file, context): def render_page(input_file, output_file, context):
logger.debug("Rendering {}...".format(os.path.splitext(os.path.basename(output_file))[0].title())) logger.debug("Rendering {}...".format(os.path.splitext(os.path.basename(output_file))[0].title()))
with open(input_file) as f: with open(input_file) as f:
template = Template(f.read()) content = render_content(f.read(), context)
with open(output_file, "w") as f: with open(output_file, "w") as f:
cover = template.render(context) f.write(content)
f.write(cover) return content
return cover
def render_templates(config, content): def render_templates(config, content):