Use single render context in all places

This commit is contained in:
Jake Howard 2017-05-25 17:16:13 +01:00
parent a37b08ab4d
commit 70351adae6
4 changed files with 22 additions and 11 deletions

View file

@ -20,7 +20,7 @@ def build(config):
if 'html' in config['output_formats']: if 'html' in config['output_formats']:
output_html(parsed_template, os.path.abspath(config['output_dir'])) output_html(parsed_template, os.path.abspath(config['output_dir']))
if 'pdf' in config['output_formats']: if 'pdf' in config['output_formats']:
render_templates(config) render_templates(config, parsed_template)
render_css() render_css()
export_pdf(parsed_template, config) export_pdf(parsed_template, config)
logger.info('Output completed in {:.2f} seconds.'.format(time.time() - start_time)) logger.info('Output completed in {:.2f} seconds.'.format(time.time() - start_time))

View file

@ -2,6 +2,7 @@ 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
logger = logging.getLogger(__file__) logger = logging.getLogger(__file__)
@ -38,7 +39,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) template = Template(html)
return template.render(config) context = get_context(config, html)
return template.render(context)
def parse_template(doc, config): def parse_template(doc, config):

14
md_pdf/build/context.py Normal file
View file

@ -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

View file

@ -1,14 +1,11 @@
from jinja2 import Template 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 os
import logging import logging
logger = logging.getLogger(__file__) logger = logging.getLogger(__file__)
EXTRA_CONFIG = {
'templates_dir': TEMPLATES_DIR,
'static_dir': STATIC_DIR
}
FILE_NAME_FORMAT = os.path.join(TEMPLATES_DIR, "{}.html") FILE_NAME_FORMAT = os.path.join(TEMPLATES_DIR, "{}.html")
TEMPLATE_FORMAT = os.path.join(TEMPLATES_DIR, "{}-template.html") TEMPLATE_FORMAT = os.path.join(TEMPLATES_DIR, "{}-template.html")
@ -24,10 +21,8 @@ def render_page(input_file, output_file, context):
return cover return cover
def render_templates(config): def render_templates(config, content):
context = config['context'].copy() context = get_context(config, content)
context['title'] = config['title']
context = dict(context, **EXTRA_CONFIG)
for template in [ for template in [
'cover', 'cover',
'header', 'header',