2017-10-14 21:13:38 +03:00
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
2019-05-30 06:06:45 -07:00
|
|
|
def parse(markdown):
|
2017-10-14 21:13:38 +03:00
|
|
|
lines = markdown.split('\n')
|
|
|
|
|
res = ''
|
|
|
|
|
in_list = False
|
2019-05-28 09:01:13 -04:00
|
|
|
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>'
|
2021-11-15 21:45:43 +03:00
|
|
|
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
|
2018-05-07 08:48:19 -04:00
|
|
|
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:
|
2018-05-07 08:48:19 -04:00
|
|
|
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)
|
2018-05-07 08:48:19 -04:00
|
|
|
i = '<li>' + curr + '</li>'
|
2017-10-14 21:13:38 +03:00
|
|
|
else:
|
|
|
|
|
if in_list:
|
2019-05-28 09:01:13 -04:00
|
|
|
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)
|
2019-05-28 09:01:13 -04:00
|
|
|
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
|