From 6f497fa489ffb4e5faff8f6b72d48b1141d065c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Thu, 30 May 2024 14:24:31 +0800 Subject: [PATCH] css --- LunaTranslator/LunaTranslator/cishu/mdict.py | 38 ++++++++++++++++---- LunaTranslator/LunaTranslator/keeprefs.py | 3 +- LunaTranslator/files/anki/back.html | 4 +-- LunaTranslator/files/anki/style.css | 3 -- LunaTranslator/requirements.txt | 4 ++- LunaTranslator/requirements_qt6.txt | 4 ++- 6 files changed, 41 insertions(+), 15 deletions(-) diff --git a/LunaTranslator/LunaTranslator/cishu/mdict.py b/LunaTranslator/LunaTranslator/cishu/mdict.py index 56ebc026..6e85e9b5 100644 --- a/LunaTranslator/LunaTranslator/cishu/mdict.py +++ b/LunaTranslator/LunaTranslator/cishu/mdict.py @@ -2483,14 +2483,14 @@ class mdict(cishubase): return "application/octet-stream" def repairtarget(self, index, base, html_content): - import base64 + import base64, uuid src_pattern = r'src="([^"]+)"' href_pattern = r'href="([^"]+)"' src_matches = re.findall(src_pattern, html_content) href_matches = re.findall(href_pattern, html_content) - + divid = "luna_internal_" + str(uuid.uuid4()) for url in src_matches + href_matches: oked = False iscss = url.lower().endswith(".css") @@ -2537,6 +2537,34 @@ class mdict(cishubase): except: file_content = None if file_content: + if iscss: + try: + import sass + + css = file_content.decode("utf8") + css = sass.compile(string=(f"#{divid} {{ {css} }}")) + csss = css.split("\n") + i = 0 + while i < len(csss): + css = csss[i] + if css.endswith(" body {"): + csss[i] = css[: -len(" body {")] + "{" + elif css == "@font-face {": + csss[i + 1] = "" + for j in range(i + 2, len(csss)): + if csss[j].endswith("} }"): + i = j + csss[j] = csss[j][:-1] + break + i += 1 + css = "\n".join(csss) + file_content = css.encode("utf8") + # print(css) + except: + from traceback import print_exc + + print_exc() + base64_content = base64.b64encode(file_content).decode("utf-8") if iscss: html_content = html_content.replace( @@ -2548,7 +2576,7 @@ class mdict(cishubase): ) elif not oked: print(url) - return html_content + return f'