Initial commit
This commit is contained in:
commit
359be2dfa0
1 changed files with 55 additions and 0 deletions
55
cgit-pygments-wrapper.py
Executable file
55
cgit-pygments-wrapper.py
Executable file
|
@ -0,0 +1,55 @@
|
|||
#!/usr/bin/env
|
||||
'''This script is for use as a source-filter or repo.source-filter for cgit.
|
||||
|
||||
The following environment variables can be used to retrieve the configuration of
|
||||
the repository for which this script is called:
|
||||
|
||||
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
|
||||
|
||||
from pyments import highlight
|
||||
from pygments.formatters import HtmlFormatter
|
||||
from pygments.lexers import CommonLispLexer, TextLexer, guess_lexer, \
|
||||
guess_lexer_for_filename, get_lexer_by_name
|
||||
from pygments.lexers.shell import BashLexer
|
||||
from pygments.util import ClassNotFound
|
||||
|
||||
CommonLispLexer.filenames.append('*.asd')
|
||||
CommonLispLexer.filenames.append('.stumpwmrc')
|
||||
|
||||
BashLexer.filenames.append('PKGBUILD')
|
||||
BashLexer.filenames.append('*.install')
|
||||
BashLexer.filenames.append('.zshrc')
|
||||
BashLexer.filenames.append('.zprofile')
|
||||
|
||||
data = sys.stdin.read()
|
||||
filename = sys.argv[1]
|
||||
formatter = HtmlFormatter(encoding='utf-8', style='autumn')
|
||||
|
||||
try:
|
||||
if filename == 'CMakeLists.txt':
|
||||
lexer = get_lexer_by_name('cmake')
|
||||
else:
|
||||
lexer = guess_lexer_for_filename(filename, data, encoding='utf-8')
|
||||
except ClassNotFound:
|
||||
# Check if there is any shebang
|
||||
if data[0:2] == '#!':
|
||||
lexer = guess_lexer(data, encoding='utf-8')
|
||||
else:
|
||||
lexer = TextLexer(encoding='utf-8')
|
||||
except TypeError:
|
||||
lexer = TextLexer(encoding='utf-8')
|
||||
|
||||
# print out pygments' css definitions as well
|
||||
sys.stdout.write('<style>')
|
||||
sys.stdout.write(formatter.get_style_defs('.highlight'))
|
||||
sys.stdout.write('</style>')
|
||||
sys.stdout.write(highlight(data, lexer, formatter).decode('utf-8'))
|
Loading…
Reference in a new issue