cruft
·
2009-11-18
mkepub
1#! /bin/sh
2
3kersplit () {
4 first=1
5 # Add an extra line to chop
6 parts=$( (cat horrors2.xhtml; echo) | sed -n '/class="part/=;$=')
7 for part in $parts; do
8 fn=$1; shift
9 cat head.xhtml > $fn
10 last=$(expr $part - 1)
11 sed -n "${first},${last}p" horrors2.xhtml >> $fn
12 cat foot.xhtml >> $fn
13 first=$part
14 done
15}
16
17xhtml="epub/book.xhtml epub/part1.xhtml epub/part2.xhtml epub/part3.xhtml epub/part4.xhtml epub/part5.xhtml epub/part6.xhtml epub/part7.xhtml"
18kersplit $xhtml
19
20
21rm -rf epub/art
22if true; then
23 mkdir epub/art
24 for i in art/*.png; do
25 echo $i
26 pngtopnm $i | pnmscale -xysize 800 600 | pnmtopng > epub/$i
27 done
28 for i in art/*.jpg; do
29 echo $i
30 jpegtopnm $i | pnmscale -xysize 800 600 | pnmtojpeg > epub/$i
31 done
32fi
33cp style.css epub/
34
35
36
37cd epub
38echo -n application/epub+zip > mimetype
39cat >MANIFEST <<EOF
40mimetype
41META-INF/container.xml
42content.opf
43EOF
44cat > content.opf <<EOF
45<?xml version="1.0"?>
46<package version="2.0" xmlns="http://www.idpf.org/2007/opf" unique-identifier="BookId">
47<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
48 <dc:title>Horrors 2</dc:title>
49 <dc:language>en</dc:language>
50 <dc:identifier id="BookId" opf:scheme="title">Horrors2</dc:identifier>
51 <dc:creator opf:file-as="Something Awful Forums" opf:role="aut">The Something Awful Forums</dc:creator>
52</metadata>
53
54<manifest>
55EOF
56rm spine
57find . -type f | sed 's#./##' | while read fn; do
58 b=$(basename $fn)
59 id=thing$count
60 count=$(expr $count + 1)
61 case "$b" in
62 META-INF/*|MANIFEST|mimetype|content.opf|spine)
63 continue
64 ;;
65 *.xhtml)
66 id=$(basename $b .xhtml)
67 type=application/xhtml+xml
68 echo $id >> spine
69 ;;
70 *.css)
71 type=image/css
72 ;;
73 book.ncx)
74 id=ncx
75 ;;
76 *.png)
77 type=image/png
78 ;;
79 *.jpg)
80 type=image/jpeg
81 ;;
82 *.svg)
83 type=image/svg+xml
84 ;;
85 *.otf)
86 type=application/octet-stream
87 ;;
88 esac
89 echo "<item id=\"$id\" href=\"$fn\" media-type=\"$type\"/>" >> content.opf
90 echo $fn >> MANIFEST
91done
92cat >> content.opf <<EOF
93</manifest>
94
95<spine toc="ncx">
96EOF
97sort spine | while read id; do
98 # "book" sorts before "part*" :)
99 echo " <itemref idref=\"$id\" />" >> content.opf
100done
101rm spine
102cat >> content.opf <<EOF
103</spine>
104 </package>
105EOF
106
107xargs zip -Xr9D ../book.epub < MANIFEST