forked from n8gray/QLColorCode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
colorize.sh
executable file
·146 lines (125 loc) · 3.39 KB
/
colorize.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
#!/bin/bash
# This code is licensed under the GPL v2. See LICENSE.txt for details.
# colorize.sh
# QLColorCode
#
# Created by Nathaniel Gray on 11/27/07.
# Copyright 2007 Nathaniel Gray, Jaeho Shin.
# 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
# Also fail whenever undefined variables are used
set -eu
: \
${qlcc_debug:=} \
${qlcc_text_fallback:=false} \
${maxFileSize:=} \
${extraHLFlags:=} \
#
RsrcDir=$1
Target=$2
case $3 in
1) Thumb=true ;;
0|*) Thumb=false ;;
esac
debug() { [ -z "$qlcc_debug" ] || ! $Thumb || echo >&2 "QLColorCode: $@"; }
debug Starting colorize.sh
read-target() { cat "$Target"; }
# Define how we invoke highlight
# (See: http://www.andre-simon.de/doku/highlight/en/highlight.html)
lang=
highlightOpts=(
--quiet
--validate-input
--encoding "$textEncoding"
--doc-title "${Target##*/}"
--include-style
--style "$hlTheme"
--font "$font"
--font-size "$fontSizePoints"
--data-dir "$RsrcDir"/highlight/share/highlight
--add-config-dir "$RsrcDir"/etc/highlight
)
invoke-highlight() {
set -- "${highlightOpts[@]}" $extraHLFlags "$@"
if [ -n "$lang" ]; then
set -- --syntax "$lang" "$@"
fi
"$RsrcDir"/highlight/bin/highlight "$@"
}
case ${Target##*/} in
*.graffle )
# some omnigraffle files are XML and get passed to us. Ignore them.
exit 1
;;
*.plist )
lang=xml
read-target() { /usr/bin/plutil -convert xml1 -o - "$Target"; }
;;
*.h )
if grep -q "@interface" "$Target" &>/dev/null; then
lang=objc
else
lang=h
fi
;;
*.m )
# look for a matlab-style comment in the first 10 lines, otherwise
# assume objective-c. If you never use matlab or never use objc,
# you might want to hardwire this one way or the other
if head -n 10 "$Target" | grep -q "^[ ]*%" &>/dev/null; then
lang=m
else
lang=objc
fi
;;
*.pro )
# Can be either IDL or Prolog. Prolog uses /* */ and % for comments.
# IDL uses ;
if head -n 10 "$Target" | grep -q "^[ ]*;" &>/dev/null; then
lang=idlang
else
lang=pro
fi
;;
*.* )
lang=${Target##*.}
;;
Makefile )
lang=make
;;
LICENSE |\
COPYING |\
README )
lang=txt
;;
esac
debug Resolved $Target to language $lang
generate-preview () {
debug Generating the preview
if $Thumb; then
head -n 100 | head -c 20000 |
invoke-highlight
elif [ -n "$maxFileSize" ]; then
head -c "$maxFileSize" |
invoke-highlight
else
invoke-highlight
fi
}
if ! read-target | generate-preview; then
# Uh-oh, it didn't work, see if we can fallback to text
if $qlcc_text_fallback || [[ $(file --brief --mime-type "$Target") = text/* ]]; then
# Fallback to rendering as plain text
debug First try failed, fallback to plain text
lang=txt
read-target | generate-preview
else
# Let other QuickLook generator handle
exit 2
fi
fi