Debian Codegraph @

external/autopygmentize

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