Add render context
This commit is contained in:
parent
5114f83b5d
commit
764cde113c
4 changed files with 24 additions and 5 deletions
|
@ -11,5 +11,5 @@ def build(config):
|
||||||
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:
|
||||||
render_cover()
|
render_cover(config.context.toDict())
|
||||||
export_pdf(doc, config)
|
export_pdf(doc, config)
|
||||||
|
|
|
@ -21,14 +21,13 @@ PDF_OPTIONS = {
|
||||||
"footer-spacing": 5,
|
"footer-spacing": 5,
|
||||||
"header-spacing": 5,
|
"header-spacing": 5,
|
||||||
|
|
||||||
"replace": [
|
|
||||||
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def export_pdf(content, config):
|
def export_pdf(content, config):
|
||||||
PDF_OPTIONS['title'] = getattr(config, 'title', 'output')
|
PDF_OPTIONS['title'] = getattr(config, 'title', 'output')
|
||||||
|
PDF_OPTIONS['replace'] = list(config.context.items())
|
||||||
|
|
||||||
return pdfkit.from_string(
|
return pdfkit.from_string(
|
||||||
content,
|
content,
|
||||||
os.path.join(os.path.abspath(config.output_dir), 'output.pdf'),
|
os.path.join(os.path.abspath(config.output_dir), 'output.pdf'),
|
||||||
|
|
|
@ -2,6 +2,7 @@ from md_pdf.exceptions import ConfigValidationException
|
||||||
from md_pdf.consts import CSL_DIR
|
from md_pdf.consts import CSL_DIR
|
||||||
import glob
|
import glob
|
||||||
import os
|
import os
|
||||||
|
from dotmap import DotMap
|
||||||
|
|
||||||
|
|
||||||
REQUIRED_KEYS = [
|
REQUIRED_KEYS = [
|
||||||
|
@ -48,11 +49,28 @@ def validate_bibliography(config):
|
||||||
raise ConfigValidationException("Could not find CSL '{}'".format(config.bibliography.csl))
|
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):
|
def validate_config(config):
|
||||||
for validator in [
|
for validator in [
|
||||||
check_required_keys,
|
check_required_keys,
|
||||||
test_input,
|
test_input,
|
||||||
test_output,
|
test_output,
|
||||||
validate_bibliography
|
validate_bibliography,
|
||||||
|
validate_context
|
||||||
]:
|
]:
|
||||||
validator(config)
|
validator(config)
|
||||||
|
|
|
@ -6,3 +6,5 @@ output_dir: out/
|
||||||
bibliography:
|
bibliography:
|
||||||
references: bib.yaml
|
references: bib.yaml
|
||||||
csl: apa
|
csl: apa
|
||||||
|
context:
|
||||||
|
foo: bar
|
||||||
|
|
Reference in a new issue