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

30 lines
737 B
Python

import re
def parse(markdown):
result = ''
for line in markdown.splitlines():
line = re.sub(r'__(.*)__', r'<strong>\1</strong>', line)
line = re.sub(r'_(.*)_', r'<em>\1</em>', line)
match = re.match(r'^(#{1,6}) (.*)', line)
if match:
level = len(match.group(1))
result += f'<h{level}>{match.group(2)}</h{level}>'
continue
match = re.match(r'\* (.*)', line)
if match:
if result.endswith('</ul>'):
result = result[:-5]
else:
result += '<ul>'
result += '<li>' + match.group(1) + '</li></ul>'
continue
result += '<p>' + line + '</p>'
return result