Merge branch 'pr/120' (ref #120).

This commit is contained in:
Simon Conseil
2014-10-15 23:45:35 +02:00
2 changed files with 32 additions and 7 deletions

View File

@@ -64,18 +64,27 @@ 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)
output = {'description': md.convert(text)}
return {
'title': md.Meta.get('title', [''])[0],
'description': html,
'meta': md.Meta.copy()
}
try:
meta = md.Meta.copy()
except AttributeError:
pass
else:
output['meta'] = meta
output['title'] = md.Meta.get('title', [''])[0]
return output
def call_subprocess(cmd):

View File

@@ -54,6 +54,22 @@ def test_read_markdown():
"<p>This is a funny description of this image</p>"
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'] == '<p>content</p>'
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