From 7e4584b0365970290d33aa91eff6ef3d87ca43f7 Mon Sep 17 00:00:00 2001 From: backwardspy Date: Sun, 17 Mar 2024 22:12:07 +0000 Subject: [PATCH 1/7] feat: add gh-pages build script for pygments css (#33) --- .github/workflows/gh-pages.yaml | 29 +++++++++++++++ catppuccin/extras/pygments.py | 13 +++++++ scripts/build-gh-pages | 65 +++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 .github/workflows/gh-pages.yaml create mode 100755 scripts/build-gh-pages diff --git a/.github/workflows/gh-pages.yaml b/.github/workflows/gh-pages.yaml new file mode 100644 index 0000000..3c244a9 --- /dev/null +++ b/.github/workflows/gh-pages.yaml @@ -0,0 +1,29 @@ +on: + workflow_dispatch: + push: + branches: + - "main" + +jobs: + build: + runs-on: "ubuntu-latest" + + steps: + - uses: "actions/checkout@v4" + - uses: "actions/setup-python@v5" + with: + python-version: "3.x" + - uses: "actions/setup-node@v4" + with: + node-version: "latest" + - run: "pip install catppuccin[pygments]" + - run: "scripts/build-gh-pages" + - run: "npx lightningcss-cli ./gh-pages/pygments/*.css --output-dir ./gh-pages/pygments/" + - uses: "peaceiris/actions-gh-pages@v3" + with: + enable_jekyll: false + github_token: "${{ secrets.GITHUB_TOKEN }}" + publish_branch: "gh-pages" + publish_dir: "./gh-pages" + user_email: "github-actions[bot]@users.noreply.github.com" + user_name: "github-actions[bot]" \ No newline at end of file diff --git a/catppuccin/extras/pygments.py b/catppuccin/extras/pygments.py index d49bfda..c342d5b 100644 --- a/catppuccin/extras/pygments.py +++ b/catppuccin/extras/pygments.py @@ -1,5 +1,6 @@ # ruff: noqa: ERA001 """Pygments styles for all Catppuccin flavors.""" + from __future__ import annotations from typing import TYPE_CHECKING @@ -121,8 +122,11 @@ class LatteStyle(Style): _colors = PALETTE.latte.colors background_color = _colors.base.hex + highlight_color = _colors.surface0.hex line_number_background_color = _colors.mantle.hex line_number_color = _colors.text.hex + line_number_special_background_color = _colors.mantle.hex + line_number_special_color = _colors.text.hex styles = _make_styles(_colors) @@ -133,8 +137,11 @@ class FrappeStyle(Style): _colors = PALETTE.frappe.colors background_color = _colors.base.hex + highlight_color = _colors.surface0.hex line_number_background_color = _colors.mantle.hex line_number_color = _colors.text.hex + line_number_special_background_color = _colors.mantle.hex + line_number_special_color = _colors.text.hex styles = _make_styles(_colors) @@ -145,8 +152,11 @@ class MacchiatoStyle(Style): _colors = PALETTE.macchiato.colors background_color = _colors.base.hex + highlight_color = _colors.surface0.hex line_number_background_color = _colors.mantle.hex line_number_color = _colors.text.hex + line_number_special_background_color = _colors.mantle.hex + line_number_special_color = _colors.text.hex styles = _make_styles(_colors) @@ -157,7 +167,10 @@ class MochaStyle(Style): _colors = PALETTE.mocha.colors background_color = _colors.base.hex + highlight_color = _colors.surface0.hex line_number_background_color = _colors.mantle.hex line_number_color = _colors.text.hex + line_number_special_background_color = _colors.mantle.hex + line_number_special_color = _colors.text.hex styles = _make_styles(_colors) diff --git a/scripts/build-gh-pages b/scripts/build-gh-pages new file mode 100755 index 0000000..81912ad --- /dev/null +++ b/scripts/build-gh-pages @@ -0,0 +1,65 @@ +#!/usr/bin/env python +import re +from pathlib import Path + +from pygments.formatters.html import HtmlFormatter + +from catppuccin import PALETTE +from catppuccin.extras.pygments import ( + FrappeStyle, + LatteStyle, + MacchiatoStyle, + MochaStyle, +) + +OUT_DIR = Path("gh-pages") +PYGMENTS_DIR = OUT_DIR / "pygments" + +PYGMENTS_STYLES = { + PALETTE.latte.identifier: LatteStyle, + PALETTE.frappe.identifier: FrappeStyle, + PALETTE.macchiato.identifier: MacchiatoStyle, + PALETTE.mocha.identifier: MochaStyle, +} + + +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 flavor_css(flavor: str) -> str: + style = PYGMENTS_STYLES[flavor] + formatter = HtmlFormatter(style=style) + return formatter.get_style_defs() + + +def variable_css() -> str: + flavor = PALETTE.latte + css = flavor_css(flavor.identifier) + for color in flavor.colors: + css = css.replace(color.hex, f"var(--ctp-{color.identifier})") + return css + + +def build_css() -> None: + # build individual CSS files for each flavor + for flavor in PALETTE: + filename = f"ctp-{flavor.identifier}.css" + path = PYGMENTS_DIR / filename + write(postprocess_css(flavor_css(flavor.identifier)), path) + + # build a variable CSS file + path = PYGMENTS_DIR / "ctp-variable.css" + write(postprocess_css(variable_css()), path) + + +if __name__ == "__main__": + build_css() From d415e110a04cb8485bddeff5fcb68095788c4511 Mon Sep 17 00:00:00 2001 From: backwardspy Date: Sun, 17 Mar 2024 22:18:51 +0000 Subject: [PATCH 2/7] ci: actually minify css, add cname (#34) --- .github/workflows/gh-pages.yaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/gh-pages.yaml b/.github/workflows/gh-pages.yaml index 3c244a9..7caeed0 100644 --- a/.github/workflows/gh-pages.yaml +++ b/.github/workflows/gh-pages.yaml @@ -18,7 +18,7 @@ jobs: node-version: "latest" - run: "pip install catppuccin[pygments]" - run: "scripts/build-gh-pages" - - run: "npx lightningcss-cli ./gh-pages/pygments/*.css --output-dir ./gh-pages/pygments/" + - run: "npx lightningcss-cli --minify ./gh-pages/pygments/*.css --output-dir ./gh-pages/pygments/" - uses: "peaceiris/actions-gh-pages@v3" with: enable_jekyll: false @@ -26,4 +26,5 @@ jobs: publish_branch: "gh-pages" publish_dir: "./gh-pages" user_email: "github-actions[bot]@users.noreply.github.com" - user_name: "github-actions[bot]" \ No newline at end of file + user_name: "github-actions[bot]" + cname: "python.catppuccin.com" From cfb2694be1a885180744fd3f99e1dba79935d4dc Mon Sep 17 00:00:00 2001 From: backwardspy Date: Sun, 17 Mar 2024 22:26:51 +0000 Subject: [PATCH 3/7] feat: change prefix on pygments css file names --- scripts/build-gh-pages | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/build-gh-pages b/scripts/build-gh-pages index 81912ad..292a978 100755 --- a/scripts/build-gh-pages +++ b/scripts/build-gh-pages @@ -52,12 +52,12 @@ def variable_css() -> str: def build_css() -> None: # build individual CSS files for each flavor for flavor in PALETTE: - filename = f"ctp-{flavor.identifier}.css" + filename = f"catppuccin-{flavor.identifier}.css" path = PYGMENTS_DIR / filename write(postprocess_css(flavor_css(flavor.identifier)), path) # build a variable CSS file - path = PYGMENTS_DIR / "ctp-variable.css" + path = PYGMENTS_DIR / "catppuccin.variables.css" write(postprocess_css(variable_css()), path) From ef18bd706b053f02b02067f28de756427efd4a3a Mon Sep 17 00:00:00 2001 From: backwardspy Date: Sun, 17 Mar 2024 22:29:09 +0000 Subject: [PATCH 4/7] =?UTF-8?q?fix:=20`.`=20=E2=86=92=20`-`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/build-gh-pages | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/build-gh-pages b/scripts/build-gh-pages index 292a978..0604520 100755 --- a/scripts/build-gh-pages +++ b/scripts/build-gh-pages @@ -57,7 +57,7 @@ def build_css() -> None: write(postprocess_css(flavor_css(flavor.identifier)), path) # build a variable CSS file - path = PYGMENTS_DIR / "catppuccin.variables.css" + path = PYGMENTS_DIR / "catppuccin-variables.css" write(postprocess_css(variable_css()), path) From 6c746c1ca7e3aa56ec970f13e76b7381c32165db Mon Sep 17 00:00:00 2001 From: backwardspy Date: Sun, 17 Mar 2024 22:47:01 +0000 Subject: [PATCH 5/7] feat(css): scope token classes to `pre` elements --- scripts/build-gh-pages | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/build-gh-pages b/scripts/build-gh-pages index 0604520..afce559 100755 --- a/scripts/build-gh-pages +++ b/scripts/build-gh-pages @@ -38,7 +38,7 @@ def postprocess_css(content: str) -> str: def flavor_css(flavor: str) -> str: style = PYGMENTS_STYLES[flavor] formatter = HtmlFormatter(style=style) - return formatter.get_style_defs() + return formatter.get_style_defs("pre") def variable_css() -> str: From a8eb6b994bbbfde1d0839599400601c64b5f17eb Mon Sep 17 00:00:00 2001 From: backwardspy Date: Sun, 17 Mar 2024 22:54:41 +0000 Subject: [PATCH 6/7] feat(css): target `pre[lang]` in addition to `pre` --- scripts/build-gh-pages | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/build-gh-pages b/scripts/build-gh-pages index afce559..6089596 100755 --- a/scripts/build-gh-pages +++ b/scripts/build-gh-pages @@ -38,7 +38,7 @@ def postprocess_css(content: str) -> str: def flavor_css(flavor: str) -> str: style = PYGMENTS_STYLES[flavor] formatter = HtmlFormatter(style=style) - return formatter.get_style_defs("pre") + return formatter.get_style_defs(["pre", "pre[lang]"]) def variable_css() -> str: From a0fd5b0e9db38fe45fbc7364f10ff92966bb8914 Mon Sep 17 00:00:00 2001 From: backwardspy Date: Sun, 17 Mar 2024 23:45:39 +0000 Subject: [PATCH 7/7] 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)