From 57648bd990766e3cfb562527138e521442d38959 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <1173718158@qq.com> Date: Sat, 30 Nov 2024 07:10:29 +0800 Subject: [PATCH] . --- py/LunaTranslator/cishu/cishubase.py | 60 ++++++++++++++++------------ 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/py/LunaTranslator/cishu/cishubase.py b/py/LunaTranslator/cishu/cishubase.py index e9e0ed74..70ad9380 100644 --- a/py/LunaTranslator/cishu/cishubase.py +++ b/py/LunaTranslator/cishu/cishubase.py @@ -103,14 +103,27 @@ class cishubase: print(markdown_text) lines = markdown_text.split("\n") html_lines = [] + lastli = "" + lideep = 0 + + def switchli(): + nonlocal lideep + while lideep: + html_lines.append("") + lideep -= 1 + lastli = "" for line in lines: - if line.startswith("# "): - html_lines.append(f"

{line[2:]}

") - elif line.startswith("## "): - html_lines.append(f"

{line[3:]}

") - elif line.startswith("### "): - html_lines.append(f"

{line[4:]}

") + if not line: + continue + m = re.match(r"#+ ", line) + if m: + switchli() + html_lines.append( + "{inner}".format( + hi=m.span()[1] - 1, inner=line[m.span()[1] :] + ) + ) else: def parsex(line): @@ -118,26 +131,21 @@ class cishubase: line = re.sub(r"\*(.*?)\*", r"\1", line) return line - if line.startswith("- ") or line.startswith("* "): - html_lines.append(f"
  • {parsex(line[2:])}
  • ") + m = re.match(r" *[-\*] ", line) + if m: + if lastli != m.group(): + if len(lastli) < len(m.group()): + html_lines.append("") + lideep -= 1 + lastli = m.group() + html_lines.append("
  • {}
  • ".format(parsex(line[m.span()[1] :]))) else: - html_lines.append(f"

    {parsex(line)}

    ") - final_html = [] - in_list = False - for line in html_lines: - if line.startswith("
  • "): - if not in_list: - final_html.append("") - in_list = False - final_html.append(line) - else: - final_html.append(line) + switchli() + html_lines.append("

    {}

    ".format(parsex(line))) - if in_list: - final_html.append("") + switchli() - return "".join(final_html) + return "".join(html_lines)