Add epub to html conversion
[llpp.git] / misc / llppac
blob1c6e4a734f03f2eb99cbba7d212dc59d5e62fdf6
1 #!/bin/sh
2 set -e
4 die() {
5 echo "$1" >&2
6 exit 1
9 cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/llpp"
10 test -d "$cachedir" || die "cache directory '$cachedir' does not exist"
12 caspsuf=
13 type=
15 executable_p() {
16 command -v "$1" >/dev/null 2>&1
19 missing() {
20 executable_p $1 || \
21 eval "$1() { die \"$2 is needed for \$type conversion\"; }"
24 maketext() {
25 test -d "$cachedir/fonts" || mkdir "$cachedir/fonts"
26 cat >"$cachedir/fonts/text" <<EOF
27 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
28 eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
29 minim veniam, quis nostrud exercitation ullamco laboris nisi ut
30 aliquip ex ea commodo consequat. Duis aute irure dolor in
31 reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
32 pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
33 culpa qui officia deserunt mollit anim id est laborum.
34 Posting Soccer Goals On Vine Is Illegal, Say England's Premier League
35 abcdefghizklmnopqrstwxyz
36 ABCDEFGHIZKLMNOPQRSTWXYZ
37 \`1234567890-=\\
38 ~!@#\$%^&*()_+|
39 [];',./
40 {}:"<>?"
42 EOF
45 trap 'test -n "$casp" && rm -f "$casp"' 0
47 while getopts m:t:f opt; do
48 case $opt in
49 m) mime=$OPTARG;;
50 t) type=$OPTARG;;
51 f) force=1;;
52 ?) die "usage: $0 [-m mime/type] [-t filter] [-f] [path|url]";;
53 esac
54 done
55 shift $(($OPTIND - 1))
56 test -z "$1" && die "usage $0: path"
58 if test ! -e "$1" && expr >/dev/null "$1" : "\(ftp\|https\?\)://"; then
59 if executable_p wget; then
60 dl() {
61 wget -q -c -nc $1 -O $2
63 elif executable_p curl; then
64 dl() {
65 curl $1 -o $2
67 else
68 die "no program to fetch remote urls found"
71 md5of="$cachedir/$(basename "$1")"
72 dl "$1" "$md5of" || test -e "$md5of"
73 shift
74 set -- "$md5of" "$@"
75 else
76 md5of="$1"
79 test -z "$type" && {
80 ft=$(file -L --mime-type -b "$1") || die "$ft"
83 case $ft in
84 application/x-gzip | application/x-compress) dc=zcat;;
85 application/x-xz) dc=xzcat;;
86 application/x-bzip2) dc=bzcat;;
87 *) unset dc || true;;
88 esac
90 filt='"${dc-cat}" "$1" |'
92 if test -z "$type"; then
93 test -z "$mime" && mime=$(file -L --mime-type -bz "$1" || die "$mime")
94 case $mime in
95 application/postscript) type=ps;;
96 application/pdf) type=pdf;;
97 image/vnd.djvu) type=djvu;;
98 text/html) type=html;;
99 text/plain) type=text;;
100 application/msword) type=word;;
101 application/vnd.openxmlformats-officedocument.* \
102 | application/vnd.ms-powerpoint \
103 | application/vnd.ms-excel \
104 | application/vnd.oasis.opendocument.*) type=uno;;
105 image/svg+xml) type=svg;;
106 image/png | image/jpeg) test -n "$dc" && type="image" || type="image2";;
107 image/*) type=image;;
108 application/x-dvi) type=dvi;;
109 application/x-font-ttf | application/vnd.ms-opentype) type=font;;
110 *) die "unhandled file type: '$mime'";;
111 esac
114 case $type in
115 ps) conv='ps2pdf - "$casp"';;
116 image2|pdf) test -z "$dc" && exec llpp "$@" || conv='cat >"$casp"';;
117 djvu) conv='ddjvu -format=pdf - "$casp"';;
118 html) {
119 missing prince "PrinceXML(http://www.princexml.com/)"
120 conv='prince -s /home/malc/xsrc/llpp/misc/rr.css - -o "$casp"'
122 html2epub) {
123 missing pandoc "pandoc(http://pandoc.org)"
124 caspsuf=".epub"
125 conv='pandoc - -w epub -o "$casp"'
127 word) {
128 if executable_p unoconv && test -z "$dc"; then
129 unset filt
130 caspsuf=.pdf
131 conv='unoconv -o "$casp" "$1"'
132 else
133 missing antiword "antiword or unoconv"
134 conv='antiword -m 8859-1.txt -a a4 - >"$casp"'
137 uno) {
138 test -n "$dc" && die "cannot convert compressed '$mime'"
139 unset filt
140 missing unoconv "unoconv(http://dag.wiee.rs/home-made/unoconv/)"
141 conv='unoconv -o "$casp" "$1"'
142 caspsuf=.pdf
144 svg) {
145 if executable_p inkscape && test -z "$dc"; then
146 unset filt
147 conv='inkscape -z -A "$casp" "$1"'
148 else
149 missing rsvg-convert "rsvg-convert"
150 conv='rsvg-convert -f pdf -o "$casp"'
153 font) {
154 if test -e "$2"; then
155 arg="$2"
156 else
157 arg="--waterfall $cachedir/fonts/text"
158 maketext
160 # https://mail.gnome.org/archives/gtk-i18n-list/2010-June/msg00000.html
161 eval $filt cat >$cachedir/fonts/font
162 cat >$cachedir/fonts/fonts.conf <<EOF
163 <?xml version="1.0"?>
164 <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
165 <fontconfig>
166 <dir>$cachedir/fonts/</dir>
167 <cachedir>$cachedir/fonts/</cachedir>
168 <config></config>
169 </fontconfig>
171 unset filt
172 missing pango-view "pango-view(http://www.pango.org/)"
173 conv='FONTCONFIG_PATH=$cachedir/fonts/ pango-view -q $arg -o $casp'
174 caspsuf=.pdf
176 fcfont) {
177 if test -e "$2"; then
178 arg="$2"
179 else
180 arg="--waterfall $cachedir/fonts/text"
181 maketext
183 unset filt
184 missing pango-view "pango-view(http://www.pango.org/)"
185 conv='pango-view --font="$1" -q $arg -o $casp'
186 caspsuf=.pdf
187 md5of=/dev/null
188 force=1
190 image) {
191 missing convert "convert(http://www.imagemagick.org/script/convert.php)"
192 conv='convert - pdf:"$casp"'
194 dvi) {
195 test -n "$dc" && die "cannot convert compressed '$mime'"
196 unset filt
197 missing dvipdf "dvipdf(http://ghostscript.com/)"
198 conv='dvipdf "$1" "$casp"'
200 text) {
201 missing pandoc "pandoc(http://pandoc.org/)"
202 conv='pandoc -t epub - -o "$casp"'
203 caspsuf=.epub
205 *) die "unhandled filter type: '$type'";;
206 esac
208 hash=$(md5sum "$md5of") || die "$hash"
209 casp=$cachedir/${hash%% *}$caspsuf
210 (test -n "$force" -o ! -e "$casp") && eval "$filt" "$conv"
212 exec llpp -origin "$@" "$casp"