From 5fe72c0e4ff1352804e0be254a6fcf5cbff4f9c6 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Wed, 10 Apr 2024 10:08:40 -0700 Subject: 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. --- cgit-pygments-wrapper | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) 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") -- cgit v1.2.3-54-g00ecf