summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpablu <me@pablu.de>2026-09-19 14:30:18 +0200
committerpablu <me@pablu.de>2026-09-19 14:46:44 +0200
commita3b5366573d9d6ba2299cd021842ab516856327c (patch)
treef7eb7d2d45f660d0b3449ee4ad4848dda67880e0
parentbea68284868eae7e34f54c51664486c1dd47cac7 (diff)
feat: add pygments support
-rw-r--r--Containerfile14
-rw-r--r--syntax-highlighting.py35
2 files changed, 49 insertions, 0 deletions
diff --git a/Containerfile b/Containerfile
index 0e46cdb..e15c99c 100644
--- a/Containerfile
+++ b/Containerfile
@@ -1,7 +1,10 @@
ARG DEBIAN_VERSION=trixie
+ARG CGIT_VERSION=1.3.1
+ARG IMAGE_REVISION=1
FROM debian:${DEBIAN_VERSION}-slim AS builder
+ARG CGIT_VERSION=1.3.1
ARG CGIT_COMMIT=044821677c774cd24f25f1818ea51d09cc64b006
RUN apt-get update \
@@ -27,10 +30,12 @@ RUN git init cgit \
WORKDIR /src/cgit
RUN make -j"$(nproc)" \
+ CGIT_VERSION="${CGIT_VERSION}" \
NO_LUA=1 \
NO_GETTEXT=YesPlease
RUN make install \
+ CGIT_VERSION="${CGIT_VERSION}" \
NO_LUA=1 \
NO_GETTEXT=YesPlease \
DESTDIR=/out \
@@ -40,15 +45,24 @@ RUN make install \
FROM debian:${DEBIAN_VERSION}-slim
+ARG CGIT_VERSION=1.3.1
+ARG IMAGE_REVISION=1
+
+LABEL org.opencontainers.image.title="cgit" \
+ org.opencontainers.image.version="${CGIT_VERSION}-r${IMAGE_REVISION}"
+
RUN apt-get update \
&& apt-get install --no-install-recommends -y \
busybox-static \
+ python3-pygments \
tini \
zlib1g \
&& rm -rf /var/lib/apt/lists/*
COPY --from=builder /out/www /www
+COPY --chmod=0555 syntax-highlighting.py /usr/libexec/cgit/syntax-highlighting.py
+
RUN mkdir -p \
/srv/git \
/var/cache/cgit
diff --git a/syntax-highlighting.py b/syntax-highlighting.py
new file mode 100644
index 0000000..2186c35
--- /dev/null
+++ b/syntax-highlighting.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+
+"""Render cgit source blobs as syntax-highlighted, HTML-escaped markup."""
+
+import io
+import sys
+
+from pygments import highlight
+from pygments.formatters import HtmlFormatter
+from pygments.lexers import TextLexer, guess_lexer, guess_lexer_for_filename
+from pygments.util import ClassNotFound
+
+
+sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding="utf-8", errors="replace")
+sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
+
+source = sys.stdin.read()
+filename = sys.argv[1]
+formatter = HtmlFormatter(style="monokai", nobackground=True)
+
+try:
+ lexer = guess_lexer_for_filename(filename, source)
+except (ClassNotFound, TypeError):
+ if source.startswith("#!"):
+ try:
+ lexer = guess_lexer(source)
+ except ClassNotFound:
+ lexer = TextLexer()
+ else:
+ lexer = TextLexer()
+
+sys.stdout.write("<style>")
+sys.stdout.write(formatter.get_style_defs(".highlight"))
+sys.stdout.write("</style>")
+sys.stdout.write(highlight(source, lexer, formatter))