From a0fd5b0e9db38fe45fbc7364f10ff92966bb8914 Mon Sep 17 00:00:00 2001 From: backwardspy Date: Sun, 17 Mar 2024 23:45:39 +0000 Subject: [PATCH] feat(css): add important stylesheet variants --- .github/workflows/gh-pages.yaml | 2 +- .gitignore | 3 +++ poetry.lock | 34 ++++++++++++++++++++++-- pyproject.toml | 7 +++-- scripts/build-gh-pages | 47 ++++++++++++++++++++++++--------- 5 files changed, 74 insertions(+), 19 deletions(-) diff --git a/.github/workflows/gh-pages.yaml b/.github/workflows/gh-pages.yaml index 7caeed0..874cea3 100644 --- a/.github/workflows/gh-pages.yaml +++ b/.github/workflows/gh-pages.yaml @@ -16,7 +16,7 @@ jobs: - uses: "actions/setup-node@v4" with: node-version: "latest" - - run: "pip install catppuccin[pygments]" + - run: "pip install catppuccin[pygments,gh-pages]" - run: "scripts/build-gh-pages" - run: "npx lightningcss-cli --minify ./gh-pages/pygments/*.css --output-dir ./gh-pages/pygments/" - uses: "peaceiris/actions-gh-pages@v3" diff --git a/.gitignore b/.gitignore index bf206de..f5885fb 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,6 @@ __pycache__/ .coverage coverage.xml htmlcov + +# gh-pages build script output +/gh-pages \ No newline at end of file diff --git a/poetry.lock b/poetry.lock index 741c2a5..1e2e7bd 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. [[package]] name = "colorama" @@ -322,6 +322,24 @@ files = [ {file = "ruff-0.2.1.tar.gz", hash = "sha256:3b42b5d8677cd0c72b99fcaf068ffc62abb5a19e71b4a3b9cfa50658a0af02f1"}, ] +[[package]] +name = "tinycss2" +version = "1.2.1" +description = "A tiny CSS parser" +optional = true +python-versions = ">=3.7" +files = [ + {file = "tinycss2-1.2.1-py3-none-any.whl", hash = "sha256:2b80a96d41e7c3914b8cda8bc7f705a4d9c49275616e886103dd839dfc847847"}, + {file = "tinycss2-1.2.1.tar.gz", hash = "sha256:8cff3a8f066c2ec677c06dbc7b45619804a6938478d9d73c284b29d14ecb0627"}, +] + +[package.dependencies] +webencodings = ">=0.4" + +[package.extras] +doc = ["sphinx", "sphinx_rtd_theme"] +test = ["flake8", "isort", "pytest"] + [[package]] name = "tomli" version = "2.0.1" @@ -392,11 +410,23 @@ files = [ {file = "typing_extensions-4.9.0.tar.gz", hash = "sha256:23478f88c37f27d76ac8aee6c905017a143b0b1b886c3c9f66bc2fd94f9f5783"}, ] +[[package]] +name = "webencodings" +version = "0.5.1" +description = "Character encoding aliases for legacy web content" +optional = true +python-versions = "*" +files = [ + {file = "webencodings-0.5.1-py2.py3-none-any.whl", hash = "sha256:a0af1213f3c2226497a97e2b3aa01a7e4bee4f403f95be16fc9acd2947514a78"}, + {file = "webencodings-0.5.1.tar.gz", hash = "sha256:b36a1c245f2d304965eb4e0a82848379241dc04b865afcc4aab16748587e1923"}, +] + [extras] +gh-pages = ["tinycss2"] pygments = ["pygments"] rich = ["rich"] [metadata] lock-version = "2.0" python-versions = "^3.8.0" -content-hash = "e0dd8e64b7b617aaf05ceee439af64da8d359b58a3efd3fb5d79ec4eef35277a" +content-hash = "cbfcbf5d9b3234e64793cd06a4f9a8198d960cb70c2f47a08125a54de925f48d" diff --git a/pyproject.toml b/pyproject.toml index 3a88288..14a89c4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,10 +19,12 @@ catppuccin-mocha = "catppuccin.extras.pygments:MochaStyle" python = "^3.8.0" pygments = { version = "^2.17.2", optional = true } rich = { version = "^13.7.0", optional = true } +tinycss2 = { version = "^1.2.1", optional = true } [tool.poetry.extras] pygments = ["pygments"] rich = ["rich"] +gh-pages = ["tinycss2"] [tool.poetry.group.dev.dependencies] mypy = "^1.8.0" @@ -58,7 +60,4 @@ ignore = [ strict = true [tool.coverage.report] -exclude_lines = [ - "pragma: no cover", - "if TYPE_CHECKING:", -] +exclude_lines = ["pragma: no cover", "if TYPE_CHECKING:"] diff --git a/scripts/build-gh-pages b/scripts/build-gh-pages index 6089596..3d072c4 100755 --- a/scripts/build-gh-pages +++ b/scripts/build-gh-pages @@ -1,7 +1,7 @@ #!/usr/bin/env python -import re from pathlib import Path +import tinycss2 from pygments.formatters.html import HtmlFormatter from catppuccin import PALETTE @@ -23,16 +23,32 @@ PYGMENTS_STYLES = { } -PADDING_PAT = re.compile(r" padding-(?:left|right): \d+px;") - - def write(content: str, path: Path) -> None: path.parent.mkdir(parents=True, exist_ok=True) path.write_text(content) -def postprocess_css(content: str) -> str: - return PADDING_PAT.sub("", content) +def postprocess_css(content: str, important: bool) -> str: + rules = tinycss2.parse_stylesheet(content, skip_comments=True, skip_whitespace=True) + for rule in rules: + declarations = tinycss2.parse_declaration_list( + rule.content, skip_comments=True, skip_whitespace=True + ) + + # remove padding + declarations = [ + declaration + for declaration in declarations + if "padding" not in declaration.lower_name + ] + + # add !important if needed + for declaration in declarations: + declaration.important = important + + rule.content = declarations + + return tinycss2.serialize(rules) def flavor_css(flavor: str) -> str: @@ -49,17 +65,24 @@ def variable_css() -> str: return css -def build_css() -> None: +def build_css(*, important: bool) -> None: # build individual CSS files for each flavor for flavor in PALETTE: - filename = f"catppuccin-{flavor.identifier}.css" + if important: + filename = f"catppuccin-{flavor.identifier}.important.css" + else: + filename = f"catppuccin-{flavor.identifier}.css" path = PYGMENTS_DIR / filename - write(postprocess_css(flavor_css(flavor.identifier)), path) + write(postprocess_css(flavor_css(flavor.identifier), important), path) # build a variable CSS file - path = PYGMENTS_DIR / "catppuccin-variables.css" - write(postprocess_css(variable_css()), path) + if important: + path = PYGMENTS_DIR / "catppuccin-variables.important.css" + else: + path = PYGMENTS_DIR / "catppuccin-variables.css" + write(postprocess_css(variable_css(), important), path) if __name__ == "__main__": - build_css() + build_css(important=False) + build_css(important=True)