about summary refs log tree commit diff stats
path: root/filters
diff options
context:
space:
mode:
Diffstat (limited to 'filters')
-rwxr-xr-xfilters/syntax-highlighting.py19
1 files changed, 9 insertions, 10 deletions
diff --git a/filters/syntax-highlighting.py b/filters/syntax-highlighting.py index bcf32c8..b5d615e 100755 --- a/filters/syntax-highlighting.py +++ b/filters/syntax-highlighting.py
@@ -1,6 +1,6 @@
1#!/usr/bin/env python2 1#!/usr/bin/env python3
2 2
3# This script uses Pygments and Python2. You must have both installed 3# This script uses Pygments and Python3. You must have both installed
4# for this to work. 4# for this to work.
5# 5#
6# http://pygments.org/ 6# http://pygments.org/
@@ -29,25 +29,24 @@ from pygments.lexers import guess_lexer_for_filename
29from pygments.formatters import HtmlFormatter 29from pygments.formatters import HtmlFormatter
30 30
31 31
32# read stdin and decode to utf-8. ignore any unkown signs. 32data = sys.stdin.read()
33data = sys.stdin.read().decode(encoding='utf-8', errors='ignore')
34filename = sys.argv[1] 33filename = sys.argv[1]
35formatter = HtmlFormatter(encoding='utf-8', style='pastie') 34formatter = HtmlFormatter(style='pastie')
36 35
37try: 36try:
38 lexer = guess_lexer_for_filename(filename, data, encoding='utf-8') 37 lexer = guess_lexer_for_filename(filename, data)
39except ClassNotFound: 38except ClassNotFound:
40 # check if there is any shebang 39 # check if there is any shebang
41 if data[0:2] == '#!': 40 if data[0:2] == '#!':
42 lexer = guess_lexer(data, encoding='utf-8') 41 lexer = guess_lexer(data)
43 else: 42 else:
44 lexer = TextLexer(encoding='utf-8') 43 lexer = TextLexer()
45except TypeError: 44except TypeError:
46 lexer = TextLexer(encoding='utf-8') 45 lexer = TextLexer()
47 46
48# highlight! :-) 47# highlight! :-)
49# printout pygments' css definitions as well 48# printout pygments' css definitions as well
50sys.stdout.write('<style>') 49sys.stdout.write('<style>')
51sys.stdout.write(formatter.get_style_defs('.highlight')) 50sys.stdout.write(formatter.get_style_defs('.highlight'))
52sys.stdout.write('</style>') 51sys.stdout.write('</style>')
53highlight(data, lexer, formatter, outfile=sys.stdout) 52sys.stdout.write(highlight(data, lexer, formatter, outfile=None))