forked from anthonygelibert/QLColorCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolorize.sh
More file actions
executable file
·158 lines (146 loc) · 3.77 KB
/
colorize.sh
File metadata and controls
executable file
·158 lines (146 loc) · 3.77 KB
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
#!/usr/bin/env zsh -f
###############################################################################
# This code is licensed under the GPL v3. See LICENSE.txt for details.
#
# Copyright 2007 Nathaniel Gray.
# Copyright 2012-2018 Anthony Gelibert.
#
# Expects $1 = path to resources dir of bundle
# $2 = name of file to colorize
# $3 = 1 if you want enough for a thumbnail, 0 for the full file
#
# Produces HTML on stdout with exit code 0 on success
###############################################################################
# Fail immediately on failure of sub-command
setopt err_exit
# Set the read-only variables
rsrcDir="$1"
target="$2"
thumb="$3"
cmd="$pathHL"
function debug() {
if [ "x$qlcc_debug" != "x" ]; then
if [ "x$thumb" = "x0" ]; then
echo "QLColorCode: $@" 1>&2
fi;
fi
}
debug "Starting colorize.sh by setting reader"
reader=(cat ${target})
debug "Handling special cases"
case ${target} in
*.graffle | *.ps )
exit 1
;;
*.iml )
lang=xml
;;
*.d )
lang=make
;;
*.fxml )
lang=fx
;;
*.s | *.s79 )
lang=assembler
;;
*.sb )
lang=lisp
;;
*.java )
lang=java
plugin=(--plug-in java_library)
;;
*.pde | *.ino )
lang=c
;;
*.c | *.cpp | *.ino )
lang=${target##*.}
plugin=(--plug-in cpp_syslog --plug-in cpp_ref_cplusplus_com --plug-in cpp_ref_local_includes)
;;
*.rdf | *.xul | *.ecore )
lang=xml
;;
*.pyc )
lang=python
reader=(/usr/local/bin/uncompyle6 ${target})
;;
*.ascr | *.scpt )
lang=applescript
reader=(/usr/bin/osadecompile ${target})
;;
*.plist )
lang=xml
reader=(/usr/bin/plutil -convert xml1 -o - ${target})
;;
*.sql )
if grep -q -E "SQLite .* database" <(file -b ${target}); then
exit 1
fi
lang=sql
;;
*.m )
lang=objc
;;
*.am | *.in )
lang=make
;;
*.mod )
lang=go
;;
*.pch | *.h )
if grep -q "@interface" <(${target}) &> /dev/null; then
lang=objc
else
lang=h
fi
;;
*.pl )
lang=pl
plugin=(--plug-in perl_ref_perl_org)
;;
*.py )
lang=py
plugin=(--plug-in python_ref_python_org)
;;
*.sh | *.zsh | *.bash | *.csh | *.bashrc | *.zshrc | *.xsh | *.bats )
lang=sh
plugin=(--plug-in bash_functions)
;;
*.scala )
lang=scala
plugin=(--plug-in scala_ref_scala_lang_org)
;;
*.cfg | *.properties | *.conf )
lang=ini
;;
*.kmt )
lang=scala
;;
*.adoc )
lang=asciidoc
;;
* )
lang=${target##*.}
;;
esac
debug "Resolved ${target} to language $lang"
go4it () {
cmdOpts=(--plug-in reduce_filesize ${plugin} --plug-in outhtml_codefold --syntax=${lang} --quiet --include-style --font=${font} --font-size=${fontSizePoints} --style=${hlTheme} --encoding=${textEncoding} ${=extraHLFlags} --validate-input)
debug "Generating the preview"
if [ "${thumb}" = "1" ]; then
${reader} | head -n 100 | head -c 20000 | ${cmd} -D "${rsrcDir}" ${cmdOpts} && exit 0
elif [ -n "${maxFileSize}" ]; then
${reader} | head -c ${maxFileSize} | ${cmd} -D "${rsrcDir}" -T "${target}" ${cmdOpts} && exit 0
else
${reader} | ${cmd} -D "${rsrcDir}" -T "${target}" ${cmdOpts} && exit 0
fi
}
setopt no_err_exit
go4it
# Uh-oh, it didn't work. Fall back to rendering the file as plain
debug "First try failed, second try..."
lang=txt
go4it
debug "Reached the end of the file. That should not happen."
exit 101