From 1ba5e5f144571aa55957ca3c8445a297d598bd92 Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Sun, 21 Jun 2020 14:30:58 +0200 Subject: (ab)use source filter to parse markdown as html, with line-numbered + highlighted plaintext only as fallback --- cgit/cgitrc | 9 ++++++++- cgit/filters/syntax-highlighting.sh | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 cgit/filters/syntax-highlighting.sh diff --git a/cgit/cgitrc b/cgit/cgitrc index be5b9aa..6542d46 100644 --- a/cgit/cgitrc +++ b/cgit/cgitrc @@ -11,7 +11,14 @@ remove-suffix=1 clone-url=git://$HTTP_HOST/$CGIT_REPO_URL user@$HTTP_HOST:$CGIT_REPO_URL https://$HTTP_HOST$SCRIPT_NAME/$CGIT_REPO_URL http://$HTTP_HOST$SCRIPT_NAME/$CGIT_REPO_URL -source-filter=/usr/lib/cgit/filters/syntax-highlighting.sh +# (ab)use source filter to parse markdown as html, +# with line-numbered + highlighted plaintext only as fallback +# TODO: add anchor to line-numbering: +#
  • 2 +# TODO: hide anchor except in :hover mode: +#
  • ยง +enable-tree-linenumbers=0 +source-filter=/etc/local-COMMON/cgit/filters/syntax-highlighting.sh about-filter=/etc/local-COMMON/cgit/filters/about-formatting.sh readme=:README.md diff --git a/cgit/filters/syntax-highlighting.sh b/cgit/filters/syntax-highlighting.sh new file mode 100644 index 0000000..44cd310 --- /dev/null +++ b/cgit/filters/syntax-highlighting.sh @@ -0,0 +1,36 @@ +#!/bin/sh +# This script can be used to implement syntax highlighting in the cgit +# tree-view by referring to this file with the source-filter or repo.source- +# filter options in cgitrc. + +# Depends: cmark highlight + +# The following environment variables can be used to retrieve the configuration +# of the repository for which this script is called: +# CGIT_REPO_URL ( = repo.url setting ) +# CGIT_REPO_NAME ( = repo.name setting ) +# CGIT_REPO_PATH ( = repo.path setting ) +# CGIT_REPO_OWNER ( = repo.owner setting ) +# CGIT_REPO_DEFBRANCH ( = repo.defbranch setting ) +# CGIT_REPO_SECTION ( = section setting ) +# CGIT_REPO_CLONE_URL ( = repo.clone-url setting ) + +# store filename and extension in local vars +BASENAME="$1" +EXTENSION="${BASENAME##*.}" + +[ "${BASENAME}" = "${EXTENSION}" ] && EXTENSION=txt +[ -z "${EXTENSION}" ] && EXTENSION=txt + +# map Makefile and Makefile.* to .mk +[ "${BASENAME%%.*}" = "Makefile" ] && EXTENSION=mk + +# map less common markdown extensions to .md +case "$(printf '%s' "$EXTENSION" | tr '[:upper:]' '[:lower:]')" in + markdown|mdown|mkd|mdwn) EXTENSION=md;; +esac + +case "$EXTENSION" in + md) exec cmark --safe;; + *) exec highlight --force -f -I -O xhtml -S "$EXTENSION" --ordered-list --line-number-ref=n 2>/dev/null;; +esac -- cgit v1.2.3