-
Notifications
You must be signed in to change notification settings - Fork 0
/
lessfilter.sh
executable file
·181 lines (156 loc) · 4.65 KB
/
lessfilter.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/sh
# lessfilter
#
# Author: Brian A. Onn
# License: MIT/X11
# use with
#
# export LESS="-R"
# export LESSOPEN="|~/.lessfilter"
#
# requirements: install the following before you use it
# hexdump
# pandoc
# pygments
# gzip
# bzip2
# tar
# vue-lexer (for .vue files)
#
# TODO: There is only two hardcoded style choices right now. Ideally,
# the style should be selected based on mime-type or file extension
#
# TODO: add LESSFILTER_HTML_VIEWER env variable to use "lynx" or "firefox" or "chrome" , etc
default_style="lightbulb" #zenburn
bash_style="lightbulb"
hexdump=$(which hexdump)
##OFF## htmlviewer=$(which webview)
##OFF## [ -n "$LESSFILTER_HTML_VIEWER" ] && htmlviewer="$LESSFILTER_HTML_VIEWER"
case "$1" in
# man pages
*.[1-9] | *.[1-9][a-z])
groff -T utf8 -man -t "$1"
exit 0
;;
# compressed man pages
*.[1-9].gz | *.[1-9][a-z].gz)
gzip -dc "$1" | groff -T utf8 -man -t
exit 0
;;
##OFF## # markdown or ReStructured Text
##OFF## *.[mM][dD]|*.markdown|*.rst)
##OFF## if [[ -r $HOME/.pandoc.css ]]; then
##OFF## pandoc_css="--embed-resources --standalone --css=$HOME/.pandoc.css"
##OFF## else
##OFF## pandoc_css=""
##OFF## fi
##OFF## if [[ -n "$htmlviewer" ]] ; then
##OFF## pandoc $pandoc_css --metadata title=$(basename "$1") -s -t html "$1" | $htmlviewer
##OFF## else
##OFF## pandoc $pandoc_css --metadata title=$(basename "$1") -s -t html "$1"
##OFF## fi
##OFF## exit 0
##OFF## ;;
# markdown or ReStructured Text
*.[mM][dD]|*.markdown|*.rst)
pygmentize -f 16m -O style=${default_style} "$1"
exit 0
;;
# syntax highlighting using Pygments. Pygments handles quite a lot !
# pip install jsx-lexer
Dockerfile|*.[ch]|*.[ch]pp|*.[ch]xx|*.cc|*.hh|*.go|*.py|*.pl|*.R|*.asm|*.java| \
*.awk|*.sql|*.el|*.clj|*.nim| *.pas|*.p| *.php | *.f | *.lua | \
*.fortran | *.fth | *.4th | *.patch | *.diff | *.css | *.rs | *.vue | \
*.js|*.jsx|*.scss|*.jade|*.htm|*.html|*.json|*.yml|*.yaml|*.v|*.sv | *.vim | *.toml)
pygmentize -f 16m -O style=${default_style} "$1"
exit 0
;;
# ini files and conf files
*.ini|*.conf|*sshd_config*)
pygmentize -f 16m -l ini -O style=${default_style} "$1"
exit 0
;;
# Vagrantfile, Capfile, Rakefile, Gemfile, Guardfile, *.rb, *.ru is ruby
[vV]agrantfile | [Cc]apfile | [Rr]akefile | [Gg]emfile | [Gg]uardfile | *.rb | *.ru)
pygmentize -f 16m -l ruby -O style="${default_style}" "$1"
exit 0
;;
# makefiles
[mM]akefile*|*.mak|*.make)
pygmentize -f 16m -l Makefile -O style="${default_style}" "$1"
exit 0
;;
# makefiles
CMakeList*.txt|*.cmake)
pygmentize -f 16m -l cmake -O style="${default_style}" "$1"
exit 0
;;
# sh and bash using Pygments
*.sh|.profile|*.bash*)
pygmentize -f 16m -l sh -O style="${bash_style}" "$1"
exit 0
;;
# xml and policy-kit using Pygments
*.xml|*.policy)
pygmentize -f 16m -l xml -O style="${default_style}" "$1"
exit 0
;;
# library archive
*.ar|*.a) ar tv "$1" && exit 0 ;;
# compressed log
*.log.gz) gzip -dc "$1" && exit 0 ;;
*.log.bz|*.log.bz2|*.txt.bz|*.txt.bz2) bzip2 -dc "$1" && exit 0 ;;
# tar gz
*.tar.gz|*.tgz) tar -tvzf "$1" && exit 0 ;;
# no lexer
*.m4) cat "$1" # placeholder. Pygments has no lexer for m4 yet
exit 0
;;
# check the mime type last
# TODO: maybe classify all files first?
*)
mimetype=$(file -b -i -L "$1" 2>/dev/null )
case ${mimetype} in
*text/xml*|*/*+xml*)
pygmentize -f 16m -l xml -O style="${default_style}" "$1"
exit 0
;;
*text/lisp*|*text/x-lisp*)
pygmentize -f 16m -l lisp -O style="${bash_style}" "$1"
exit 0
;;
*application/zip*binary)
unzip -lv "$1"
exit 0
;;
*application/x-tar*binary|*application/tar*binary)
tar tvf "$1"
exit 0
;;
*inode/x-empty*)
echo "'$1': is empty (size is 0 bytes)."
exit 0
;;
# catch all other binaries here
*charset*binary )
file "$1" 2>/dev/null
echo
if test "1${hexdump}" = "1"; then
echo "I need the hexdump(1) program to view binary files"
echo "You should install it"
echo
cat -v "$1"
exit 1
fi
${hexdump} -C "$1"
exit 0
;;
esac
esac
head -1 "$1" | egrep '(#!/bin/sh|#!/bin/bash|#!/usr/bin/env.*(ba)?sh)' > /dev/null 2>&1
if test $? -eq 0; then
pygmentize -f 16m -l sh -O style="${bash_style}" "$1"
else
exit 1
fi
exit 0