diff options
| author | Danilo M. <danix@danix.xyz> | 2026-05-08 10:42:00 +0200 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-05-08 10:42:00 +0200 |
| commit | 179f8e056dcd39091e94f0e6f161d7e8c24af2e8 (patch) | |
| tree | 63fbaaac99db3e30ceae3be8b33de70c6dbc7b34 /about-filter.py | |
| parent | 5ec08055932e5af8fc23e7c4f0a4dbb9d12a1b7f (diff) | |
| download | cgit-theme-danix-179f8e056dcd39091e94f0e6f161d7e8c24af2e8.tar.gz cgit-theme-danix-179f8e056dcd39091e94f0e6f161d7e8c24af2e8.zip | |
feat(about): add README rendering with Pygments syntax highlighting
- Add about-filter.py: renders README.md via python-markdown,
highlights fenced code blocks with Pygments using existing
Catppuccin Macchiato token CSS classes
- Fix about section selector: cgit v1.2.3 emits div#summary,
not div#readme; update all rules accordingly
- Style div#summary: surface bg, purple glow, 1200px max-width
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'about-filter.py')
| -rwxr-xr-x | about-filter.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/about-filter.py b/about-filter.py new file mode 100755 index 0000000..fecbb1c --- /dev/null +++ b/about-filter.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +""" +cgit about-filter: renders README.md as HTML with Pygments syntax highlighting. + +cgit calls this script with the readme filename as argv[1] and file content on stdin. +Output is HTML written to stdout. +""" + +import sys +import re +import markdown +from pygments import highlight +from pygments.lexers import get_lexer_by_name, TextLexer +from pygments.formatters import HtmlFormatter +from pygments.util import ClassNotFound + + +def highlight_code_blocks(html): + def replacer(match): + lang = match.group(1) or "" + code = match.group(2) + # Unescape HTML entities from markdown output + code = code.replace("&", "&").replace("<", "<").replace(">", ">").replace(""", '"') + try: + lexer = get_lexer_by_name(lang.strip(), stripnl=False) if lang.strip() else TextLexer(stripnl=False) + except ClassNotFound: + lexer = TextLexer(stripnl=False) + formatter = HtmlFormatter(nowrap=True, cssclass="highlight", noclasses=False) + highlighted = highlight(code, lexer, formatter) + return f'<pre><code class="highlight">{highlighted}</code></pre>' + + # Match <pre><code class="language-LANG"> or <pre><code> + pattern = re.compile( + r'<pre><code(?:\s+class="language-([^"]*)")?>(.*?)</code></pre>', + re.DOTALL + ) + return pattern.sub(replacer, html) + + +content = sys.stdin.read() +html = markdown.markdown(content, extensions=["fenced_code", "tables", "toc"]) +print(highlight_code_blocks(html)) |
