Skip to content

Commit 7e4584b

Browse files
authored
feat: add gh-pages build script for pygments css (#33)
1 parent 465e326 commit 7e4584b

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

.github/workflows/gh-pages.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
on:
2+
workflow_dispatch:
3+
push:
4+
branches:
5+
- "main"
6+
7+
jobs:
8+
build:
9+
runs-on: "ubuntu-latest"
10+
11+
steps:
12+
- uses: "actions/checkout@v4"
13+
- uses: "actions/setup-python@v5"
14+
with:
15+
python-version: "3.x"
16+
- uses: "actions/setup-node@v4"
17+
with:
18+
node-version: "latest"
19+
- run: "pip install catppuccin[pygments]"
20+
- run: "scripts/build-gh-pages"
21+
- run: "npx lightningcss-cli ./gh-pages/pygments/*.css --output-dir ./gh-pages/pygments/"
22+
- uses: "peaceiris/actions-gh-pages@v3"
23+
with:
24+
enable_jekyll: false
25+
github_token: "${{ secrets.GITHUB_TOKEN }}"
26+
publish_branch: "gh-pages"
27+
publish_dir: "./gh-pages"
28+
user_email: "github-actions[bot]@users.noreply.github.com"
29+
user_name: "github-actions[bot]"

catppuccin/extras/pygments.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# ruff: noqa: ERA001
22
"""Pygments styles for all Catppuccin flavors."""
3+
34
from __future__ import annotations
45

56
from typing import TYPE_CHECKING
@@ -121,8 +122,11 @@ class LatteStyle(Style):
121122
_colors = PALETTE.latte.colors
122123

123124
background_color = _colors.base.hex
125+
highlight_color = _colors.surface0.hex
124126
line_number_background_color = _colors.mantle.hex
125127
line_number_color = _colors.text.hex
128+
line_number_special_background_color = _colors.mantle.hex
129+
line_number_special_color = _colors.text.hex
126130

127131
styles = _make_styles(_colors)
128132

@@ -133,8 +137,11 @@ class FrappeStyle(Style):
133137
_colors = PALETTE.frappe.colors
134138

135139
background_color = _colors.base.hex
140+
highlight_color = _colors.surface0.hex
136141
line_number_background_color = _colors.mantle.hex
137142
line_number_color = _colors.text.hex
143+
line_number_special_background_color = _colors.mantle.hex
144+
line_number_special_color = _colors.text.hex
138145

139146
styles = _make_styles(_colors)
140147

@@ -145,8 +152,11 @@ class MacchiatoStyle(Style):
145152
_colors = PALETTE.macchiato.colors
146153

147154
background_color = _colors.base.hex
155+
highlight_color = _colors.surface0.hex
148156
line_number_background_color = _colors.mantle.hex
149157
line_number_color = _colors.text.hex
158+
line_number_special_background_color = _colors.mantle.hex
159+
line_number_special_color = _colors.text.hex
150160

151161
styles = _make_styles(_colors)
152162

@@ -157,7 +167,10 @@ class MochaStyle(Style):
157167
_colors = PALETTE.mocha.colors
158168

159169
background_color = _colors.base.hex
170+
highlight_color = _colors.surface0.hex
160171
line_number_background_color = _colors.mantle.hex
161172
line_number_color = _colors.text.hex
173+
line_number_special_background_color = _colors.mantle.hex
174+
line_number_special_color = _colors.text.hex
162175

163176
styles = _make_styles(_colors)

scripts/build-gh-pages

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python
2+
import re
3+
from pathlib import Path
4+
5+
from pygments.formatters.html import HtmlFormatter
6+
7+
from catppuccin import PALETTE
8+
from catppuccin.extras.pygments import (
9+
FrappeStyle,
10+
LatteStyle,
11+
MacchiatoStyle,
12+
MochaStyle,
13+
)
14+
15+
OUT_DIR = Path("gh-pages")
16+
PYGMENTS_DIR = OUT_DIR / "pygments"
17+
18+
PYGMENTS_STYLES = {
19+
PALETTE.latte.identifier: LatteStyle,
20+
PALETTE.frappe.identifier: FrappeStyle,
21+
PALETTE.macchiato.identifier: MacchiatoStyle,
22+
PALETTE.mocha.identifier: MochaStyle,
23+
}
24+
25+
26+
PADDING_PAT = re.compile(r" padding-(?:left|right): \d+px;")
27+
28+
29+
def write(content: str, path: Path) -> None:
30+
path.parent.mkdir(parents=True, exist_ok=True)
31+
path.write_text(content)
32+
33+
34+
def postprocess_css(content: str) -> str:
35+
return PADDING_PAT.sub("", content)
36+
37+
38+
def flavor_css(flavor: str) -> str:
39+
style = PYGMENTS_STYLES[flavor]
40+
formatter = HtmlFormatter(style=style)
41+
return formatter.get_style_defs()
42+
43+
44+
def variable_css() -> str:
45+
flavor = PALETTE.latte
46+
css = flavor_css(flavor.identifier)
47+
for color in flavor.colors:
48+
css = css.replace(color.hex, f"var(--ctp-{color.identifier})")
49+
return css
50+
51+
52+
def build_css() -> None:
53+
# build individual CSS files for each flavor
54+
for flavor in PALETTE:
55+
filename = f"ctp-{flavor.identifier}.css"
56+
path = PYGMENTS_DIR / filename
57+
write(postprocess_css(flavor_css(flavor.identifier)), path)
58+
59+
# build a variable CSS file
60+
path = PYGMENTS_DIR / "ctp-variable.css"
61+
write(postprocess_css(variable_css()), path)
62+
63+
64+
if __name__ == "__main__":
65+
build_css()

0 commit comments

Comments
 (0)