filters/syntax-highlighting.sh (view raw)
1#!/bin/sh
2# This script can be used to implement syntax highlighting in the cgit
3# tree-view by refering to this file with the source-filter or repo.source-
4# filter options in cgitrc.
5#
6# This script requires a shell supporting the ${var##pattern} syntax.
7# It is supported by at least dash and bash, however busybox environments
8# might have to use an external call to sed instead.
9#
10# Note: the highlight command (http://www.andre-simon.de/) uses css for syntax
11# highlighting, so you'll probably want something like the following included
12# in your css file (generated by highlight 2.4.8 and adapted for cgit):
13#
14# table.blob .num { color:#2928ff; }
15# table.blob .esc { color:#ff00ff; }
16# table.blob .str { color:#ff0000; }
17# table.blob .dstr { color:#818100; }
18# table.blob .slc { color:#838183; font-style:italic; }
19# table.blob .com { color:#838183; font-style:italic; }
20# table.blob .dir { color:#008200; }
21# table.blob .sym { color:#000000; }
22# table.blob .kwa { color:#000000; font-weight:bold; }
23# table.blob .kwb { color:#830000; }
24# table.blob .kwc { color:#000000; font-weight:bold; }
25# table.blob .kwd { color:#010181; }
26#
27# The following environment variables can be used to retrieve the configuration
28# of the repository for which this script is called:
29# CGIT_REPO_URL ( = repo.url setting )
30# CGIT_REPO_NAME ( = repo.name setting )
31# CGIT_REPO_PATH ( = repo.path setting )
32# CGIT_REPO_OWNER ( = repo.owner setting )
33# CGIT_REPO_DEFBRANCH ( = repo.defbranch setting )
34# CGIT_REPO_SECTION ( = section setting )
35# CGIT_REPO_CLONE_URL ( = repo.clone-url setting )
36#
37
38# store filename and extension in local vars
39BASENAME="$1"
40EXTENSION="${BASENAME##*.}"
41
42# map Makefile and Makefile.* to .mk
43[ "${BASENAME%%.*}" == "Makefile" ] && EXTENSION=mk
44
45# highlight versions 2 and 3 have different commandline options. Specifically,
46# the -X option that is used for version 2 is replaced by the -O xhtml option
47# for version 3.
48#
49# Version 2 can be found (for example) on EPEL 5, while version 3 can be
50# found (for example) on EPEL 6.
51#
52# This is for version 2
53exec highlight --force -f -I -X -S $EXTENSION 2>/dev/null
54
55# This is for version 3
56#
57# On CentOS 6.2 (using highlight from EPEL), when highlight doesn't know about
58# an EXTENSION, it outputs a lua error and _no_ text, even when the --force
59# option is used.
60#
61# Also see the bug reports at:
62# http://sourceforge.net/tracker/?func=detail&aid=3490017&group_id=215618&atid=1034391
63# https://bugzilla.redhat.com/show_bug.cgi?id=795567
64#
65# This workaround can be removed when the bug is fixed upstream and the new
66# version is packaged in most distributions.
67#
68# The workaround is to set the extension to 'txt' (plain text) when highlight
69# exits with an error (doesn't know the format).
70#
71#echo "test" | highlight -f -I -O xhtml -S $EXTENSION &>/dev/null
72#[ ${?} -ne 0 ] && EXTENSION="txt"
73#exec highlight --force -f -I -O xhtml -S $EXTENSION 2>/dev/null