diff --git a/python/markdown/markdown.py b/python/markdown/markdown.py
index cdea006..b52fe44 100644
--- a/python/markdown/markdown.py
+++ b/python/markdown/markdown.py
@@ -2,52 +2,63 @@ import re
def parse(markdown):
lines = markdown.split('\n')
- res = ''
- in_list = False
- in_list_append = False
+ result, in_list, finish_list = '', False, False
+
for line in lines:
+ # Heading formatting
if re.match('###### (.*)', line) is not None:
line = '
' + line[7:] + '
'
elif re.match('## (.*)', line) is not None:
line = '' + line[3:] + '
'
elif re.match('# (.*)', line) is not None:
line = '' + line[2:] + '
'
- m = re.match(r'\* (.*)', line)
- if m:
- in_list, is_bold, is_italic = True, False, False
- current_line = m.group(1)
- # Bold
- strongmatch = re.match('(.*)__(.*)__(.*)', current_line)
- if strongmatch:
- current_line = strongmatch.group(1) + '' + \
- strongmatch.group(2) + '' + strongmatch.group(3)
- is_bold = True
- # Italics
- italicmatch = re.match('(.*)_(.*)_(.*)', current_line)
- if italicmatch:
- current_line = italicmatch.group(1) + '' + italicmatch.group(2) + \
- '' + italicmatch.group(3)
- is_italic = True
-
- line = '' if not in_list else '' + current_line + ''
+
+ # List detection
+ is_list = re.match(r'\* (.*)', line) or False
+ line = is_list.group(1) if is_list else line
+
+ # Bold
+ strongmatch = re.match('(.*)__(.*)__(.*)', line)
+ if strongmatch:
+ line = strongmatch.group(1) + '' + \
+ strongmatch.group(2) + '' + strongmatch.group(3)
+
+ # Italics
+ italicmatch = re.match('(.*)_(.*)_(.*)', line)
+ if italicmatch:
+ line = italicmatch.group(1) + '' + italicmatch.group(2) + \
+ '' + italicmatch.group(3)
+
+ # If a list has been detected
+ if is_list:
+ if not in_list:
+ line = '- ' + line + '
'
+ in_list = True
+ else:
+ line = '- ' + line + '
'
else:
+ # If a list wasn't detected, but it's supposedly trying to continue a list
if in_list:
- in_list_append = True
+ finish_list = True
in_list = False
- m = re.match(''
- m = re.match('(.*)__(.*)__(.*)', line)
- if m:
- line = m.group(1) + '' + m.group(2) + '' + m.group(3)
- m = re.match('(.*)_(.*)_(.*)', line)
- if m:
- line = m.group(1) + '' + m.group(2) + '' + m.group(3)
- if in_list_append:
+ # Detect whether a heading, list or text paragraph has already been started
+ # This is just to ensure it's wrapped in something at the very least, and follows HTML sytnax.
+ occupied = re.match(''
+
+ # If a list has ended and it needs to be formally ended
+ if finish_list:
line = '
' + line
- in_list_append = False
- res += line
+ is_list = False
+ finish_list = False
+
+ # Finish this line and add it to the result.
+ result += line
+
+ # If we have somehow not ended a list yet, complete it now.
if in_list:
- res += ''
- return res
+ result += ''
+
+ return result
\ No newline at end of file