Skip to content

Commit a0fd5b0

Browse files
committed
feat(css): add important stylesheet variants
1 parent a8eb6b9 commit a0fd5b0

File tree

5 files changed

+74
-19
lines changed

5 files changed

+74
-19
lines changed

.github/workflows/gh-pages.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- uses: "actions/setup-node@v4"
1717
with:
1818
node-version: "latest"
19-
- run: "pip install catppuccin[pygments]"
19+
- run: "pip install catppuccin[pygments,gh-pages]"
2020
- run: "scripts/build-gh-pages"
2121
- run: "npx lightningcss-cli --minify ./gh-pages/pygments/*.css --output-dir ./gh-pages/pygments/"
2222
- uses: "peaceiris/actions-gh-pages@v3"

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ __pycache__/
1515
.coverage
1616
coverage.xml
1717
htmlcov
18+
19+
# gh-pages build script output
20+
/gh-pages

poetry.lock

Lines changed: 32 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ catppuccin-mocha = "catppuccin.extras.pygments:MochaStyle"
1919
python = "^3.8.0"
2020
pygments = { version = "^2.17.2", optional = true }
2121
rich = { version = "^13.7.0", optional = true }
22+
tinycss2 = { version = "^1.2.1", optional = true }
2223

2324
[tool.poetry.extras]
2425
pygments = ["pygments"]
2526
rich = ["rich"]
27+
gh-pages = ["tinycss2"]
2628

2729
[tool.poetry.group.dev.dependencies]
2830
mypy = "^1.8.0"
@@ -58,7 +60,4 @@ ignore = [
5860
strict = true
5961

6062
[tool.coverage.report]
61-
exclude_lines = [
62-
"pragma: no cover",
63-
"if TYPE_CHECKING:",
64-
]
63+
exclude_lines = ["pragma: no cover", "if TYPE_CHECKING:"]

scripts/build-gh-pages

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
2-
import re
32
from pathlib import Path
43

4+
import tinycss2
55
from pygments.formatters.html import HtmlFormatter
66

77
from catppuccin import PALETTE
@@ -23,16 +23,32 @@ PYGMENTS_STYLES = {
2323
}
2424

2525

26-
PADDING_PAT = re.compile(r" padding-(?:left|right): \d+px;")
27-
28-
2926
def write(content: str, path: Path) -> None:
3027
path.parent.mkdir(parents=True, exist_ok=True)
3128
path.write_text(content)
3229

3330

34-
def postprocess_css(content: str) -> str:
35-
return PADDING_PAT.sub("", content)
31+
def postprocess_css(content: str, important: bool) -> str:
32+
rules = tinycss2.parse_stylesheet(content, skip_comments=True, skip_whitespace=True)
33+
for rule in rules:
34+
declarations = tinycss2.parse_declaration_list(
35+
rule.content, skip_comments=True, skip_whitespace=True
36+
)
37+
38+
# remove padding
39+
declarations = [
40+
declaration
41+
for declaration in declarations
42+
if "padding" not in declaration.lower_name
43+
]
44+
45+
# add !important if needed
46+
for declaration in declarations:
47+
declaration.important = important
48+
49+
rule.content = declarations
50+
51+
return tinycss2.serialize(rules)
3652

3753

3854
def flavor_css(flavor: str) -> str:
@@ -49,17 +65,24 @@ def variable_css() -> str:
4965
return css
5066

5167

52-
def build_css() -> None:
68+
def build_css(*, important: bool) -> None:
5369
# build individual CSS files for each flavor
5470
for flavor in PALETTE:
55-
filename = f"catppuccin-{flavor.identifier}.css"
71+
if important:
72+
filename = f"catppuccin-{flavor.identifier}.important.css"
73+
else:
74+
filename = f"catppuccin-{flavor.identifier}.css"
5675
path = PYGMENTS_DIR / filename
57-
write(postprocess_css(flavor_css(flavor.identifier)), path)
76+
write(postprocess_css(flavor_css(flavor.identifier), important), path)
5877

5978
# build a variable CSS file
60-
path = PYGMENTS_DIR / "catppuccin-variables.css"
61-
write(postprocess_css(variable_css()), path)
79+
if important:
80+
path = PYGMENTS_DIR / "catppuccin-variables.important.css"
81+
else:
82+
path = PYGMENTS_DIR / "catppuccin-variables.css"
83+
write(postprocess_css(variable_css(), important), path)
6284

6385

6486
if __name__ == "__main__":
65-
build_css()
87+
build_css(important=False)
88+
build_css(important=True)

0 commit comments

Comments
 (0)