Files
python/exercises/practice/markdown/markdown.py

78 lines
2.7 KiB
Python
Raw Normal View History

2017-10-14 21:13:38 +03:00
import re
def parse(markdown):
2017-10-14 21:13:38 +03:00
lines = markdown.split('\n')
res = ''
in_list = False
in_list_append = False
2017-10-14 21:13:38 +03:00
for i in lines:
if re.match('###### (.*)', i) is not None:
i = '<h6>' + i[7:] + '</h6>'
elif re.match('##### (.*)', i) is not None:
i = '<h5>' + i[6:] + '</h5>'
elif re.match('#### (.*)', i) is not None:
i = '<h4>' + i[5:] + '</h4>'
elif re.match('### (.*)', i) is not None:
i = '<h3>' + i[4:] + '</h3>'
2017-10-14 21:13:38 +03:00
elif re.match('## (.*)', i) is not None:
i = '<h2>' + i[3:] + '</h2>'
elif re.match('# (.*)', i) is not None:
i = '<h1>' + i[2:] + '</h1>'
m = re.match(r'\* (.*)', i)
if m:
if not in_list:
in_list = True
is_bold = False
is_italic = False
curr = m.group(1)
m1 = re.match('(.*)__(.*)__(.*)', curr)
if m1:
curr = m1.group(1) + '<strong>' + \
m1.group(2) + '</strong>' + m1.group(3)
is_bold = True
m1 = re.match('(.*)_(.*)_(.*)', curr)
if m1:
curr = m1.group(1) + '<em>' + m1.group(2) + \
'</em>' + m1.group(3)
is_italic = True
i = '<ul><li>' + curr + '</li>'
2017-10-14 21:13:38 +03:00
else:
is_bold = False
is_italic = False
curr = m.group(1)
m1 = re.match('(.*)__(.*)__(.*)', curr)
if m1:
is_bold = True
m1 = re.match('(.*)_(.*)_(.*)', curr)
if m1:
is_italic = True
if is_bold:
curr = m1.group(1) + '<strong>' + \
m1.group(2) + '</strong>' + m1.group(3)
if is_italic:
2017-10-14 21:13:38 +03:00
curr = m1.group(1) + '<em>' + m1.group(2) + \
'</em>' + m1.group(3)
i = '<li>' + curr + '</li>'
2017-10-14 21:13:38 +03:00
else:
if in_list:
in_list_append = True
2017-10-14 21:13:38 +03:00
in_list = False
m = re.match('<h|<ul|<p|<li', i)
if not m:
i = '<p>' + i + '</p>'
m = re.match('(.*)__(.*)__(.*)', i)
if m:
i = m.group(1) + '<strong>' + m.group(2) + '</strong>' + m.group(3)
m = re.match('(.*)_(.*)_(.*)', i)
if m:
i = m.group(1) + '<em>' + m.group(2) + '</em>' + m.group(3)
if in_list_append:
i = '</ul>' + i
in_list_append = False
2017-10-14 21:13:38 +03:00
res += i
if in_list:
res += '</ul>'
return res