Skip to content

Commit f0b806f

Browse files
authored
autopygmentize: use file before pygmentize; add JSON support (#1786)
Although pygmentize -N is much cheaper than file, it makes some bad guesses, so use file first. Add support for MIME type application/json.
1 parent f0d433d commit f0b806f

File tree

1 file changed

+53
-53
lines changed

1 file changed

+53
-53
lines changed

external/autopygmentize

+53-53
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
# Best effort auto-pygmentization with transparent decompression
3-
# by Reuben Thomas 2008-2019
3+
# by Reuben Thomas 2008-2021
44
# This program is in the public domain.
55

66
# Strategy: first see if pygmentize can find a lexer; if not, ask file; if that finds nothing, fail
@@ -13,58 +13,58 @@ options=${@:1:$(($#-1))} # handle others args as options to pass to pygmentize
1313

1414
file_common_opts="--brief --dereference"
1515

16-
lexer=$(pygmentize -N "$file")
17-
if [[ "$lexer" == text ]]; then
18-
# Try to do better than just "text"
19-
case $(file --mime-type --uncompress $file_common_opts "$file") in
20-
application/xml|image/svg+xml) lexer=xml;;
21-
application/javascript) lexer=javascript;;
22-
text/html) lexer=html;;
23-
text/troff) lexer=nroff;;
24-
text/x-asm) lexer=nasm;;
25-
text/x-awk) lexer=awk;;
26-
text/x-c) lexer=c;;
27-
text/x-c++) lexer=cpp;;
28-
text/x-crystal) lexer=crystal;;
29-
text/x-diff) lexer=diff;;
30-
text/x-fortran) lexer=fortran;;
31-
text/x-gawk) lexer=gawk;;
32-
text/x-java) lexer=java;;
33-
text/x-lisp) lexer=common-lisp;;
34-
text/x-lua) lexer=lua;;
35-
text/x-makefile) lexer=make;;
36-
text/x-msdos-batch) lexer=bat;;
37-
text/x-nawk) lexer=nawk;;
38-
text/x-pascal) lexer=pascal;;
39-
text/x-perl) lexer=perl;;
40-
text/x-php) lexer=php;;
41-
text/x-po) lexer=po;;
42-
text/x-python) lexer=python;;
43-
text/x-ruby) lexer=ruby;;
44-
text/x-shellscript) lexer=sh;;
45-
text/x-tcl) lexer=tcl;;
46-
text/x-tex|text/x-texinfo) lexer=latex;; # FIXME: texinfo really needs its own lexer
47-
48-
# Types that file outputs which pygmentize didn't support as of file 5.20, pygments 2.0
49-
# text/calendar
50-
# text/inf
51-
# text/PGP
52-
# text/rtf
53-
# text/texmacs
54-
# text/vnd.graphviz
55-
# text/x-bcpl
56-
# text/x-info
57-
# text/x-m4
58-
# text/x-vcard
59-
# text/x-xmcd
60-
61-
text/plain) # special filenames. TODO: insert more
62-
case $(basename "$file") in
63-
.zshrc) lexer=sh;;
64-
esac
65-
;;
66-
esac
67-
fi
16+
case $(file --mime-type --uncompress $file_common_opts "$file") in
17+
application/xml|image/svg+xml) lexer=xml;;
18+
application/javascript) lexer=javascript;;
19+
application/json) lexer=json;;
20+
text/html) lexer=html;;
21+
text/troff) lexer=nroff;;
22+
text/x-asm) lexer=nasm;;
23+
text/x-awk) lexer=awk;;
24+
text/x-c) lexer=c;;
25+
text/x-c++) lexer=cpp;;
26+
text/x-crystal) lexer=crystal;;
27+
text/x-diff) lexer=diff;;
28+
text/x-fortran) lexer=fortran;;
29+
text/x-gawk) lexer=gawk;;
30+
text/x-java) lexer=java;;
31+
text/x-lisp) lexer=common-lisp;;
32+
text/x-lua) lexer=lua;;
33+
text/x-makefile) lexer=make;;
34+
text/x-msdos-batch) lexer=bat;;
35+
text/x-nawk) lexer=nawk;;
36+
text/x-pascal) lexer=pascal;;
37+
text/x-perl) lexer=perl;;
38+
text/x-php) lexer=php;;
39+
text/x-po) lexer=po;;
40+
text/x-python) lexer=python;;
41+
text/x-ruby) lexer=ruby;;
42+
text/x-shellscript) lexer=sh;;
43+
text/x-tcl) lexer=tcl;;
44+
text/x-tex|text/x-texinfo) lexer=latex;; # FIXME: texinfo really needs its own lexer
45+
46+
# Types that file outputs which pygmentize didn't support as of file 5.20, pygments 2.0
47+
# text/calendar
48+
# text/inf
49+
# text/PGP
50+
# text/rtf
51+
# text/texmacs
52+
# text/vnd.graphviz
53+
# text/x-bcpl
54+
# text/x-info
55+
# text/x-m4
56+
# text/x-vcard
57+
# text/x-xmcd
58+
59+
text/plain) # special filenames. TODO: insert more
60+
case $(basename "$file") in
61+
.zshrc) lexer=sh;;
62+
esac
63+
# pygmentize -N is much cheaper than file, but makes some bad guesses (e.g.
64+
# it guesses ".pl" is Prolog, not Perl)
65+
lexer=$(pygmentize -N "$file")
66+
;;
67+
esac
6868

6969
# Find a concatenator for compressed files
7070
concat=cat

0 commit comments

Comments
 (0)