mirror of
https://github.com/HIllya51/LunaTranslator.git
synced 2024-12-29 00:24:13 +08:00
.
This commit is contained in:
parent
696de614ac
commit
57648bd990
@ -103,14 +103,27 @@ class cishubase:
|
|||||||
print(markdown_text)
|
print(markdown_text)
|
||||||
lines = markdown_text.split("\n")
|
lines = markdown_text.split("\n")
|
||||||
html_lines = []
|
html_lines = []
|
||||||
|
lastli = ""
|
||||||
|
lideep = 0
|
||||||
|
|
||||||
|
def switchli():
|
||||||
|
nonlocal lideep
|
||||||
|
while lideep:
|
||||||
|
html_lines.append("</ul>")
|
||||||
|
lideep -= 1
|
||||||
|
lastli = ""
|
||||||
|
|
||||||
for line in lines:
|
for line in lines:
|
||||||
if line.startswith("# "):
|
if not line:
|
||||||
html_lines.append(f"<h1>{line[2:]}</h1>")
|
continue
|
||||||
elif line.startswith("## "):
|
m = re.match(r"#+ ", line)
|
||||||
html_lines.append(f"<h2>{line[3:]}</h2>")
|
if m:
|
||||||
elif line.startswith("### "):
|
switchli()
|
||||||
html_lines.append(f"<h3>{line[4:]}</h3>")
|
html_lines.append(
|
||||||
|
"<h{hi}>{inner}</h{hi}>".format(
|
||||||
|
hi=m.span()[1] - 1, inner=line[m.span()[1] :]
|
||||||
|
)
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
|
|
||||||
def parsex(line):
|
def parsex(line):
|
||||||
@ -118,26 +131,21 @@ class cishubase:
|
|||||||
line = re.sub(r"\*(.*?)\*", r"<em>\1</em>", line)
|
line = re.sub(r"\*(.*?)\*", r"<em>\1</em>", line)
|
||||||
return line
|
return line
|
||||||
|
|
||||||
if line.startswith("- ") or line.startswith("* "):
|
m = re.match(r" *[-\*] ", line)
|
||||||
html_lines.append(f"<li>{parsex(line[2:])}</li>")
|
if m:
|
||||||
|
if lastli != m.group():
|
||||||
|
if len(lastli) < len(m.group()):
|
||||||
|
html_lines.append("<ul>")
|
||||||
|
lideep += 1
|
||||||
else:
|
else:
|
||||||
html_lines.append(f"<p>{parsex(line)}</p>")
|
html_lines.append("</ul>")
|
||||||
final_html = []
|
lideep -= 1
|
||||||
in_list = False
|
lastli = m.group()
|
||||||
for line in html_lines:
|
html_lines.append("<li>{}</li>".format(parsex(line[m.span()[1] :])))
|
||||||
if line.startswith("<li>"):
|
|
||||||
if not in_list:
|
|
||||||
final_html.append("<ul>")
|
|
||||||
in_list = True
|
|
||||||
final_html.append(line)
|
|
||||||
elif in_list:
|
|
||||||
final_html.append("</ul>")
|
|
||||||
in_list = False
|
|
||||||
final_html.append(line)
|
|
||||||
else:
|
else:
|
||||||
final_html.append(line)
|
switchli()
|
||||||
|
html_lines.append("<p>{}</p>".format(parsex(line)))
|
||||||
|
|
||||||
if in_list:
|
switchli()
|
||||||
final_html.append("</ul>")
|
|
||||||
|
|
||||||
return "".join(final_html)
|
return "".join(html_lines)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user