diff --git a/md_pdf/build/__init__.py b/md_pdf/build/__init__.py index 2c77bc5..141f134 100644 --- a/md_pdf/build/__init__.py +++ b/md_pdf/build/__init__.py @@ -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) diff --git a/md_pdf/build/pdf.py b/md_pdf/build/pdf.py index 503923e..27df804 100644 --- a/md_pdf/build/pdf.py +++ b/md_pdf/build/pdf.py @@ -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'), diff --git a/md_pdf/config/validate.py b/md_pdf/config/validate.py index 0b97d6b..535c7bc 100644 --- a/md_pdf/config/validate.py +++ b/md_pdf/config/validate.py @@ -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) diff --git a/test-files/mdp.yml b/test-files/mdp.yml index 63e1ac9..6e8f7f4 100644 --- a/test-files/mdp.yml +++ b/test-files/mdp.yml @@ -6,3 +6,5 @@ output_dir: out/ bibliography: references: bib.yaml csl: apa +context: + foo: bar