forked from n8gray/QLColorCode
-
Notifications
You must be signed in to change notification settings - Fork 4
/
colorize.sh
executable file
·108 lines (94 loc) · 2.77 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
#!/bin/zsh
# 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.
# 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
rsrcDir=$1
target=$2
thumb=$3
debug () {
if [ "x$qlcc_debug" != "x" ]; then if [ "x$thumb" = "x0" ]; then
echo "QLColorCode: $@" 1>&2
fi; fi
}
debug Starting colorize.sh
#echo target is $target
hlDir=/usr/local/share/highlight
cmd=/usr/local/bin/highlight
cmdOpts=(-I -k "$font" -K $fontSizePoints --quiet -I \
--style=${hlTheme} \
--encoding $textEncoding ${=extraHLFlags} --validate-input)
#for o in $cmdOpts; do echo $o\<br/\>; done
debug Setting reader
reader=(cat $target)
debug Handling special cases
case $target in
*.graffle )
# some omnigraffle files are XML and get passed to us. Ignore them.
exit 1
;;
*.plist )
lang=xml
reader=(/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##*.}
;;
esac
debug Resolved $target to language $lang
go4it () {
debug Generating the preview
local title=`basename ${target}`
if [ $thumb = "1" ]; then
$reader | head -n 100 | head -c 20000 | $cmd -S $lang $cmdOpts && exit 0
elif [ -n "$maxFileSize" ]; then
$reader | head -c $maxFileSize | $cmd -T "${title}" -S $lang $cmdOpts && exit 0
else
$reader | $cmd -S $lang -T ${title} $cmdOpts && exit 0
fi
}
setopt no_err_exit
debug First try...
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