feat: add gh-pages build script for pygments css (#33)

This commit is contained in:
backwardspy 2024-03-17 22:12:07 +00:00 committed by GitHub
parent 465e3269e9
commit 7e4584b036
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 107 additions and 0 deletions

29
.github/workflows/gh-pages.yaml vendored Normal file
View File

@ -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]"

View File

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

65
scripts/build-gh-pages Executable file
View File

@ -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()