Add render context

This commit is contained in:
Jake Howard 2017-03-30 17:08:00 +01:00
parent 5114f83b5d
commit 764cde113c
4 changed files with 24 additions and 5 deletions

View file

@ -11,5 +11,5 @@ def build(config):
if 'html' in config.output_formats:
output_html(doc, os.path.abspath(config.output_dir))
if 'pdf' in config.output_formats:
render_cover()
render_cover(config.context.toDict())
export_pdf(doc, config)

View file

@ -21,14 +21,13 @@ PDF_OPTIONS = {
"footer-spacing": 5,
"header-spacing": 5,
"replace": [
]
}
def export_pdf(content, config):
PDF_OPTIONS['title'] = getattr(config, 'title', 'output')
PDF_OPTIONS['replace'] = list(config.context.items())
return pdfkit.from_string(
content,
os.path.join(os.path.abspath(config.output_dir), 'output.pdf'),

View file

@ -2,6 +2,7 @@ from md_pdf.exceptions import ConfigValidationException
from md_pdf.consts import CSL_DIR
import glob
import os
from dotmap import DotMap
REQUIRED_KEYS = [
@ -48,11 +49,28 @@ def validate_bibliography(config):
raise ConfigValidationException("Could not find CSL '{}'".format(config.bibliography.csl))
def validate_context(config):
if 'context' not in config:
return
if type(config.context) != DotMap:
raise ConfigValidationException("Context must be key:value store")
non_str_keys = [key for key in config.context.keys() if type(key) != str]
if non_str_keys:
raise ConfigValidationException("Context keys must be strings. Non-strings: {}".format(", ".join(non_str_keys)))
invalid_values = [value for value in config.context.values() if type(value) in [list, dict, DotMap]]
if invalid_values:
raise ConfigValidationException("Context keys must be plain. Invalid values: {}".format(", ".join(invalid_values)))
def validate_config(config):
for validator in [
check_required_keys,
test_input,
test_output,
validate_bibliography
validate_bibliography,
validate_context
]:
validator(config)

View file

@ -6,3 +6,5 @@ output_dir: out/
bibliography:
references: bib.yaml
csl: apa
context:
foo: bar