From 7e4584b0365970290d33aa91eff6ef3d87ca43f7 Mon Sep 17 00:00:00 2001 From: backwardspy Date: Sun, 17 Mar 2024 22:12:07 +0000 Subject: [PATCH] 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()