forked from standardebooks/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint-manifest-and-spine
executable file
·111 lines (90 loc) · 4.09 KB
/
print-manifest-and-spine
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
#!/bin/bash
usage(){
fmt <<EOF
DESCRIPTION
Create a <manifest> and <spine> tag for content.opf based on the passed Standard Ebooks source directory and print to standard output.
USAGE
print-manifest-and-spine DIRECTORY [DIRECTORY...]
DIRECTORY is the source directory, which must contain DIRECTORY/images/ and DIRECTORY/src/.
EOF
exit
}
die(){ printf "Error: ${1}\n" 1>&2; exit 1; }
require(){ command -v $1 > /dev/null 2>&1 || { suggestion=""; if [ ! -z "$2" ]; then suggestion=" $2"; fi; die "$1 is not installed.${suggestion}"; } }
if [ $# -eq 1 ]; then if [ "$1" = "--help" -o "$1" = "-h" ]; then usage; fi fi
#End boilerplate
dirs=""
while [ $# -gt 0 ]
do
case "$1" in
*)
dirs=$(printf "%s\n%s" "${dirs}" "$1")
;;
esac
shift
done
printf "%s\n" "${dirs}" | while IFS= read -r i;
do
if [ "${i}" = "" ]; then
continue
fi
cd "${curDir}" #Reset when starting the loop over
srcDir="$(realpath "${i%/}")"
if [ ! -d "${srcDir}/src" ]; then
die "${srcDir} doesn't look like a Standard Ebook source directory."
fi
#Build the manifest
echo -e "\t<manifest>" > /tmp/manifest
echo -e "\t\t<item href=\"toc.xhtml\" id=\"toc.xhtml\" media-type=\"application/xhtml+xml\" properties=\"nav\"/>" >> /tmp/manifest
#Get the CSS
ls -v "${srcDir}/src/epub/css/" | sed --regexp-extended "
s|(.*)|\t\t<item href=\"css/\1\" id=\"\1\" media-type=\"text/css\"/>|g
" >> /tmp/manifest
#Get all of our images
ls -v "${srcDir}/src/epub/images/" | sed --regexp-extended "
s|(.*)|\t\t<item href=\"images/\1\" id=\"\1\"/>|g
s|id=\"(.*\.svg)\"|id=\"\1\" media-type=\"image/svg+xml\"|g
s|id=\"(.*\.png)\"|id=\"\1\" media-type=\"image/png\"|g
s|id=\"(.*\.jpg)\"|id=\"\1\" media-type=\"image/jpeg\"|g
s|id=\"cover.svg\" media\-type=\"image/svg\+xml\"|id=\"cover.svg\" media-type=\"image/svg+xml\" properties=\"cover-image\"|g
" >> /tmp/manifest
#Get all of our text documents
ls -v "${srcDir}/src/epub/text/" | sed --regexp-extended "
s|(.*)|\t\t<item href=\"text/\1\" id=\"\1\" media-type=\"application/xhtml+xml\"/>|g
s|id=\"titlepage.xhtml\" media\-type=\"application/xhtml\+xml\"|id=\"titlepage.xhtml\" media-type=\"application/xhtml+xml\" properties=\"svg\"|g
s|id=\"colophon.xhtml\" media\-type=\"application/xhtml\+xml\"|id=\"colophon.xhtml\" media-type=\"application/xhtml+xml\" properties=\"svg\"|g
" >> /tmp/manifest
echo -e "\t</manifest>" >> /tmp/manifest
cat /tmp/manifest
#Build the spine
echo -e "\t<spine>" > /tmp/spine
echo -e "\t\t<itemref idref=\"titlepage.xhtml\"/>\n\t\t<itemref idref=\"imprint.xhtml\"/>" >> /tmp/spine
ls "${srcDir}/src/epub/text/" | grep --quiet "dedication.xhtml" && echo -e "\t\t<itemref idref=\"dedication.xhtml\"/>" >> /tmp/spine
ls "${srcDir}/src/epub/text/" | grep --quiet "introduction.xhtml" && echo -e "\t\t<itemref idref=\"introduction.xhtml\"/>" >> /tmp/spine
ls "${srcDir}/src/epub/text/" | grep --quiet "foreword.xhtml" && echo -e "\t\t<itemref idref=\"foreword.xhtml\"/>" >> /tmp/spine
ls "${srcDir}/src/epub/text/" | grep --quiet "preface.xhtml" && echo -e "\t\t<itemref idref=\"preface.xhtml\"/>" >> /tmp/spine
ls "${srcDir}/src/epub/text/" | grep --quiet "epigraph.xhtml" && echo -e "\t\t<itemref idref=\"epigraph.xhtml\"/>" >> /tmp/spine
ls "${srcDir}/src/epub/text/" | grep --quiet "halftitle.xhtml" && echo -e "\t\t<itemref idref=\"halftitle.xhtml\"/>" >> /tmp/spine
ls -v "${srcDir}/src/epub/text/" | sed --regexp-extended "
/titlepage.xhtml/d
/colophon.xhtml/d
/uncopyright.xhtml/d
/dedication.xhtml/d
/introduction.xhtml/d
/halftitle.xhtml/d
/imprint.xhtml/d
/epigraph.xhtml/d
/preface.xhtml/d
/foreword.xhtml/d
/loi.xhtml/d
/endnotes.xhtml/d
s|(.*)|\t\t<itemref idref=\"\1\"/>|g
" >> /tmp/spine
ls "${srcDir}/src/epub/text/" | grep --quiet "endnotes.xhtml" && echo -e "\t\t<itemref idref=\"endnotes.xhtml\"/>" >> /tmp/spine
ls "${srcDir}/src/epub/text/" | grep --quiet "loi.xhtml" && echo -e "\t\t<itemref idref=\"loi.xhtml\"/>" >> /tmp/spine
echo -e "\t\t<itemref idref=\"colophon.xhtml\"/>" >> /tmp/spine
echo -e "\t\t<itemref idref=\"uncopyright.xhtml\"/>" >> /tmp/spine
echo -e "\t</spine>" >> /tmp/spine
cat /tmp/spine
rm -f /tmp/manifest /tmp/spine
done