From 359be2dfa041bc0e1c690b2771d556988612b327 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Fri, 13 Nov 2020 22:58:55 -0800 Subject: [PATCH] Initial commit --- cgit-pygments-wrapper.py | 55 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 cgit-pygments-wrapper.py 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'))