From 1c937e91884be06e3ae98bda51053cdf25b10179 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 25 May 2017 21:33:18 +0100 Subject: [PATCH] Render submission date --- md_pdf/assets/templates/cover-template.html | 4 ++++ md_pdf/assets/templates/header-template.html | 6 +++++- md_pdf/build/context.py | 15 ++++++++++++--- md_pdf/config/validate.py | 18 ++++++++++++++++-- md_pdf/consts.py | 4 ++++ setup.py | 1 + test-files/mdp.yml | 1 + 7 files changed, 43 insertions(+), 6 deletions(-) diff --git a/md_pdf/assets/templates/cover-template.html b/md_pdf/assets/templates/cover-template.html index 53f95a4..cac7519 100644 --- a/md_pdf/assets/templates/cover-template.html +++ b/md_pdf/assets/templates/cover-template.html @@ -16,5 +16,9 @@ {% if turnitin_number %}

TurnItIn Number: {{ turnitin_number }}

{% endif %} + + {% if submission_date %} +

Submission Date: {{ submission_date }}

+ {% endif %} diff --git a/md_pdf/assets/templates/header-template.html b/md_pdf/assets/templates/header-template.html index 3f8c9a8..24b1048 100644 --- a/md_pdf/assets/templates/header-template.html +++ b/md_pdf/assets/templates/header-template.html @@ -8,7 +8,11 @@ - + + {% if submission_date %} + Date: {{ submission_date }} + {% endif %} + diff --git a/md_pdf/build/context.py b/md_pdf/build/context.py index 8978051..28be8b4 100644 --- a/md_pdf/build/context.py +++ b/md_pdf/build/context.py @@ -1,13 +1,16 @@ -from md_pdf.consts import TEMPLATES_DIR, STATIC_DIR +from md_pdf.consts import TEMPLATES_DIR, STATIC_DIR, DATE_FORMAT, TIME_FORMAT, DATETIME_FORMAT from word_count import word_count from md_pdf.utils import get_plain_text -from datetime import datetime +from dateutil import parser +import datetime EXTRA_CONTEXT = { 'templates_dir': TEMPLATES_DIR, 'static_dir': STATIC_DIR, - 'date': datetime.now() + 'date': datetime.datetime.now().strftime(DATE_FORMAT), + 'time': datetime.datetime.now().strftime(TIME_FORMAT), + 'datetime': datetime.datetime.now().strftime(DATETIME_FORMAT) } @@ -25,4 +28,10 @@ def get_context(config, content): ) if config.get('show_word_count'): context['word_count'] = word_count(get_plain_text(content)) + if config.get('submission_date'): + if type(config['submission_date']) in [datetime.date, datetime.datetime, datetime.time]: + submission_date = config['submission_date'] + else: + submission_date = parser.parse(config['submission_date']) + context['submission_date'] = submission_date.strftime(DATE_FORMAT) return context diff --git a/md_pdf/config/validate.py b/md_pdf/config/validate.py index c8dc41e..5eaf687 100644 --- a/md_pdf/config/validate.py +++ b/md_pdf/config/validate.py @@ -3,7 +3,8 @@ from md_pdf.consts import CSL_DIR import glob import os import logging - +from dateutil import parser +import datetime logger = logging.getLogger(__file__) @@ -83,6 +84,18 @@ def validate_wordcount(config): raise ConfigValidationException("Show word count key should be either true or false") +def validate_submission_date(config): + if 'submission_date' not in config: + return + if type(config['submission_date']) in [datetime.date, datetime.datetime, datetime.time]: + return + try: + parser.parse(config['submission_date']) + except ValueError: + raise ConfigValidationException("Invalid Submission Date format") + + + def validate_config(config): logger.debug("Validating Config...") for validator in [ @@ -92,7 +105,8 @@ def validate_config(config): validate_bibliography, validate_context, validate_toc, - validate_wordcount + validate_wordcount, + validate_submission_date ]: validator(config) logger.debug("Config Ok!") diff --git a/md_pdf/consts.py b/md_pdf/consts.py index a0aadb9..7e3620e 100644 --- a/md_pdf/consts.py +++ b/md_pdf/consts.py @@ -11,3 +11,7 @@ STATIC_DIR = os.path.join(ASSET_DIR, 'static') CONFIG_FILE = os.path.join(WORKING_DIR, 'mdp.yml') CSL_DOWNLOAD_LINK = "https://github.com/citation-style-language/styles/archive/master.zip" + +DATE_FORMAT = "%d %B %Y" +TIME_FORMAT = "%H:%M" +DATETIME_FORMAT = "{} {}".format(DATE_FORMAT, TIME_FORMAT) diff --git a/setup.py b/setup.py index 6094970..5a59877 100644 --- a/setup.py +++ b/setup.py @@ -12,6 +12,7 @@ setup( "progressbar2==3.16.0", "pypandoc==1.3.3", "pyscss==1.3.5", + "python-dateutil==2.6.0", "PyYAML==3.12", "word-count==0.1.0" ], diff --git a/test-files/mdp.yml b/test-files/mdp.yml index 538845b..bfd54ef 100644 --- a/test-files/mdp.yml +++ b/test-files/mdp.yml @@ -14,3 +14,4 @@ context: title: test title toc: true show_word_count: true +submission_date: 2017-01-01