File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -5,10 +5,12 @@ def parse_markdown(markdown):
55 lines = markdown .split ('\n ' )
66 html = ''
77 in_list = False
8+ in_list_append = False
89 for line in lines :
9- res = parse_line (line , in_list )
10+ res = parse_line (line , in_list , in_list_append )
1011 html += res ['line' ]
1112 in_list = res ['in_list' ]
13+ in_list_append = res ['in_list_append' ]
1214 if in_list :
1315 html += '</ul>'
1416 return html
@@ -47,7 +49,7 @@ def check_italic(line):
4749 return None
4850
4951
50- def parse_line (line , in_list ):
52+ def parse_line (line , in_list , in_list_append ):
5153 res = check_headers (line )
5254
5355 list_match = re .match (r'\* (.*)' , res )
@@ -60,7 +62,7 @@ def parse_line(line, in_list):
6062 res = wrap (list_match .group (1 ), 'li' )
6163 else :
6264 if in_list :
63- res += '</ul>'
65+ in_list_append = True
6466 in_list = False
6567
6668 if not re .match ('<h|<ul|<li' , res ):
@@ -74,7 +76,12 @@ def parse_line(line, in_list):
7476 while check_italic (res ):
7577 res = check_italic (res )
7678
79+ if in_list_append :
80+ res = '</ul>' + res
81+ in_list_append = False
82+
7783 return {
7884 'line' : res ,
79- 'in_list' : in_list
85+ 'in_list' : in_list ,
86+ 'in_list_append' : in_list_append
8087 }
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ def parse_markdown(markdown):
55 lines = markdown .split ('\n ' )
66 res = ''
77 in_list = False
8+ in_list_append = False
89 for i in lines :
910 if re .match ('###### (.*)' , i ) is not None :
1011 i = '<h6>' + i [7 :] + '</h6>'
@@ -49,7 +50,7 @@ def parse_markdown(markdown):
4950 i = '<li>' + curr + '</li>'
5051 else :
5152 if in_list :
52- i = '</ul>+i'
53+ in_list_append = True
5354 in_list = False
5455
5556 m = re .match ('<h|<ul|<p|<li' , i )
@@ -61,6 +62,9 @@ def parse_markdown(markdown):
6162 m = re .match ('(.*)_(.*)_(.*)' , i )
6263 if m :
6364 i = m .group (1 ) + '<em>' + m .group (2 ) + '</em>' + m .group (3 )
65+ if in_list_append :
66+ i = '</ul>' + i
67+ in_list_append = False
6468 res += i
6569 if in_list :
6670 res += '</ul>'
Original file line number Diff line number Diff line change 22from markdown import parse_markdown
33
44
5- # Tests adapted from `problem-specifications//canonical-data.json` @ v1.3 .0
5+ # Tests adapted from `problem-specifications//canonical-data.json` @ v1.4 .0
66
77class MarkdownTest (unittest .TestCase ):
88
@@ -64,6 +64,13 @@ def test_symbols_in_the_paragraph_text_that_should_not_be_interpreted(
6464 parse_markdown ('This is a paragraph with # and * in the text' ),
6565 '<p>This is a paragraph with # and * in the text</p>' )
6666
67+ def test_unordered_lists_close_properly_with_preceding_and_following_lines (
68+ self ):
69+ self .assertEqual (
70+ parse_markdown ('# Start a list\n * Item 1\n * Item 2\n End a list' ),
71+ '<h1>Start a list</h1><ul><li>Item 1</li>'
72+ '<li>Item 2</li></ul><p>End a list</p>' )
73+
6774
6875if __name__ == '__main__' :
6976 unittest .main ()
You can’t perform that action at this time.
0 commit comments