-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown.py
60 lines (56 loc) · 1.82 KB
/
markdown.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#
# Microlog. Copyright (c) 2023 laffra, dcharbon. All rights reserved.
#
def toHTML(markdownText):
try:
from textwrap import dedent
except:
dedent = lambda text: text
prevIndent = -1
html = []
fenced = False
for lineno, line in enumerate(dedent(markdownText).replace("\\n", "\n").strip().split("\n"), 1):
# line = line.replace("<", "<")
if line.strip() in ["---", "```"]:
if fenced:
html.append("</pre>")
fenced = False
else:
fenced = True
html.append("<pre>")
continue
if fenced:
for n in range(prevIndent):
if line and line[0] == " ":
line = line[1:]
html.append(line)
html.append("<br>")
continue
indent = prevIndent
if line:
indent = 0
while line and line[0] == " ":
indent += 1
line = line[1:]
if indent > prevIndent and prevIndent != -1:
html.append("<ul style='margin-block-start: 0; margin-bottom: 20px'>")
if indent < prevIndent:
html.append("</ul>")
if line == "":
html.append("<br>")
elif len(line) > 3 and line[0:3] == "###":
html.append(f"<h3>{line[3:].strip()}</h3>")
elif len(line) > 2 and line[0:2] == "##":
html.append(f"<h2>{line[2:].strip()}</h2>")
elif line[0] == "#":
html.append(f"<h1>{line[1:].strip()}</h1>")
elif line[0] == "-":
html.append(f"<li>{line[1:]}</li>")
else:
html.append(f"{line}<br>")
prevIndent = indent
html.append("</h1>")
html.append("</h2>")
html.append("</h3>")
html.append("</ul>")
return "".join(html)