archive
/
md-pdf
Archived
1
Fork 0

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']:
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))

View File

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

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 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',