Debian Codegraph @

external/autopygmentize

raw
1 #!/bin/bash
2 # Best effort auto-pygmentization with transparent decompression
3 # by Reuben Thomas 2008-2025
4 # This program is in the public domain.
5
6 # Strategy: first see if pygmentize can find a lexer; if not, ask file; if that finds nothing, fail
7 # Set the environment variable PYGMENTIZE_OPTS or pass options before the file path to configure pygments.
8
9 # This program can be used as a .lessfilter for the less pager to auto-color less's output
10
11 file="${!#}" # last argument
12 options=${@:1:$(($#-1))} # handle others args as options to pass to pygmentize
13
14 file_common_opts="--brief --dereference"
15
16 # Better command for XDG-compatible systems: xdg-mime query filetype "$file"
17 case $(file --mime-type --uncompress $file_common_opts) in
18 application/javascript) lexer=javascript;;
19 application/json) lexer=json;;
20 */html|*/xhtml+xml) lexer=html;;
21 text/troff) lexer=nroff;;
22 text/x-asm) lexer=nasm;;
23 */x-awk) lexer=awk;;
24 text/x-c|text/x-csrc|text/x-chdr) lexer=c;;
25 text/x-c++|text/x-c++src) lexer=cpp;;
26 */x-clojure) lexer=clojure;;
27 text/x-crystal) lexer=crystal;;
28 text/css) lexer=css;;
29 text/x-diff) lexer=diff;;
30 */x-execline) lexer=execline;;
31 text/x-forth) lexer=forth;;
32 text/x-fortran) lexer=fortran;;
33 */x-gawk) lexer=gawk;;
34 */x-go) lexer=go;;
35 text/x-java) lexer=java;;
36 */x-lisp) lexer=common-lisp;;
37 */x-emacs-lisp) lexer=emacs-lisp;;
38 */x-lua|*/x-luatex) lexer=lua;;
39 text/x-makefile) lexer=make;;
40 */x-msdos-batch) lexer=bat;;
41 */x-nawk) lexer=nawk;;
42 text/x-objective-c) lexer=objective-c;;
43 */x-pascal) lexer=pascal;;
44 */x-perl) lexer=perl;;
45 */x-php) lexer=php;;
46 */x-po) lexer=po;;
47 */x-python*|*/x-script.python) lexer=python;;
48 */x-ruby) lexer=ruby;;
49 text/rust) lexer=rust;;
50 */x-shellscript) lexer=sh;;
51 # Workaround for TypeScript: mimetype doesn't recognize it.
52 text/vnd.trolltech.linguist) lexer=typescript;;
53 */x-tcl) lexer=tcl;;
54 text/x-tex|text/x-texinfo) lexer=latex;; # FIXME: texinfo really needs its own lexer
55 */x-ursa) lexer=ursa;;
56 */x-vala) lexer=vala;;
57 */xml|image/svg+xml) lexer=xml;;
58 text/vnd.graphviz) lexer=graphviz;;
59
60 # Types that file outputs which pygmentize didn't support as of file 5.41, pygments 2.11.2
61 # text/binary
62 # text/calendar
63 # text/PGP
64 # text/prs.lines.tag
65 # text/rtf
66 # text/spreadsheet
67 # text/texmacs
68 # text/vcard
69 # text/vnd.sosi
70 # text/x-Algol68
71 # text/x-bcpl
72 # text/x-dmtf-mif
73 # text/x-gimp-curve
74 # text/x-gimp-ggr
75 # text/x-gimp-gpl
76 # text/x-info
77 # text/x-installshield-lid
78 # text/x-m4
79 # text/x-modulefile
80 # text/x-ms-adm
81 # text/x-ms-cpx
82 # text/x-ms-regedirt
83 # text/x-ms-tag
84 # text/x-systemtap
85 # text/x-vcard
86 # text/x-wine-extension-reg
87 # text/x-xmcd
88
89 *) # special filenames. TODO: insert more
90 case $(basename "$file") in
91 .zshrc) lexer=sh;;
92 esac
93 # pygmentize -N is much cheaper than file, but makes some bad guesses (e.g.
94 # it guesses ".pl" is Prolog, not Perl)
95 lexer=$(pygmentize -N "$file")
96 ;;
97 esac
98
99 # Find a concatenator for compressed files
100 concat=
101 concat_opts=
102 case $(file $file_common_opts --mime-type "$file") in
103 # TODO: add support
104 # application/x-rzip (does not decompress to stdout)
105 # application/x-dzip (Windows only)
106 application/gzip|application/x-gzip) concat=zcat;;
107 application/x-bzip) concat=bzip; concat_opts=-dc;;
108 application/x-bzip2) concat=bzcat;;
109 application/x-lz4) concat=lz4; concat_opts=-dc;;
110 application/x-lzh-compressed) concat=p7zip; concat_opts=-dc;;
111 application/x-lzma) concat=lzcat;;
112 application/x-lzip) concat=lzip; concat_opts=-dc;;
113 application/x-xz) concat=xzcat;;
114 application/x-zoo) concat=zoo; concat_opts=fu;;
115 esac
116 # If concat is unset or doesn't exist, use cat instead
117 if [[ "$concat" == "" ]] || ! command -v "$concat"; then
118 concat=cat
119 concat_opts=
120 fi
121
122 # Find a suitable reader, preceded by a hex dump for binary files,
123 # or fmt for text with very long lines
124 prereader=""
125 reader=cat
126 encoding=$(file --mime-encoding --uncompress $file_common_opts "$file")
127 # FIXME: need a way to switch between hex and text view, as file often
128 # misdiagnoses files when they contain a few control characters
129 # if [[ $encoding == "binary" ]]; then
130 # prereader="od -x" # POSIX fallback
131 # if [[ -n $(which hd) ]]; then
132 # prereader=hd # preferred
133 # fi
134 # lexer=hexdump
135 # encoding=latin1
136 #el
137 # FIXME: Using fmt does not work well for system logs
138 # if [[ "$lexer" == "text" ]]; then
139 # if file "$file" | grep -ql "text, with very long lines"; then
140 # reader=fmt
141 # fi
142 # fi
143 if [[ "$lexer" != "text" ]]; then
144 reader="pygmentize -O inencoding=$encoding $PYGMENTIZE_OPTS $options -l $lexer"
145 fi
146
147 # Run the reader
148 if [[ -n "$prereader" ]]; then
149 exec $concat "$file" | $prereader | $reader
150 else
151 exec $concat "$file" | $reader
152 fi