diff --git a/md_pdf/assets/templates/footer-template.html b/md_pdf/assets/templates/footer-template.html new file mode 100644 index 0000000..fdf7715 --- /dev/null +++ b/md_pdf/assets/templates/footer-template.html @@ -0,0 +1,16 @@ + + + + + + + + + + +
+ Page of +
+ + + diff --git a/md_pdf/assets/templates/footer.html b/md_pdf/assets/templates/footer.html index fdf7715..acdda22 100644 --- a/md_pdf/assets/templates/footer.html +++ b/md_pdf/assets/templates/footer.html @@ -13,4 +13,4 @@ - + \ No newline at end of file diff --git a/md_pdf/assets/templates/header-template.html b/md_pdf/assets/templates/header-template.html new file mode 100644 index 0000000..91e760b --- /dev/null +++ b/md_pdf/assets/templates/header-template.html @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/md_pdf/assets/templates/header.html b/md_pdf/assets/templates/header.html index 91e760b..3903f09 100644 --- a/md_pdf/assets/templates/header.html +++ b/md_pdf/assets/templates/header.html @@ -6,4 +6,4 @@ - + \ No newline at end of file diff --git a/md_pdf/assets/templates/toc-template.xsl b/md_pdf/assets/templates/toc-template.xsl new file mode 100644 index 0000000..b95d6da --- /dev/null +++ b/md_pdf/assets/templates/toc-template.xsl @@ -0,0 +1,62 @@ + + + + + + + Table of Contents + + + + + +

Table of Contents

+
+ + +
+ +
  • + + + +
      + added to prevent self-closing tags in QtXmlPatterns + +
    +
  • +
    +
    diff --git a/md_pdf/assets/templates/toc.xsl b/md_pdf/assets/templates/toc.xsl index 20b6cc2..886202f 100644 --- a/md_pdf/assets/templates/toc.xsl +++ b/md_pdf/assets/templates/toc.xsl @@ -29,6 +29,7 @@ ul ul {padding-left: 1em;} a {text-decoration:none; color: black;} +

    Table of Contents

    @@ -38,7 +39,7 @@
  • - +
    @@ -58,4 +59,4 @@
  • - + \ No newline at end of file diff --git a/md_pdf/build/__init__.py b/md_pdf/build/__init__.py index daf9f3e..29a47d8 100644 --- a/md_pdf/build/__init__.py +++ b/md_pdf/build/__init__.py @@ -1,6 +1,6 @@ from md_pdf.build.md import read_files from md_pdf.build.pandoc import build_document, output_html -from md_pdf.build.templates import render_cover +from md_pdf.build.templates import render_templates from md_pdf.build.css import render_css from md_pdf.build.pdf import export_pdf from md_pdf.build.content import parse_template @@ -20,7 +20,7 @@ def build(config): if 'html' in config['output_formats']: output_html(parsed_template, os.path.abspath(config['output_dir'])) if 'pdf' in config['output_formats']: - render_cover(config) + render_templates(config) render_css() export_pdf(parsed_template, config) logger.info('Output completed in {:.2f} seconds.'.format(time.time() - start_time)) diff --git a/md_pdf/build/pdf.py b/md_pdf/build/pdf.py index 896c990..9c26923 100644 --- a/md_pdf/build/pdf.py +++ b/md_pdf/build/pdf.py @@ -1,6 +1,6 @@ import pdfkit from md_pdf.consts import TEMPLATES_DIR, STATIC_DIR -from md_pdf.build.templates import OUTPUT_COVER_FILE +from md_pdf.build.templates import FILE_NAME_FORMAT import os import logging @@ -14,10 +14,9 @@ DEFAULT_MARGIN_HORIZONTAL = '2.5cm' STYLE_FILE = os.path.join(STATIC_DIR, 'style.css') HEADER_FILE = os.path.join(TEMPLATES_DIR, 'header.html') FOOTER_FILE = os.path.join(TEMPLATES_DIR, 'footer.html') -TOC_TEMPLATE_FILE = os.path.join(TEMPLATES_DIR, 'toc.xsl') TOC_OPTIONS = { - 'xsl-style-sheet': TOC_TEMPLATE_FILE + 'xsl-style-sheet': os.path.join(TEMPLATES_DIR, 'toc.xsl') } PDF_OPTIONS = { @@ -49,7 +48,7 @@ def export_pdf(content, config): content, os.path.join(os.path.abspath(config['output_dir']), 'output.pdf'), options=PDF_OPTIONS, - cover=OUTPUT_COVER_FILE, + cover=FILE_NAME_FORMAT.format('cover'), toc=TOC_OPTIONS, cover_first=True ) diff --git a/md_pdf/build/templates.py b/md_pdf/build/templates.py index dc3b3d5..eca1a29 100644 --- a/md_pdf/build/templates.py +++ b/md_pdf/build/templates.py @@ -1,22 +1,37 @@ from jinja2 import Template -from md_pdf.consts import TEMPLATES_DIR +from md_pdf.consts import TEMPLATES_DIR, STATIC_DIR import os import logging logger = logging.getLogger(__file__) +EXTRA_CONFIG = { + 'templates_dir': TEMPLATES_DIR, + 'static_dir': STATIC_DIR +} -COVER_TEMPLATE = os.path.join(TEMPLATES_DIR, 'cover-template.html') -OUTPUT_COVER_FILE = os.path.join(TEMPLATES_DIR, 'cover.html') +FILE_NAME_FORMAT = os.path.join(TEMPLATES_DIR, "{}.html") +TEMPLATE_FORMAT = os.path.join(TEMPLATES_DIR, "{}-template.html") -def render_cover(config): - logger.debug("Rendering Cover...") - context = config['context'].copy() - context['title'] = config['title'] - with open(COVER_TEMPLATE) as f: +def render_page(input_file, output_file, context): + logger.debug("Rendering {}...") + with open(input_file) as f: template = Template(f.read()) - with open(OUTPUT_COVER_FILE, "w") as f: + with open(output_file, "w") as f: cover = template.render(context) f.write(cover) return cover + + +def render_templates(config): + context = config['context'].copy() + context['title'] = config['title'] + context = dict(context, **EXTRA_CONFIG) + for template in [ + 'cover', + 'header', + 'footer' + ]: + render_page(TEMPLATE_FORMAT.format(template), FILE_NAME_FORMAT.format(template), context) + render_page(os.path.join(TEMPLATES_DIR, 'toc-template.xsl'), os.path.join(TEMPLATES_DIR, 'toc.xsl'), context)