This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
md-pdf/md_pdf/build/templates.py

34 lines
1 KiB
Python
Raw Normal View History

2017-03-27 09:17:49 +01:00
from jinja2 import Template
from md_pdf.consts import TEMPLATES_DIR
from md_pdf.build.context import get_context
2017-03-28 21:31:42 +01:00
import os
2017-04-01 16:12:03 +01:00
import logging
logger = logging.getLogger(__file__)
2017-03-27 09:17:49 +01:00
2017-03-28 21:31:42 +01:00
2017-05-17 17:52:57 +01:00
FILE_NAME_FORMAT = os.path.join(TEMPLATES_DIR, "{}.html")
TEMPLATE_FORMAT = os.path.join(TEMPLATES_DIR, "{}-template.html")
2017-03-28 21:31:42 +01:00
2017-05-17 17:52:57 +01:00
def render_page(input_file, output_file, context):
2017-05-17 21:25:06 +01:00
logger.debug("Rendering {}...".format(os.path.splitext(os.path.basename(output_file))[0].title()))
2017-05-17 17:52:57 +01:00
with open(input_file) as f:
2017-03-27 09:17:49 +01:00
template = Template(f.read())
2017-05-17 17:52:57 +01:00
with open(output_file, "w") as f:
2017-03-27 09:17:49 +01:00
cover = template.render(context)
f.write(cover)
return cover
2017-05-17 17:52:57 +01:00
def render_templates(config, content):
context = get_context(config, content)
2017-05-17 17:52:57 +01:00
for template in [
'cover',
'header',
'footer'
]:
render_page(TEMPLATE_FORMAT.format(template), FILE_NAME_FORMAT.format(template), context)
2017-05-17 21:25:06 +01:00
if config.get('toc', False):
render_page(os.path.join(TEMPLATES_DIR, 'toc-template.xsl'), os.path.join(TEMPLATES_DIR, 'toc.xsl'), context)