Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed reference markdown parsing bug
#14 (comment)

@anko noticed that references starting with spaces were not
parsed correctly by formd. This commit fixes the parsing error
and adds a new unit test, `test_space_ref` to validate the
reference parser.
  • Loading branch information
Seth Brown committed Jan 17, 2015
1 parent 81814ab commit 3e08bc6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/formd.py
Expand Up @@ -21,7 +21,7 @@ def __init__(self, text):
(\[[^\[\]:]*?\]| # ref
\(.*?\r?\n?.*?\)\)?) # url
""", re.MULTILINE | re.X)
self.match_refs = re.compile(r'(?<=\n)\[[^^\r?\n]*?\]:\s?.*')
self.match_refs = re.compile(r'(?<=\n)\s?.*\[[^^\r?\n]*?\]:\s?.*')
self.data = []

def _links(self):
Expand All @@ -34,7 +34,8 @@ def _links(self):
def _refs(self):
""" Find Markdown references"""
refs = re.findall(self.match_refs, self.text)
refs.sort()
refs = sorted([_.lstrip().replace('\n', '').replace('\r', '')
for _ in refs])
refs = OrderedDict(i.split(":", 1) for i in refs)
return refs

Expand Down
8 changes: 8 additions & 0 deletions src/tests/tests.py
Expand Up @@ -10,6 +10,7 @@ class TestForMd(unittest.TestCase):
inl = '[Markdown](http://en.wikipedia.com/wiki/Markdown)'
lref = '+ A: [Markdown][1]\n\n\n[1]: http://en.wikipedia.com/wiki/Markdown'
linl = '+ A: [Markdown](http://en.wikipedia.com/wiki/Markdown)'
space_ref = '[Markdown][1]\n\n\n [1]: http://en.wikipedia.com/wiki/Markdown'

def test_none(self):
f = ForMd(self.no_md)
Expand Down Expand Up @@ -49,5 +50,12 @@ def test_flip_lst_link(self):
inline_conv = list(f.inline_md())[0]
self.assertEqual(self.linl, inline_conv)

def test_space_ref(self):
""" Format links embedded in markdown lists
"""
f = ForMd(self.space_ref)
ref_conv = list(f.ref_md())[0]
self.assertEqual(self.ref, ref_conv)

if __name__ == '__main__':
unittest.main()

0 comments on commit 3e08bc6

Please sign in to comment.