From 70351adae6433737b2536105697f7eb8163b14b2 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 25 May 2017 17:16:13 +0100 Subject: [PATCH] Use single render context in all places --- md_pdf/build/__init__.py | 2 +- md_pdf/build/content.py | 4 +++- md_pdf/build/context.py | 14 ++++++++++++++ md_pdf/build/templates.py | 13 ++++--------- 4 files changed, 22 insertions(+), 11 deletions(-) create mode 100644 md_pdf/build/context.py diff --git a/md_pdf/build/__init__.py b/md_pdf/build/__init__.py index 29a47d8..27075f1 100644 --- a/md_pdf/build/__init__.py +++ b/md_pdf/build/__init__.py @@ -20,7 +20,7 @@ def build(config): if 'html' in config['output_formats']: output_html(parsed_template, os.path.abspath(config['output_dir'])) if 'pdf' in config['output_formats']: - render_templates(config) + render_templates(config, parsed_template) render_css() export_pdf(parsed_template, config) logger.info('Output completed in {:.2f} seconds.'.format(time.time() - start_time)) diff --git a/md_pdf/build/content.py b/md_pdf/build/content.py index 2d89308..27e03a3 100644 --- a/md_pdf/build/content.py +++ b/md_pdf/build/content.py @@ -2,6 +2,7 @@ from jinja2 import Template from bs4 import BeautifulSoup import os import logging +from md_pdf.build.context import get_context logger = logging.getLogger(__file__) @@ -38,7 +39,8 @@ def add_body_class(doc, config): def render_template(html, config): logger.debug("Rendering Template...") template = Template(html) - return template.render(config) + context = get_context(config, html) + return template.render(context) def parse_template(doc, config): diff --git a/md_pdf/build/context.py b/md_pdf/build/context.py new file mode 100644 index 0000000..84102d2 --- /dev/null +++ b/md_pdf/build/context.py @@ -0,0 +1,14 @@ +from md_pdf.consts import TEMPLATES_DIR, STATIC_DIR + + +EXTRA_CONTEXT = { + 'templates_dir': TEMPLATES_DIR, + 'static_dir': STATIC_DIR +} + + +def get_context(config, content): + context = config['context'].copy() + context['title'] = config['title'] + context = dict(context, **EXTRA_CONTEXT) + return context diff --git a/md_pdf/build/templates.py b/md_pdf/build/templates.py index 3c8c4ef..e593f5c 100644 --- a/md_pdf/build/templates.py +++ b/md_pdf/build/templates.py @@ -1,14 +1,11 @@ from jinja2 import Template -from md_pdf.consts import TEMPLATES_DIR, STATIC_DIR +from md_pdf.consts import TEMPLATES_DIR +from md_pdf.build.context import get_context import os import logging logger = logging.getLogger(__file__) -EXTRA_CONFIG = { - 'templates_dir': TEMPLATES_DIR, - 'static_dir': STATIC_DIR -} FILE_NAME_FORMAT = os.path.join(TEMPLATES_DIR, "{}.html") TEMPLATE_FORMAT = os.path.join(TEMPLATES_DIR, "{}-template.html") @@ -24,10 +21,8 @@ def render_page(input_file, output_file, context): return cover -def render_templates(config): - context = config['context'].copy() - context['title'] = config['title'] - context = dict(context, **EXTRA_CONFIG) +def render_templates(config, content): + context = get_context(config, content) for template in [ 'cover', 'header',