From cb2a0898e574f2761ca51e36ce5f1bc5b7676039 Mon Sep 17 00:00:00 2001 From: Glandos Date: Sat, 16 Jun 2018 23:01:03 +0200 Subject: [PATCH 1/3] Initialize markdown only once --- sigal/utils.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/sigal/utils.py b/sigal/utils.py index 411ac00..6c1d06a 100644 --- a/sigal/utils.py +++ b/sigal/utils.py @@ -29,6 +29,8 @@ VIDEO_MIMES = {'.mp4': 'video/mp4', '.webm': 'video/webm', '.ogv': 'video/ogg'} +MD = None + class Devnull(object): """'Black hole' for output that should not be printed""" @@ -68,26 +70,28 @@ def read_markdown(filename): """Reads markdown file, converts output and fetches title and meta-data for further processing. """ + global MD # Use utf-8-sig codec to remove BOM if it is present. This is only possible # this way prior to feeding the text to the markdown parser (which would # also default to pure utf-8) with open(filename, 'r', encoding='utf-8-sig') as f: text = f.read() - md = Markdown(extensions=['markdown.extensions.meta', - 'markdown.extensions.tables'], - output_format='html5') + if MD is None: + MD = Markdown(extensions=['markdown.extensions.meta', + 'markdown.extensions.tables'], + output_format='html5') # Mark HTML with Markup to prevent jinja2 autoescaping - output = {'description': Markup(md.convert(text))} + output = {'description': Markup(MD.convert(text))} try: - meta = md.Meta.copy() + meta = MD.Meta.copy() except AttributeError: pass else: output['meta'] = meta try: - output['title'] = md.Meta['title'][0] + output['title'] = MD.Meta['title'][0] except KeyError: pass From c3fc14c4f19c0404f8b882ee91c7338fe09a0205 Mon Sep 17 00:00:00 2001 From: Glandos Date: Tue, 19 Jun 2018 22:43:07 +0200 Subject: [PATCH 2/3] Call reset() if already initialized. This also include a workaround for a waiting-to-be-merged bug in python-markdown. --- sigal/utils.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sigal/utils.py b/sigal/utils.py index 6c1d06a..642641d 100644 --- a/sigal/utils.py +++ b/sigal/utils.py @@ -81,6 +81,12 @@ def read_markdown(filename): MD = Markdown(extensions=['markdown.extensions.meta', 'markdown.extensions.tables'], output_format='html5') + else: + MD.reset() + # When https://github.com/Python-Markdown/markdown/pull/672 + # will be available, this can be removed. + MD.Meta = {} + # Mark HTML with Markup to prevent jinja2 autoescaping output = {'description': Markup(MD.convert(text))} From f95393b114b350a78e482a101ba433a7c6ecd139 Mon Sep 17 00:00:00 2001 From: Glandos Date: Tue, 19 Jun 2018 22:43:43 +0200 Subject: [PATCH 3/3] empty content means empty meta dict, not absent --- tests/test_utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index d04f446..3f91704 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -66,7 +66,9 @@ def test_read_markdown_empty_file(tmpdir): src.write("") m = utils.read_markdown(str(src)) assert 'title' not in m - assert 'meta' not in m + # See https://github.com/Python-Markdown/markdown/pull/672 + # Meta attributes should always be there + assert m['meta'] == {} assert m['description'] == ''