Permalink
Browse files

Merge pull request #329 from Glandos/performance

Initialize markdown only once
  • Loading branch information...
saimn committed Jul 8, 2018
2 parents e33c4e3 + f95393b commit c02f6ff2637abddab970bee6dd545781522244ad
Showing with 19 additions and 7 deletions.
  1. +16 −6 sigal/utils.py
  2. +3 −1 tests/test_utils.py
@@ -29,6 +29,8 @@
'.webm': 'video/webm',
'.ogv': 'video/ogg'}
MD = None
class Devnull(object):
"""'Black hole' for output that should not be printed"""
@@ -68,26 +70,34 @@ 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')
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))}
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
@@ -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'] == ''

0 comments on commit c02f6ff

Please sign in to comment.