2015-10-12 18:23:56 +02:00
|
|
|
#!/usr/bin/env python3
|
2013-05-27 21:39:43 +02:00
|
|
|
|
2015-10-12 18:23:56 +02:00
|
|
|
# This script uses Pygments and Python3. You must have both installed
|
2014-01-13 22:10:45 +01:00
|
|
|
# for this to work.
|
|
|
|
#
|
2013-05-27 21:39:43 +02:00
|
|
|
# http://pygments.org/
|
|
|
|
# http://python.org/
|
|
|
|
#
|
2014-01-13 22:10:45 +01:00
|
|
|
# It may be used with the source-filter or repo.source-filter settings
|
|
|
|
# in cgitrc.
|
2013-05-27 21:39:43 +02:00
|
|
|
#
|
2014-01-13 22:10:45 +01:00
|
|
|
# The following environment variables can be used to retrieve the
|
|
|
|
# configuration of the repository for which this script is called:
|
2013-05-27 21:39:43 +02:00
|
|
|
# CGIT_REPO_URL ( = repo.url setting )
|
|
|
|
# CGIT_REPO_NAME ( = repo.name setting )
|
|
|
|
# CGIT_REPO_PATH ( = repo.path setting )
|
|
|
|
# CGIT_REPO_OWNER ( = repo.owner setting )
|
|
|
|
# CGIT_REPO_DEFBRANCH ( = repo.defbranch setting )
|
|
|
|
# CGIT_REPO_SECTION ( = section setting )
|
|
|
|
# CGIT_REPO_CLONE_URL ( = repo.clone-url setting )
|
|
|
|
|
|
|
|
|
|
|
|
import sys
|
2016-01-18 11:14:06 +01:00
|
|
|
import io
|
2013-05-27 21:39:43 +02:00
|
|
|
from pygments import highlight
|
2014-01-13 22:10:45 +01:00
|
|
|
from pygments.util import ClassNotFound
|
|
|
|
from pygments.lexers import TextLexer
|
|
|
|
from pygments.lexers import guess_lexer
|
|
|
|
from pygments.lexers import guess_lexer_for_filename
|
2013-05-27 21:39:43 +02:00
|
|
|
from pygments.formatters import HtmlFormatter
|
|
|
|
|
2014-01-13 22:10:45 +01:00
|
|
|
|
2017-01-22 12:44:44 +01:00
|
|
|
sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8', errors='replace')
|
|
|
|
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
|
2015-10-12 18:23:56 +02:00
|
|
|
data = sys.stdin.read()
|
2014-01-13 22:10:45 +01:00
|
|
|
filename = sys.argv[1]
|
2017-10-29 03:43:26 +01:00
|
|
|
formatter = HtmlFormatter(style='pastie', nobackground=True)
|
2014-01-13 22:10:45 +01:00
|
|
|
|
2013-05-27 21:39:43 +02:00
|
|
|
try:
|
2015-10-12 18:23:56 +02:00
|
|
|
lexer = guess_lexer_for_filename(filename, data)
|
2014-01-13 22:10:45 +01:00
|
|
|
except ClassNotFound:
|
|
|
|
# check if there is any shebang
|
|
|
|
if data[0:2] == '#!':
|
2015-10-12 18:23:56 +02:00
|
|
|
lexer = guess_lexer(data)
|
2014-01-13 22:10:45 +01:00
|
|
|
else:
|
2015-10-12 18:23:56 +02:00
|
|
|
lexer = TextLexer()
|
2014-01-13 22:10:45 +01:00
|
|
|
except TypeError:
|
2015-10-12 18:23:56 +02:00
|
|
|
lexer = TextLexer()
|
2013-05-27 21:39:43 +02:00
|
|
|
|
2014-01-13 22:10:45 +01:00
|
|
|
# highlight! :-)
|
|
|
|
# printout pygments' css definitions as well
|
|
|
|
sys.stdout.write('<style>')
|
|
|
|
sys.stdout.write(formatter.get_style_defs('.highlight'))
|
|
|
|
sys.stdout.write('</style>')
|
2015-10-12 18:23:56 +02:00
|
|
|
sys.stdout.write(highlight(data, lexer, formatter, outfile=None))
|