Skip to content

Commit

Permalink
feat: add gh-pages build script for pygments css
Browse files Browse the repository at this point in the history
  • Loading branch information
backwardspy committed Mar 17, 2024
1 parent 465e326 commit 6704a3d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/gh-pages.yaml
Original file line number Diff line number Diff line change
@@ -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]"
13 changes: 13 additions & 0 deletions catppuccin/extras/pygments.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# ruff: noqa: ERA001
"""Pygments styles for all Catppuccin flavors."""

from __future__ import annotations

from typing import TYPE_CHECKING
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)
65 changes: 65 additions & 0 deletions scripts/build-gh-pages
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 6704a3d

Please sign in to comment.