summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xcgit-pygments-wrapper24
1 files changed, 23 insertions, 1 deletions
diff --git a/cgit-pygments-wrapper b/cgit-pygments-wrapper
index f4e7a96..60a1ab1 100755
--- a/cgit-pygments-wrapper
+++ b/cgit-pygments-wrapper
@@ -14,6 +14,7 @@ CGIT_REPO_CLONE_URL ( = repo.clone-url setting )
"""
import sys
+import re
from pygments import highlight
from pygments.formatters import HtmlFormatter
@@ -28,6 +29,25 @@ from pygments.lexers import (
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")
@@ -43,7 +63,9 @@ formatter = HtmlFormatter(encoding="utf-8", style="autumn")
try:
if filename == "CMakeLists.txt":
lexer = get_lexer_by_name("cmake")
- elif shebang_matches(data, r"guile"):
+ elif shebang_matches(data, r"guile") or modeline_matches(
+ data, r"mode: scheme"
+ ):
lexer = SchemeLexer()
else:
lexer = guess_lexer_for_filename(filename, data, encoding="utf-8")