aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Stefan Tatschner <stefan@sevenbyte.org>2014-01-14 06:10:45 (JST)
committerGravatar Jason A. Donenfeld <Jason@zx2c4.com>2014-01-14 06:48:51 (JST)
commitceffeb5d52609a38c5d5d410456d796673fe8461 (patch)
treec262452378843ea97e893caf5866d3848553e380
parenta52aaa90dab1d95f9df383d2cb3f7c428ab849fe (diff)
downloadcgit-ceffeb5d52609a38c5d5d410456d796673fe8461.zip
cgit-ceffeb5d52609a38c5d5d410456d796673fe8461.tar.gz
filters: Improved syntax-highlighting.py
- Switched back to python2 according to a problem in pygments with python3. With the next release of pygments this problem should be fixed. Issue see here: https://bitbucket.org/birkenfeld/pygments-main/issue/901/problems-with-python3 - Just read the stdin, decode it to utf-8 and ignore unknown signs. This ensures that even destroyed files do not cause any errors in the filter. - Improved language guessing: -> At first use guess_lexer_for_filename for a better detection of the used programming languages (even mixed cases will be detected, e.g. php + html). -> If nothing was found look if there is a shebang and use guess_lexer. -> As default/fallback choose TextLexer. Signed-off-by: Stefan Tatschner <stefan@sevenbyte.org>
-rwxr-xr-xfilters/syntax-highlighting.py52
1 files changed, 33 insertions, 19 deletions
diff --git a/filters/syntax-highlighting.py b/filters/syntax-highlighting.py
index 72d9097..bcf32c8 100755
--- a/filters/syntax-highlighting.py
+++ b/filters/syntax-highlighting.py
@@ -1,13 +1,16 @@
1#!/usr/bin/env python3 1#!/usr/bin/env python2
2 2
3# This script uses Pygments and Python3. You must have both installed for this to work. 3# This script uses Pygments and Python2. You must have both installed
4# for this to work.
5#
4# http://pygments.org/ 6# http://pygments.org/
5# http://python.org/ 7# http://python.org/
6# 8#
7# It may be used with the source-filter or repo.source-filter settings in cgitrc. 9# It may be used with the source-filter or repo.source-filter settings
10# in cgitrc.
8# 11#
9# The following environment variables can be used to retrieve the configuration 12# The following environment variables can be used to retrieve the
10# of the repository for which this script is called: 13# configuration of the repository for which this script is called:
11# CGIT_REPO_URL ( = repo.url setting ) 14# CGIT_REPO_URL ( = repo.url setting )
12# CGIT_REPO_NAME ( = repo.name setting ) 15# CGIT_REPO_NAME ( = repo.name setting )
13# CGIT_REPO_PATH ( = repo.path setting ) 16# CGIT_REPO_PATH ( = repo.path setting )
@@ -18,22 +21,33 @@
18 21
19 22
20import sys 23import sys
21import cgi
22import codecs
23from pygments.lexers import get_lexer_for_filename
24from pygments import highlight 24from pygments import highlight
25from pygments.util import ClassNotFound
26from pygments.lexers import TextLexer
27from pygments.lexers import guess_lexer
28from pygments.lexers import guess_lexer_for_filename
25from pygments.formatters import HtmlFormatter 29from pygments.formatters import HtmlFormatter
26 30
27sys.stdin = codecs.getreader("utf-8")(sys.stdin.detach()) 31
28sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach()) 32# read stdin and decode to utf-8. ignore any unkown signs.
29doc = sys.stdin.read() 33data = sys.stdin.read().decode(encoding='utf-8', errors='ignore')
34filename = sys.argv[1]
35formatter = HtmlFormatter(encoding='utf-8', style='pastie')
36
30try: 37try:
31 lexer = get_lexer_for_filename(sys.argv[1]) 38 lexer = guess_lexer_for_filename(filename, data, encoding='utf-8')
32 formatter = HtmlFormatter(style='pastie') 39except ClassNotFound:
33 sys.stdout.write("<style>") 40 # check if there is any shebang
34 sys.stdout.write(formatter.get_style_defs('.highlight')) 41 if data[0:2] == '#!':
35 sys.stdout.write("</style>") 42 lexer = guess_lexer(data, encoding='utf-8')
43 else:
44 lexer = TextLexer(encoding='utf-8')
45except TypeError:
46 lexer = TextLexer(encoding='utf-8')
36 47
37 highlight(doc, lexer, formatter, sys.stdout) 48# highlight! :-)
38except: 49# printout pygments' css definitions as well
39 sys.stdout.write(str(cgi.escape(doc).encode("ascii", "xmlcharrefreplace"), "ascii")) 50sys.stdout.write('<style>')
51sys.stdout.write(formatter.get_style_defs('.highlight'))
52sys.stdout.write('</style>')
53highlight(data, lexer, formatter, outfile=sys.stdout)