commit 359be2dfa041bc0e1c690b2771d556988612b327 Author: Tom Willemse Date: Fri Nov 13 22:58:55 2020 -0800 Initial commit diff --git a/cgit-pygments-wrapper.py b/cgit-pygments-wrapper.py new file mode 100755 index 0000000..ad17227 --- /dev/null +++ b/cgit-pygments-wrapper.py @@ -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('') +sys.stdout.write(highlight(data, lexer, formatter).decode('utf-8'))