diff options
author | Stefan Tatschner <stefan@sevenbyte.org> | 2014-01-14 06:10:45 (JST) |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2014-01-14 06:48:51 (JST) |
commit | ceffeb5d52609a38c5d5d410456d796673fe8461 (patch) | |
tree | c262452378843ea97e893caf5866d3848553e380 | |
parent | a52aaa90dab1d95f9df383d2cb3f7c428ab849fe (diff) | |
download | cgit-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-x | filters/syntax-highlighting.py | 52 |
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 | ||
20 | import sys | 23 | import sys |
21 | import cgi | ||
22 | import codecs | ||
23 | from pygments.lexers import get_lexer_for_filename | ||
24 | from pygments import highlight | 24 | from pygments import highlight |
25 | from pygments.util import ClassNotFound | ||
26 | from pygments.lexers import TextLexer | ||
27 | from pygments.lexers import guess_lexer | ||
28 | from pygments.lexers import guess_lexer_for_filename | ||
25 | from pygments.formatters import HtmlFormatter | 29 | from pygments.formatters import HtmlFormatter |
26 | 30 | ||
27 | sys.stdin = codecs.getreader("utf-8")(sys.stdin.detach()) | 31 | |
28 | sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach()) | 32 | # read stdin and decode to utf-8. ignore any unkown signs. |
29 | doc = sys.stdin.read() | 33 | data = sys.stdin.read().decode(encoding='utf-8', errors='ignore') |
34 | filename = sys.argv[1] | ||
35 | formatter = HtmlFormatter(encoding='utf-8', style='pastie') | ||
36 | |||
30 | try: | 37 | try: |
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') | 39 | except 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') | ||
45 | except TypeError: | ||
46 | lexer = TextLexer(encoding='utf-8') | ||
36 | 47 | ||
37 | highlight(doc, lexer, formatter, sys.stdout) | 48 | # highlight! :-) |
38 | except: | 49 | # printout pygments' css definitions as well |
39 | sys.stdout.write(str(cgi.escape(doc).encode("ascii", "xmlcharrefreplace"), "ascii")) | 50 | sys.stdout.write('<style>') |
51 | sys.stdout.write(formatter.get_style_defs('.highlight')) | ||
52 | sys.stdout.write('</style>') | ||
53 | highlight(data, lexer, formatter, outfile=sys.stdout) | ||