about summary refs log tree commit diff
path: root/filters/syntax-highlighting.py
blob: dcdba034f89d35caf716c9d122ab1b82f0324221 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python3

# This script uses Pygments and Python3. You must have both installed for this to work.
# http://pygments.org/
# http://python.org/
#
# It may be used with the source-filter or repo.source-filter settings in cgitrc.
#
# 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 )


import sys
import cgi
import codecs
from pygments.lexers import get_lexer_for_filename
from pygments import highlight
from pygments.formatters import HtmlFormatter

sys.stdin = codecs.getreader("utf-8")(sys.stdin.detach())
doc = sys.stdin.read()
try:
	lexer = get_lexer_for_filename(sys.argv[1])
	formatter = HtmlFormatter(style='pastie')
	sys.stdout.write("<style>")
	sys.stdout.write(formatter.get_style_defs('.highlight'))
	sys.stdout.write("</style>")

	highlight(doc, lexer, formatter, sys.stdout)
except:
	sys.stdout.write(str(cgi.escape(doc).encode("ascii", "xmlcharrefreplace"), "ascii"))
7029894d3a3837166996b35&follow=1'>cgit.mk: fix dependency handlingJohn Keeping 2013-04-08Makefile: re-include cgit.conf in cgit.mkJohn Keeping 2013-04-08Do not unnecessarily strdup() environment variablesLukas Fleischer 2013-04-08Maŕk cgit_environment members constLukas Fleischer 2013-04-08Return const char * in cgit_{httpscheme, hosturl, rooturl}()Lukas Fleischer 2013-04-08Update git to v1.8.2.1John Keeping 2013-04-07ui-blob: don't segfault when no path is givenJohn Keeping 2013-03-20Convert pager navigation into a unordered listLukas Fleischer 2013-03-20Makefile: remove CGIT-CFLAGS files in clean stageJason A. Donenfeld 2013-03-20ui-summary.c: Move urls variable into print_urls()Lukas Fleischer 2013-03-20Fix colspan valuesLukas Fleischer 2013-03-20html: check return value of writeJason A. Donenfeld 2013-03-20ui-shared: squelch compiler warning.Jason A. Donenfeld 2013-03-20cgit.mk: Use SHELL_PATH_SQ to run gen-version.shJohn Keeping 2013-03-20cgit.mk: don't rebuild everything if CGIT_VERSION changesJohn Keeping 2013-03-20ui-patch: use cgit_version not CGIT_VERSIONJohn Keeping 2013-03-20Makefile: re-use Git's Makefile where possibleJohn Keeping