mirror of
https://git.zx2c4.com/cgit
synced 2024-11-22 08:28:42 +00:00
filters: port syntax-highlighting.py to python 3.x
Signed-off-by: Christian Hesse <mail@eworm.de>
This commit is contained in:
parent
3f9e14ada1
commit
143e65252c
@ -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.
|
# for this to work.
|
||||||
#
|
#
|
||||||
# http://pygments.org/
|
# http://pygments.org/
|
||||||
@ -29,25 +29,24 @@ from pygments.lexers import guess_lexer_for_filename
|
|||||||
from pygments.formatters import HtmlFormatter
|
from pygments.formatters import HtmlFormatter
|
||||||
|
|
||||||
|
|
||||||
# read stdin and decode to utf-8. ignore any unkown signs.
|
data = sys.stdin.read()
|
||||||
data = sys.stdin.read().decode(encoding='utf-8', errors='ignore')
|
|
||||||
filename = sys.argv[1]
|
filename = sys.argv[1]
|
||||||
formatter = HtmlFormatter(encoding='utf-8', style='pastie')
|
formatter = HtmlFormatter(style='pastie')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
lexer = guess_lexer_for_filename(filename, data, encoding='utf-8')
|
lexer = guess_lexer_for_filename(filename, data)
|
||||||
except ClassNotFound:
|
except ClassNotFound:
|
||||||
# check if there is any shebang
|
# check if there is any shebang
|
||||||
if data[0:2] == '#!':
|
if data[0:2] == '#!':
|
||||||
lexer = guess_lexer(data, encoding='utf-8')
|
lexer = guess_lexer(data)
|
||||||
else:
|
else:
|
||||||
lexer = TextLexer(encoding='utf-8')
|
lexer = TextLexer()
|
||||||
except TypeError:
|
except TypeError:
|
||||||
lexer = TextLexer(encoding='utf-8')
|
lexer = TextLexer()
|
||||||
|
|
||||||
# highlight! :-)
|
# highlight! :-)
|
||||||
# printout pygments' css definitions as well
|
# printout pygments' css definitions as well
|
||||||
sys.stdout.write('<style>')
|
sys.stdout.write('<style>')
|
||||||
sys.stdout.write(formatter.get_style_defs('.highlight'))
|
sys.stdout.write(formatter.get_style_defs('.highlight'))
|
||||||
sys.stdout.write('</style>')
|
sys.stdout.write('</style>')
|
||||||
highlight(data, lexer, formatter, outfile=sys.stdout)
|
sys.stdout.write(highlight(data, lexer, formatter, outfile=None))
|
||||||
|
Loading…
Reference in New Issue
Block a user