Python: markdown
This commit is contained in:
parent
399d240948
commit
a0535502eb
4 changed files with 178 additions and 0 deletions
35
python/markdown/markdown.py
Normal file
35
python/markdown/markdown.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue