diff --git a/scripts/blog.py b/scripts/blog.py index 82f1cf3..93d7c8f 100644 --- a/scripts/blog.py +++ b/scripts/blog.py @@ -21,6 +21,7 @@ import shutil import traceback import time import re +from typing import List import jinja2 import inotify.adapters @@ -79,6 +80,13 @@ def parse_nikola_date(match): def parse_complete_date(match): return datetime.datetime.strptime(match.group(0), '%Y-%m-%d %H:%M:%S %Z%z') +def split_tags(tags: str) -> List[str]: + if isinstance(tags, str): + return [tag.strip() for tag in tags.split(',')] + elif isinstance(tags, list): + return tags + else: + raise NotImplementedError("Unknown tag type: {}".format(type(tags))) def slugify(title): """ @@ -163,7 +171,12 @@ def load_doc(filepath): def render_article(doc, front_matter, f): - result = ARTICLE_TEMPLATE.render(content=doc, title=front_matter['title']) + result = ARTICLE_TEMPLATE.render( + content=doc, + title=front_matter['title'], + post_publication_date=front_matter['date'], + post_tags=split_tags(front_matter['tags']), + ) f.write(result) def regen_all(source_top, dest_top, docs=None): @@ -174,7 +187,13 @@ def regen_all(source_top, dest_top, docs=None): os.makedirs(os.path.dirname(doc_full_path), exist_ok=True) # print("==", doc_full_path) with open(doc_full_path + '.html', 'wt') as f: - render_article(doc, front_matter, f) + try: + render_article(doc, front_matter, f) + except: + logging.error(traceback.format_exc()) + logging.error("Rendering failed 😿") + continue + for static in STATIC_RESOURCES: src_path = static[0] @@ -212,7 +231,13 @@ def main(source_top, dest_top): filepath = os.path.join(directory, file) if filepath.startswith(STATIC_PATH): t0 = time.time() - update_statics() + try: + update_statics() + except: + logging.error(traceback.format_exc()) + logging.error("Loading new templates failed 😿") + continue + is_static_resource = False for static in STATIC_RESOURCES: src_path = static[0] @@ -252,7 +277,13 @@ def main(source_top, dest_top): os.makedirs(os.path.dirname(doc_full_path), exist_ok=True) # print("==", doc_full_path) with open(doc_full_path + '.html', 'wt') as f: - render_article(doc, front_matter, f) + try: + render_article(doc, front_matter, f) + except: + logging.error(traceback.format_exc()) + logging.error("Rendering failed 😿") + continue + logging.info("Updated all in {:.2f}s".format(time.time() - t0)) diff --git a/static/article.tmpl.html b/static/article.tmpl.html index 144132b..d2750ea 100644 --- a/static/article.tmpl.html +++ b/static/article.tmpl.html @@ -29,7 +29,19 @@