summaryrefslogtreecommitdiffstats
path: root/cgit-pygments-wrapper
blob: 5e49e8ae718b6facef420bf2860e085f77bd9034 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
"""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
import re

from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import (
    CommonLispLexer,
    SchemeLexer,
    TextLexer,
    get_lexer_by_name,
    guess_lexer,
    guess_lexer_for_filename,
)
from pygments.lexers.shell import BashLexer
from pygments.util import ClassNotFound, shebang_matches


def modeline_matches(text, regex):
    line_1_index = text.find("\n")
    line_2_index = text.find("\n", line_1_index + 1)
    regex = re.compile(r"-*- .*?%s.* -*-" % regex, re.IGNORECASE)

    if line_1_index >= 0:
        first_line = text[:line_1_index].lower()
        second_line = text[line_1_index:line_2_index].lower()
    else:
        first_line = text.lower()
        second_line = ""

    return (
        regex.search(first_line) is not None
        or regex.search(second_line) is not None
    )


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")
    elif (
        shebang_matches(data, r"guile")
        or modeline_matches(data, r"mode: scheme")
        or shebang_matches(data, r"scsh")
    ):
        lexer = SchemeLexer()
    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"))