Python: markdown, iteration 2
This commit is contained in:
parent
a0535502eb
commit
bae42c9a49
1 changed files with 15 additions and 20 deletions
|
|
@ -2,34 +2,29 @@ import re
|
|||
|
||||
|
||||
def parse(markdown):
|
||||
res = ''
|
||||
in_list = False
|
||||
result = ''
|
||||
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)
|
||||
match = re.match(r'^(#{1,6}) (.*)', line)
|
||||
if match:
|
||||
if not in_list:
|
||||
in_list = True
|
||||
res += '<ul>'
|
||||
res += '<li>' + match.group(1) + '</li>'
|
||||
level = len(match.group(1))
|
||||
result += f'<h{level}>{match.group(2)}</h{level}>'
|
||||
continue
|
||||
|
||||
if in_list:
|
||||
in_list = False
|
||||
res += '</ul>'
|
||||
match = re.match(r'\* (.*)', line)
|
||||
if match:
|
||||
|
||||
res += '<p>' + line + '</p>'
|
||||
if result.endswith('</ul>'):
|
||||
result = result[:-5]
|
||||
else:
|
||||
result += '<ul>'
|
||||
|
||||
if in_list:
|
||||
res += '</ul>'
|
||||
result += '<li>' + match.group(1) + '</li></ul>'
|
||||
continue
|
||||
|
||||
return res
|
||||
result += '<p>' + line + '</p>'
|
||||
|
||||
return result
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue