From b2715d4a236c3aa8a24472427cccdd637a802fea Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 17 May 2017 14:46:11 +0100 Subject: [PATCH 01/14] Add basic TOC file --- md_pdf/assets/templates/toc.xsl | 61 +++++++++++++++++++++++++++++++++ md_pdf/build/pdf.py | 6 ++++ 2 files changed, 67 insertions(+) create mode 100644 md_pdf/assets/templates/toc.xsl diff --git a/md_pdf/assets/templates/toc.xsl b/md_pdf/assets/templates/toc.xsl new file mode 100644 index 0000000..20b6cc2 --- /dev/null +++ b/md_pdf/assets/templates/toc.xsl @@ -0,0 +1,61 @@ + + + + + + + Table of Contents + + + + +

Table of Contents

+
+ + +
+ +
  • + + + +
      + added to prevent self-closing tags in QtXmlPatterns + +
    +
  • +
    +
    diff --git a/md_pdf/build/pdf.py b/md_pdf/build/pdf.py index 12d7352..b84409d 100644 --- a/md_pdf/build/pdf.py +++ b/md_pdf/build/pdf.py @@ -14,6 +14,12 @@ 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 +} + PDF_OPTIONS = { "no-pdf-compression": "", "enable-internal-links": "", From 5c83842116389b5990fbd368352072d6e4c9a819 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 17 May 2017 14:54:40 +0100 Subject: [PATCH 02/14] Actually render it --- md_pdf/build/pdf.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/md_pdf/build/pdf.py b/md_pdf/build/pdf.py index b84409d..07ecb19 100644 --- a/md_pdf/build/pdf.py +++ b/md_pdf/build/pdf.py @@ -49,5 +49,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=OUTPUT_COVER_FILE, + toc=TOC_OPTIONS, + cover_first=True ) From 3e4b951bf1cf5706414c217ba8d68d6dbf2269f0 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 17 May 2017 15:02:52 +0100 Subject: [PATCH 03/14] Change file names --- md_pdf/build/__init__.py | 4 ++-- md_pdf/build/{template.py => content.py} | 0 md_pdf/build/pdf.py | 2 +- md_pdf/build/{cover.py => templates.py} | 0 4 files changed, 3 insertions(+), 3 deletions(-) rename md_pdf/build/{template.py => content.py} (100%) rename md_pdf/build/{cover.py => templates.py} (100%) diff --git a/md_pdf/build/__init__.py b/md_pdf/build/__init__.py index 5b9f885..daf9f3e 100644 --- a/md_pdf/build/__init__.py +++ b/md_pdf/build/__init__.py @@ -1,9 +1,9 @@ from md_pdf.build.md import read_files from md_pdf.build.pandoc import build_document, output_html -from md_pdf.build.cover import render_cover +from md_pdf.build.templates import render_cover from md_pdf.build.css import render_css from md_pdf.build.pdf import export_pdf -from md_pdf.build.template import parse_template +from md_pdf.build.content import parse_template import os import logging import time diff --git a/md_pdf/build/template.py b/md_pdf/build/content.py similarity index 100% rename from md_pdf/build/template.py rename to md_pdf/build/content.py diff --git a/md_pdf/build/pdf.py b/md_pdf/build/pdf.py index 07ecb19..896c990 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.cover import OUTPUT_COVER_FILE +from md_pdf.build.templates import OUTPUT_COVER_FILE import os import logging diff --git a/md_pdf/build/cover.py b/md_pdf/build/templates.py similarity index 100% rename from md_pdf/build/cover.py rename to md_pdf/build/templates.py From 19df5f4b2ab79b77024017776288620fba58d3c8 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 17 May 2017 17:52:57 +0100 Subject: [PATCH 04/14] Render pdf templates with jinja --- md_pdf/assets/templates/footer-template.html | 16 +++++ md_pdf/assets/templates/footer.html | 2 +- md_pdf/assets/templates/header-template.html | 9 +++ md_pdf/assets/templates/header.html | 2 +- md_pdf/assets/templates/toc-template.xsl | 62 ++++++++++++++++++++ md_pdf/assets/templates/toc.xsl | 5 +- md_pdf/build/__init__.py | 4 +- md_pdf/build/pdf.py | 7 +-- md_pdf/build/templates.py | 33 ++++++++--- 9 files changed, 121 insertions(+), 19 deletions(-) create mode 100644 md_pdf/assets/templates/footer-template.html create mode 100644 md_pdf/assets/templates/header-template.html create mode 100644 md_pdf/assets/templates/toc-template.xsl 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 @@
  • - +
  • - + \ 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) From fc6b04ea878da9c7e27a89b53950d5cbc5d8a749 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 17 May 2017 17:57:30 +0100 Subject: [PATCH 05/14] Remove old files now created by template --- .gitignore | 3 ++ md_pdf/assets/templates/footer.html | 16 -------- md_pdf/assets/templates/header.html | 9 ----- md_pdf/assets/templates/toc.xsl | 62 ----------------------------- 4 files changed, 3 insertions(+), 87 deletions(-) delete mode 100644 md_pdf/assets/templates/footer.html delete mode 100644 md_pdf/assets/templates/header.html delete mode 100644 md_pdf/assets/templates/toc.xsl diff --git a/.gitignore b/.gitignore index d1ac150..edd2d08 100644 --- a/.gitignore +++ b/.gitignore @@ -246,6 +246,9 @@ ENV/ out/ md_pdf/assets/templates/cover.html +md_pdf/assets/templates/header.html +md_pdf/assets/templates/footer.html +md_pdf/assets/templates/toc.xsl md_pdf/assets/static/style.css md_pdf/assets/csl/ md_pdf/assets/styles-master/ diff --git a/md_pdf/assets/templates/footer.html b/md_pdf/assets/templates/footer.html deleted file mode 100644 index acdda22..0000000 --- a/md_pdf/assets/templates/footer.html +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - -
    - Page of -
    - - - \ No newline at end of file diff --git a/md_pdf/assets/templates/header.html b/md_pdf/assets/templates/header.html deleted file mode 100644 index 3903f09..0000000 --- a/md_pdf/assets/templates/header.html +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/md_pdf/assets/templates/toc.xsl b/md_pdf/assets/templates/toc.xsl deleted file mode 100644 index 886202f..0000000 --- a/md_pdf/assets/templates/toc.xsl +++ /dev/null @@ -1,62 +0,0 @@ - - - - - - - Table of Contents - - - - - -

    Table of Contents

    -
    - - -
    - -
  • - - - -
      - added to prevent self-closing tags in QtXmlPatterns - -
    -
  • - - \ No newline at end of file From 81ab25fdfd0d6bcc4d83d8ff0a651e354c740747 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 17 May 2017 18:04:45 +0100 Subject: [PATCH 06/14] reformat template --- md_pdf/assets/templates/toc-template.xsl | 34 +++++++++++++++--------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/md_pdf/assets/templates/toc-template.xsl b/md_pdf/assets/templates/toc-template.xsl index b95d6da..1d2d051 100644 --- a/md_pdf/assets/templates/toc-template.xsl +++ b/md_pdf/assets/templates/toc-template.xsl @@ -1,16 +1,16 @@ - - + + - Table of Contents - - +

    Table of Contents

      @@ -42,8 +24,8 @@
    • -
      - + diff --git a/test-files/2-pandoc.md b/test-files/2-pandoc.md index e248668..ba44409 100644 --- a/test-files/2-pandoc.md +++ b/test-files/2-pandoc.md @@ -21,3 +21,6 @@ - Citation with suffix only [@item1 and nowhere else]. - With some markup [*see* @item1 p. **32**]. + + +##### More referencing tests From 00badaa7e212f829d2b0d692c3f6fcc5623aa036 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 17 May 2017 20:29:03 +0100 Subject: [PATCH 08/14] Add config key for toc --- md_pdf/config/validate.py | 10 +++++++++- test-files/mdp.yml | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/md_pdf/config/validate.py b/md_pdf/config/validate.py index a9d5623..22e20cf 100644 --- a/md_pdf/config/validate.py +++ b/md_pdf/config/validate.py @@ -69,12 +69,20 @@ def validate_context(config): raise ConfigValidationException("Context keys must be plain. Invalid values: {}".format(", ".join(invalid_values))) +def validate_toc(config): + if 'toc' not in config: + return + if type(config['toc']) != bool: + raise ConfigValidationException("Table of contents key should be either true or false") + + def validate_config(config): for validator in [ check_required_keys, test_input, test_output, validate_bibliography, - validate_context + validate_context, + validate_toc ]: validator(config) diff --git a/test-files/mdp.yml b/test-files/mdp.yml index 411e971..661ce22 100644 --- a/test-files/mdp.yml +++ b/test-files/mdp.yml @@ -12,3 +12,4 @@ context: student_number: 123456 turnitin_number: 789123 title: test title +toc: true From b5bbc2d22afe45ab7b0870c640a77885e3649a97 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 17 May 2017 20:49:58 +0100 Subject: [PATCH 09/14] Allow toggling of toc --- md_pdf/build/pdf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/md_pdf/build/pdf.py b/md_pdf/build/pdf.py index 9c26923..b498b5d 100644 --- a/md_pdf/build/pdf.py +++ b/md_pdf/build/pdf.py @@ -49,6 +49,6 @@ def export_pdf(content, config): os.path.join(os.path.abspath(config['output_dir']), 'output.pdf'), options=PDF_OPTIONS, cover=FILE_NAME_FORMAT.format('cover'), - toc=TOC_OPTIONS, + toc=TOC_OPTIONS if config['toc'] else {}, cover_first=True ) From 444a07cf5f499f9e16a1a13185c262a741d2827b Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 17 May 2017 21:12:53 +0100 Subject: [PATCH 10/14] Cleanup template --- md_pdf/assets/templates/toc-template.xsl | 5 ----- 1 file changed, 5 deletions(-) diff --git a/md_pdf/assets/templates/toc-template.xsl b/md_pdf/assets/templates/toc-template.xsl index 38985e6..bf5f905 100644 --- a/md_pdf/assets/templates/toc-template.xsl +++ b/md_pdf/assets/templates/toc-template.xsl @@ -4,10 +4,6 @@ xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:outline="http://wkhtmltopdf.org/outline" xmlns="http://www.w3.org/1999/xhtml"> - @@ -44,7 +40,6 @@
        - added to prevent self-closing tags in QtXmlPatterns
    • From 71b641151ef3e07947bfd48225fe0a5fa562e360 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 17 May 2017 21:14:12 +0100 Subject: [PATCH 11/14] Fix lint --- md_pdf/assets/static/style.scss | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/md_pdf/assets/static/style.scss b/md_pdf/assets/static/style.scss index c9c2723..522df91 100644 --- a/md_pdf/assets/static/style.scss +++ b/md_pdf/assets/static/style.scss @@ -62,13 +62,13 @@ body.footer, body.header { body.tocs { h1 { - text-align: center; margin-bottom: 45px; + text-align: center; } .row { - border-bottom: 1px dashed black; margin-bottom: $font-size-base; + border-bottom: 1px dashed black; font-size: $font-size-base * 1.5; } From a9038e9315643ba760109dc7988d2b8bbc973fcc Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 17 May 2017 21:25:06 +0100 Subject: [PATCH 12/14] Better log output --- md_pdf/build/templates.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/md_pdf/build/templates.py b/md_pdf/build/templates.py index eca1a29..3c8c4ef 100644 --- a/md_pdf/build/templates.py +++ b/md_pdf/build/templates.py @@ -15,7 +15,7 @@ TEMPLATE_FORMAT = os.path.join(TEMPLATES_DIR, "{}-template.html") def render_page(input_file, output_file, context): - logger.debug("Rendering {}...") + logger.debug("Rendering {}...".format(os.path.splitext(os.path.basename(output_file))[0].title())) with open(input_file) as f: template = Template(f.read()) with open(output_file, "w") as f: @@ -34,4 +34,5 @@ def render_templates(config): '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) + if config.get('toc', False): + render_page(os.path.join(TEMPLATES_DIR, 'toc-template.xsl'), os.path.join(TEMPLATES_DIR, 'toc.xsl'), context) From 9b8d861042eb31b107f4509eac1a9f9eada41a5d Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 17 May 2017 21:38:01 +0100 Subject: [PATCH 13/14] Log on config validation --- md_pdf/config/validate.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/md_pdf/config/validate.py b/md_pdf/config/validate.py index 22e20cf..d7e0199 100644 --- a/md_pdf/config/validate.py +++ b/md_pdf/config/validate.py @@ -77,6 +77,7 @@ def validate_toc(config): def validate_config(config): + logger.debug("Validating Config...") for validator in [ check_required_keys, test_input, @@ -86,3 +87,4 @@ def validate_config(config): validate_toc ]: validator(config) + logger.debug("Config Ok!") From a193728ed5383a23554ce2253099ab5f85c3b281 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 23 May 2017 21:59:40 +0100 Subject: [PATCH 14/14] Dont show references title in toc --- md_pdf/assets/templates/toc-template.xsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/md_pdf/assets/templates/toc-template.xsl b/md_pdf/assets/templates/toc-template.xsl index bf5f905..f7ad75d 100644 --- a/md_pdf/assets/templates/toc-template.xsl +++ b/md_pdf/assets/templates/toc-template.xsl @@ -19,7 +19,7 @@
    • - +