Add context for templates
This commit is contained in:
parent
764cde113c
commit
339c4a3df2
4 changed files with 13 additions and 6 deletions
|
@ -7,7 +7,7 @@ import os
|
||||||
|
|
||||||
def build(config):
|
def build(config):
|
||||||
data = read_files(os.path.abspath(config.input))
|
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:
|
if 'html' in config.output_formats:
|
||||||
output_html(doc, os.path.abspath(config.output_dir))
|
output_html(doc, os.path.abspath(config.output_dir))
|
||||||
if 'pdf' in config.output_formats:
|
if 'pdf' in config.output_formats:
|
||||||
|
|
|
@ -2,6 +2,7 @@ import pypandoc
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
import os
|
import os
|
||||||
from md_pdf.consts import PROJECT_DIR, CSL_DIR
|
from md_pdf.consts import PROJECT_DIR, CSL_DIR
|
||||||
|
from jinja2 import Template
|
||||||
|
|
||||||
|
|
||||||
CSL_FILE = os.path.join(PROJECT_DIR, 'assets', 'harverd.csl')
|
CSL_FILE = os.path.join(PROJECT_DIR, 'assets', 'harverd.csl')
|
||||||
|
@ -22,7 +23,12 @@ def output_html(html, out_dir):
|
||||||
f.write(html)
|
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 = [
|
args = [
|
||||||
'-s',
|
'-s',
|
||||||
]
|
]
|
||||||
|
@ -34,12 +40,12 @@ def build_document(files_content, bibliography):
|
||||||
]
|
]
|
||||||
filters.append('pandoc-citeproc')
|
filters.append('pandoc-citeproc')
|
||||||
|
|
||||||
html = pypandoc.convert_text(
|
html = fix_references_title(pypandoc.convert_text(
|
||||||
files_content,
|
files_content,
|
||||||
'html',
|
'html',
|
||||||
format='md',
|
format='md',
|
||||||
extra_args=args,
|
extra_args=args,
|
||||||
filters=filters
|
filters=filters
|
||||||
)
|
))
|
||||||
|
|
||||||
return fix_references_title(html)
|
return parse_template(html, context.toDict())
|
||||||
|
|
|
@ -20,7 +20,6 @@ PDF_OPTIONS = {
|
||||||
"footer-html": FOOTER_FILE,
|
"footer-html": FOOTER_FILE,
|
||||||
"footer-spacing": 5,
|
"footer-spacing": 5,
|
||||||
"header-spacing": 5,
|
"header-spacing": 5,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -8,3 +8,5 @@ bibliography:
|
||||||
csl: apa
|
csl: apa
|
||||||
context:
|
context:
|
||||||
foo: bar
|
foo: bar
|
||||||
|
bar: foo
|
||||||
|
title: test
|
||||||
|
|
Reference in a new issue