This commit is contained in:
恍兮惚兮 2024-05-31 21:11:28 +08:00
parent 49f04da800
commit 5f950ec20d
4 changed files with 83 additions and 24 deletions

View File

@ -2550,32 +2550,91 @@ class mdict(cishubase):
def tryparsecss(self, html_content, url, file_content, divid): def tryparsecss(self, html_content, url, file_content, divid):
try: try:
import sass file_content = file_content.decode("utf8")
except:
return html_content
try:
from tinycss2 import parse_stylesheet, serialize
from tinycss2.ast import (
HashToken,
WhitespaceToken,
AtRule,
QualifiedRule,
ParseError,
)
css = file_content.decode("utf8") rules = parse_stylesheet(file_content, True, True)
css = sass.compile(string=(f"#{divid} {{ {css} }}"))
csss = css.split("\n") def parseaqr(rule: QualifiedRule):
i = 0 start = True
while i < len(csss): idx = 0
css = csss[i] skip = False
if css.endswith(" body {"): for token in rule.prelude.copy():
csss[i] = css[: -len(" body {")] + "{" if skip and token.type == "whitespace":
elif css == "@font-face {": skip = False
csss[i + 1] = "" idx += 1
for j in range(i + 2, len(csss)): continue
if csss[j].endswith("} }"): if start:
i = j if token.type == "ident" and token.value == "body":
csss[j] = csss[j][:-1] # body
break rule.prelude.insert(idx + 1, HashToken(0, 0, divid, True))
i += 1 rule.prelude.insert(idx + 1, WhitespaceToken(0, 0, " "))
css = "\n".join(csss) idx += 2
file_content = css.encode("utf8") else:
# print(css) # .id tag
# tag
# #class tag
rule.prelude.insert(idx, WhitespaceToken(0, 0, " "))
rule.prelude.insert(idx, HashToken(0, 0, divid, True))
idx += 2
start = False
elif token.type == "literal" and token.value == ",":
# 有多个限定符
start = True
skip = True
idx += 1
class shitrule(AtRule):
def __init__(self, __):
super().__init__(0, 0, " ", " ", [], [])
self.__ = __
def _serialize_to(self, _):
return _(self.__)
def parserules(rules):
# print(stylesheet)
for i, rule in enumerate(rules.copy()):
if isinstance(rule, AtRule):
if not rule.content:
# @charset "UTF-8";
continue
internal = "".join([_.serialize() for _ in rule.content])
internal = parse_stylesheet(internal, True, True)
if len(internal) and isinstance(internal[0], ParseError):
# @font-face
continue
# @....{ .klas{} }
internal = parserules(internal)
ser = "@"
ser += rule.at_keyword
ser += "{"
for _ in internal:
ser += _.serialize()
ser += "}"
# rule.serialize=functools.partial(__,ser)
rules[i] = shitrule(ser)
elif isinstance(rule, QualifiedRule):
parseaqr(rules[i])
return rules
file_content = serialize(parserules(rules))
# print(file_content)
except: except:
from traceback import print_exc from traceback import print_exc
print_exc() print_exc()
base64_content = base64.b64encode(file_content).decode("utf-8") base64_content = base64.b64encode(file_content.encode("utf8")).decode("utf-8")
html_content = html_content.replace( html_content = html_content.replace(
url, f"data:text/css;base64,{base64_content}" url, f"data:text/css;base64,{base64_content}"
) )

View File

@ -10,4 +10,4 @@ from importlib import resources
from distutils.version import StrictVersion from distutils.version import StrictVersion
from dataclasses import dataclass from dataclasses import dataclass
import colorsys import colorsys
import sass import tinycss2

View File

@ -4,4 +4,4 @@ PyQt5==5.15.10
PyQt5-Qt5==5.15.2 PyQt5-Qt5==5.15.2
webviewpy==1.2.0 webviewpy==1.2.0
pefile pefile
libsass tinycss2==1.3.0

View File

@ -4,4 +4,4 @@ PyQt6==6.7.0
PyQt6-Qt6==6.7.0 PyQt6-Qt6==6.7.0
webviewpy==1.2.0 webviewpy==1.2.0
pefile pefile
libsass tinycss2==1.3.0