Skip to content

Commit bad8c52

Browse files
committed
Fall back to guess the lexer by content
If we can't guess the lexer by the file name, try to guess based on the content. This allows pygments to colorize extension-less files, usually scripts. Signed-off-by: Alberto Bertogli <[email protected]>
1 parent 62da3eb commit bad8c52

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

utils.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@ def colorize_diff(s):
5050

5151
def colorize_blob(fname, s):
5252
try:
53-
lexer = lexers.guess_lexer_for_filename(fname, s)
53+
lexer = lexers.guess_lexer_for_filename(fname, s, encoding = 'utf-8')
5454
except lexers.ClassNotFound:
55-
lexer = lexers.TextLexer(encoding = 'utf-8')
55+
try:
56+
lexer = lexers.guess_lexer(s[:200], encoding = 'utf-8')
57+
except lexers.ClassNotFound:
58+
lexer = lexers.TextLexer(encoding = 'utf-8')
59+
5660
formatter = HtmlFormatter(encoding = 'utf-8',
5761
cssclass = 'source_code',
5862
linenos = 'table')

0 commit comments

Comments
 (0)