From 594ed8825d6c230576ae9c94715e97f936c84e5c Mon Sep 17 00:00:00 2001 From: Kai Fricke Date: Mon, 13 Oct 2014 16:14:55 +0200 Subject: [PATCH 1/2] Handling of empty markdown or missing meta-data Respect markdown files which do not contain any meta-data. --- sigal/utils.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/sigal/utils.py b/sigal/utils.py index a751fbc..da060b2 100644 --- a/sigal/utils.py +++ b/sigal/utils.py @@ -64,18 +64,22 @@ def url_from_path(path): def read_markdown(filename): - # Use utf-8-sig codec to remove BOM if it is present + """Reads markdown file, converts output and fetches title and meta-data for further processing.""" + # 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 codecs.open(filename, 'r', 'utf-8-sig') as f: text = f.read() - md = Markdown(extensions=['meta'], output_format='html5') html = md.convert(text) - - return { - 'title': md.Meta.get('title', [''])[0], - 'description': html, - 'meta': md.Meta.copy() - } + try: + meta = md.Meta.copy() + except (AttributeError): + meta = None + if meta: + title = md.Meta.get('title', [''])[0] + else: + title = None + return {'title': title, 'description': html, 'meta': meta} def call_subprocess(cmd): From aaf3a7d13e1a9b63f4494f5be6b72c97b78420bf Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Wed, 15 Oct 2014 23:43:53 +0200 Subject: [PATCH 2/2] read_markdown returns meta only if found, + add test. (Closes #120) --- sigal/utils.py | 25 +++++++++++++++---------- tests/test_utils.py | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/sigal/utils.py b/sigal/utils.py index da060b2..3d3bff8 100644 --- a/sigal/utils.py +++ b/sigal/utils.py @@ -64,22 +64,27 @@ def url_from_path(path): def read_markdown(filename): - """Reads markdown file, converts output and fetches title and meta-data for further processing.""" - # 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) + """Reads markdown file, converts output and fetches title and meta-data for + further processing. + """ + # 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 codecs.open(filename, 'r', 'utf-8-sig') as f: text = f.read() + md = Markdown(extensions=['meta'], output_format='html5') - html = md.convert(text) + output = {'description': md.convert(text)} + try: meta = md.Meta.copy() - except (AttributeError): - meta = None - if meta: - title = md.Meta.get('title', [''])[0] + except AttributeError: + pass else: - title = None - return {'title': title, 'description': html, 'meta': meta} + output['meta'] = meta + output['title'] = md.Meta.get('title', [''])[0] + + return output def call_subprocess(cmd): diff --git a/tests/test_utils.py b/tests/test_utils.py index 902c622..dcacc7e 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -54,6 +54,22 @@ def test_read_markdown(): "

This is a funny description of this image

" +def test_read_markdown_empty_file(tmpdir): + src = tmpdir.join("file.txt") + src.write("content") + m = utils.read_markdown(str(src)) + assert m['title'] == '' + assert m['meta'] == {} + assert m['description'] == '

content

' + + src = tmpdir.join("empty.txt") + src.write("") + m = utils.read_markdown(str(src)) + assert 'title' not in m + assert 'meta' not in m + assert m['description'] == '' + + def test_call_subprocess(): returncode, stdout, stderr = utils.call_subprocess(['echo', 'ok']) assert returncode == 0