summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2020-06-21 14:30:58 +0200
committerJonas Smedegaard <dr@jones.dk>2020-06-21 14:30:58 +0200
commit1ba5e5f144571aa55957ca3c8445a297d598bd92 (patch)
tree18c327fcb3c9916540468f428fcaa5a48afb8337
parent2cbc4ce1506989c116ee418c28c7c283627750eb (diff)
(ab)use source filter to parse markdown as html, with line-numbered + highlighted plaintext only as fallback
-rw-r--r--cgit/cgitrc9
-rw-r--r--cgit/filters/syntax-highlighting.sh36
2 files changed, 44 insertions, 1 deletions
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:
+# <li style='list-style-type:none'><a id='n2' href='#n2'>2</a>
+# TODO: hide anchor except in :hover mode:
+# <li><a id='n2' href='#n2'>ยง</a>
+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