Add context for templates

This commit is contained in:
Jake Howard 2017-03-30 18:01:48 +01:00
parent 764cde113c
commit 339c4a3df2
4 changed files with 13 additions and 6 deletions

View file

@ -7,7 +7,7 @@ import os
def build(config):
data = read_files(os.path.abspath(config.input))
doc = build_document(data, getattr(config, 'bibliography', None))
doc = build_document(data, getattr(config, 'bibliography', None), getattr(config, 'context', None))
if 'html' in config.output_formats:
output_html(doc, os.path.abspath(config.output_dir))
if 'pdf' in config.output_formats:

View file

@ -2,6 +2,7 @@ import pypandoc
from bs4 import BeautifulSoup
import os
from md_pdf.consts import PROJECT_DIR, CSL_DIR
from jinja2 import Template
CSL_FILE = os.path.join(PROJECT_DIR, 'assets', 'harverd.csl')
@ -22,7 +23,12 @@ def output_html(html, out_dir):
f.write(html)
def build_document(files_content, bibliography):
def parse_template(html, context):
template = Template(html)
return template.render(context)
def build_document(files_content, bibliography, context):
args = [
'-s',
]
@ -34,12 +40,12 @@ def build_document(files_content, bibliography):
]
filters.append('pandoc-citeproc')
html = pypandoc.convert_text(
html = fix_references_title(pypandoc.convert_text(
files_content,
'html',
format='md',
extra_args=args,
filters=filters
)
))
return fix_references_title(html)
return parse_template(html, context.toDict())

View file

@ -20,7 +20,6 @@ PDF_OPTIONS = {
"footer-html": FOOTER_FILE,
"footer-spacing": 5,
"header-spacing": 5,
}

View file

@ -8,3 +8,5 @@ bibliography:
csl: apa
context:
foo: bar
bar: foo
title: test