]> gitweb.ps.run Git - ps-cgit/blobdiff - filters/syntax-highlighting.py
filters: migrate from luacrypto to luaossl
[ps-cgit] / filters / syntax-highlighting.py
index bcf32c82af2309302324a93c87291a98c90b484e..e912594c48a71fce1baffb11149ab9b72cc223d4 100755 (executable)
@@ -1,6 +1,6 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
 
-# This script uses Pygments and Python2. You must have both installed
+# This script uses Pygments and Python3. You must have both installed
 # for this to work.
 #
 # http://pygments.org/
@@ -21,6 +21,7 @@
 
 
 import sys
+import io
 from pygments import highlight
 from pygments.util import ClassNotFound
 from pygments.lexers import TextLexer
@@ -29,25 +30,26 @@ from pygments.lexers import guess_lexer_for_filename
 from pygments.formatters import HtmlFormatter
 
 
-# read stdin and decode to utf-8. ignore any unkown signs.
-data = sys.stdin.read().decode(encoding='utf-8', errors='ignore')
+sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8', errors='replace')
+sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
+data = sys.stdin.read()
 filename = sys.argv[1]
-formatter = HtmlFormatter(encoding='utf-8', style='pastie')
+formatter = HtmlFormatter(style='pastie', nobackground=True)
 
 try:
-       lexer = guess_lexer_for_filename(filename, data, encoding='utf-8')
+       lexer = guess_lexer_for_filename(filename, data)
 except ClassNotFound:
        # check if there is any shebang
        if data[0:2] == '#!':
-               lexer = guess_lexer(data, encoding='utf-8')
+               lexer = guess_lexer(data)
        else:
-               lexer = TextLexer(encoding='utf-8')
+               lexer = TextLexer()
 except TypeError:
-       lexer = TextLexer(encoding='utf-8')
+       lexer = TextLexer()
 
 # highlight! :-)
 # printout pygments' css definitions as well
 sys.stdout.write('<style>')
 sys.stdout.write(formatter.get_style_defs('.highlight'))
 sys.stdout.write('</style>')
-highlight(data, lexer, formatter, outfile=sys.stdout)
+sys.stdout.write(highlight(data, lexer, formatter, outfile=None))