import re
def parse(markdown):
lines = markdown.split('\n')
res = ''
in_list = False
in_list_append = False
for line in lines:
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 = not in_list, 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 = '- ' + current_line + '
' if not in_list else '- ' + current_line + '
'
else:
if in_list:
in_list_append = 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:
line = '
' + line
in_list_append = False
res += line
if in_list:
res += ''
return res