2017-05-28 11:13:36 +01:00
|
|
|
from md_pdf.consts import TEMPLATES_DIR, INTERNAL_TEMPLATES_DIR
|
2017-05-25 17:16:13 +01:00
|
|
|
from md_pdf.build.context import get_context
|
2017-06-08 09:48:35 +01:00
|
|
|
from md_pdf.build.jinja import render_content
|
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")
|
2017-05-28 11:13:36 +01:00
|
|
|
TEMPLATE_FORMAT = os.path.join(INTERNAL_TEMPLATES_DIR, "{}-template.html")
|
2017-03-28 21:31:42 +01:00
|
|
|
|
|
|
|
|
2017-06-09 23:07:30 +01:00
|
|
|
def render_page(input_file: str, output_file: str, context: dict) -> str:
|
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-06-08 09:48:35 +01:00
|
|
|
content = render_content(f.read(), context)
|
2017-05-17 17:52:57 +01:00
|
|
|
with open(output_file, "w") as f:
|
2017-06-08 09:48:35 +01:00
|
|
|
f.write(content)
|
|
|
|
return content
|
2017-05-17 17:52:57 +01:00
|
|
|
|
|
|
|
|
2017-06-09 23:07:30 +01:00
|
|
|
def render_templates(config: dict, content: str):
|
2017-05-25 17:16:13 +01:00
|
|
|
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):
|
2017-05-28 11:13:36 +01:00
|
|
|
render_page(os.path.join(INTERNAL_TEMPLATES_DIR, 'toc-template.xsl'), os.path.join(TEMPLATES_DIR, 'toc.xsl'), context)
|