exercism-solutions/python/markdown/markdown.py
2021-06-11 18:38:20 +03:00

35 lines
811 B
Python

import re
def parse(markdown):
res = ''
in_list = False
for line in markdown.splitlines():
match = re.match(r'^(#{1,6}) (.*)', line)
if match:
level = len(match.group(1))
res += f'<h{level}>{match.group(2)}</h{level}>'
continue
line = re.sub(r'__(.*)__', r'<strong>\1</strong>', line)
line = re.sub(r'_(.*)_', r'<em>\1</em>', line)
match = re.match(r'\* (.*)', line)
if match:
if not in_list:
in_list = True
res += '<ul>'
res += '<li>' + match.group(1) + '</li>'
continue
if in_list:
in_list = False
res += '</ul>'
res += '<p>' + line + '</p>'
if in_list:
res += '</ul>'
return res