mirror of
https://github.com/GradienceTeam/Gradience.git
synced 2024-06-02 21:26:57 +00:00
feat: add css parser
This commit is contained in:
parent
b64c726c94
commit
c47694bd6f
|
@ -63,6 +63,7 @@ gradience_modules = [
|
|||
'modules/flatpak_overrides.py',
|
||||
'modules/utils.py',
|
||||
'modules/run_async.py',
|
||||
'modules/css.py',
|
||||
]
|
||||
|
||||
PY_INSTALLDIR.install_sources(gradience_modules, subdir: 'gradience/modules')
|
||||
|
|
43
src/modules/css.py
Normal file
43
src/modules/css.py
Normal file
|
@ -0,0 +1,43 @@
|
|||
import cssutils
|
||||
|
||||
COLORS = [
|
||||
"blue_",
|
||||
"green_",
|
||||
"yellow_",
|
||||
"orange_",
|
||||
"red_",
|
||||
"purple_",
|
||||
"brown_",
|
||||
"light_",
|
||||
"dark_",
|
||||
]
|
||||
|
||||
|
||||
def load_preset_from_file(path):
|
||||
css = ""
|
||||
variables = {}
|
||||
palette = {}
|
||||
|
||||
for color in COLORS:
|
||||
palette[color] = {}
|
||||
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
sheet = cssutils.parseString(f.read())
|
||||
for rule in sheet:
|
||||
css_text = rule.cssText
|
||||
if rule.type == rule.UNKNOWN_RULE:
|
||||
if css_text.startswith("@define-color"):
|
||||
name, color = css_text.split(" ", 1)[1].split(" ", 1)
|
||||
for color_name in COLORS:
|
||||
if name.startswith(color_name):
|
||||
palette[name[:-1]][name[-1:]] = color
|
||||
break
|
||||
else:
|
||||
variables[name] = color
|
||||
print(f"{name} = {color}")
|
||||
elif rule.type == rule.STYLE_RULE:
|
||||
css += f"\n{rule.cssText}"
|
||||
return css, variables, palette
|
||||
|
||||
# if __name__ == "__main__":
|
||||
# load_preset_from_file("/home/user/.config/gtk-4.0/gtk.css")
|
Loading…
Reference in a new issue