summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Tom Willemse2024-04-10 10:08:40 -0700
committerGravatar Tom Willemse2024-04-10 10:10:30 -0700
commit5fe72c0e4ff1352804e0be254a6fcf5cbff4f9c6 (patch)
treef88e38a3b8ef7dd0ec3354e923000b0bd19b3403
parent34ea1c73c12b1766aa7bafa92fc1fd1c82e782eb (diff)
downloadcgit-pygments-wrapper-5fe72c0e4ff1352804e0be254a6fcf5cbff4f9c6.tar.gz
cgit-pygments-wrapper-5fe72c0e4ff1352804e0be254a6fcf5cbff4f9c6.zip
Use the Scheme lexer if a scheme mode line is found
Emacs allows one to specify a property line at the top of the file (usually first line, but if there is a shebang it's allowed on the second line) that can include which mode to use. This can be used to discover which lexer to use.
-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")