forked from bmaupin/go-epub
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pkg.go
270 lines (233 loc) · 6.99 KB
/
pkg.go
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package epub
import (
"encoding/xml"
"fmt"
"io/ioutil"
"path/filepath"
"time"
)
const (
pkgAuthorID = "role"
pkgAuthorData = "aut"
pkgAuthorProperty = "role"
pkgAuthorRefines = "#creator"
pkgAuthorScheme = "marc:relators"
pkgCreatorID = "creator"
pkgFileTemplate = `<?xml version="1.0" encoding="UTF-8"?>
<package version="3.0" unique-identifier="pub-id" xmlns="http://www.idpf.org/2007/opf">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:identifier id="pub-id"></dc:identifier>
<dc:title></dc:title>
<dc:language></dc:language>
<dc:description></dc:description>
</metadata>
<manifest>
</manifest>
<spine toc="ncx">
</spine>
</package>
`
pkgModifiedProperty = "dcterms:modified"
pkgUniqueIdentifier = "pub-id"
xmlnsDc = "http://purl.org/dc/elements/1.1/"
)
// pkg implements the package document file (package.opf), which contains
// metadata about the EPUB (title, author, etc) as well as a list of files the
// EPUB contains.
//
// Sample: https://github.com/bmaupin/epub-samples/blob/master/minimal-v3plus2/EPUB/package.opf
// Spec: http://www.idpf.org/epub/301/spec/epub-publications.html
type pkg struct {
xml *pkgRoot
authorMeta *pkgMeta
modifiedMeta *pkgMeta
}
// This holds the actual XML for the package file
type pkgRoot struct {
XMLName xml.Name `xml:"http://www.idpf.org/2007/opf package"`
UniqueIdentifier string `xml:"unique-identifier,attr"`
Version string `xml:"version,attr"`
Metadata pkgMetadata `xml:"metadata"`
ManifestItems []pkgItem `xml:"manifest>item"`
Spine pkgSpine `xml:"spine"`
}
// <dc:creator>, e.g. the author
type pkgCreator struct {
XMLName xml.Name `xml:"dc:creator"`
ID string `xml:"id,attr"`
Data string `xml:",chardata"`
}
// <dc:identifier>, where the unique identifier is stored
// Ex: <dc:identifier id="pub-id">urn:uuid:fe93046f-af57-475a-a0cb-a0d4bc99ba6d</dc:identifier>
type pkgIdentifier struct {
ID string `xml:"id,attr"`
Data string `xml:",chardata"`
}
// <item> elements, one per each file stored in the EPUB
// Ex: <item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav" />
// <item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml" />
// <item id="section0001.xhtml" href="xhtml/section0001.xhtml" media-type="application/xhtml+xml" />
type pkgItem struct {
ID string `xml:"id,attr"`
Href string `xml:"href,attr"`
MediaType string `xml:"media-type,attr"`
Properties string `xml:"properties,attr,omitempty"`
}
// <itemref> elements, which define the reading order
// Ex: <itemref idref="section0001.xhtml" />
type pkgItemref struct {
Idref string `xml:"idref,attr"`
}
// The <meta> element, which contains modified date, role of the creator (e.g.
// author), etc
// Ex: <meta refines="#creator" property="role" scheme="marc:relators" id="role">aut</meta>
// <meta property="dcterms:modified">2011-01-01T12:00:00Z</meta>
type pkgMeta struct {
Refines string `xml:"refines,attr,omitempty"`
Property string `xml:"property,attr"`
Scheme string `xml:"scheme,attr,omitempty"`
ID string `xml:"id,attr,omitempty"`
Data string `xml:",chardata"`
}
// The <metadata> element
type pkgMetadata struct {
XmlnsDc string `xml:"xmlns:dc,attr"`
Identifier pkgIdentifier `xml:"dc:identifier"`
// Ex: <dc:title>Your title here</dc:title>
Title string `xml:"dc:title"`
// Ex: <dc:language>en</dc:language>
Language string `xml:"dc:language"`
Description string `xml:"dc:description,omitempty"`
Date string `xml:"dc:date"`
Creator *pkgCreator
Meta []pkgMeta `xml:"meta"`
}
// The <spine> element
type pkgSpine struct {
Items []pkgItemref `xml:"itemref"`
Toc string `xml:"toc,attr"`
Ppd string `xml:"page-progression-direction,attr,omitempty"`
}
// Constructor for pkg
func newPackage() *pkg {
p := &pkg{
xml: &pkgRoot{
Metadata: pkgMetadata{
XmlnsDc: xmlnsDc,
Identifier: pkgIdentifier{
ID: pkgUniqueIdentifier,
},
},
},
}
err := xml.Unmarshal([]byte(pkgFileTemplate), &p.xml)
if err != nil {
panic(fmt.Sprintf(
"Error unmarshalling package file XML: %s\n"+
"\tp.xml=%#v\n"+
"\tpkgFileTemplate=%s",
err,
*p.xml,
pkgFileTemplate))
}
return p
}
func (p *pkg) addToManifest(id string, href string, mediaType string, properties string) {
href = filepath.ToSlash(href)
i := &pkgItem{
ID: id,
Href: href,
MediaType: mediaType,
Properties: properties,
}
p.xml.ManifestItems = append(p.xml.ManifestItems, *i)
}
func (p *pkg) addToSpine(id string) {
i := &pkgItemref{
Idref: id,
}
p.xml.Spine.Items = append(p.xml.Spine.Items, *i)
}
func (p *pkg) setAuthor(author string) {
p.xml.Metadata.Creator = &pkgCreator{
Data: author,
ID: pkgCreatorID,
}
p.authorMeta = &pkgMeta{
Data: pkgAuthorData,
ID: pkgAuthorID,
Property: pkgAuthorProperty,
Refines: pkgAuthorRefines,
Scheme: pkgAuthorScheme,
}
p.xml.Metadata.Meta = updateMeta(p.xml.Metadata.Meta, p.authorMeta)
}
func (p *pkg) setIdentifier(identifier string) {
p.xml.Metadata.Identifier.Data = identifier
}
func (p *pkg) setLang(lang string) {
p.xml.Metadata.Language = lang
}
func (p *pkg) setDescription(desc string) {
p.xml.Metadata.Description = desc
}
func (p *pkg) setPpd(direction string) {
p.xml.Spine.Ppd = direction
}
func (p *pkg) setModified(timestamp string) {
p.modifiedMeta = &pkgMeta{
Data: timestamp,
Property: pkgModifiedProperty,
}
p.xml.Metadata.Meta = updateMeta(p.xml.Metadata.Meta, p.modifiedMeta)
}
func (p *pkg) setTitle(title string) {
p.xml.Metadata.Title = title
}
func (p *pkg) setDesc(desc string) {
p.xml.Metadata.Description = desc
}
// Update the <meta> element
func updateMeta(a []pkgMeta, m *pkgMeta) []pkgMeta {
indexToReplace := -1
if len(a) > 0 {
// If we've already added the modified meta element to the meta array
for i, meta := range a {
if meta == *m {
indexToReplace = i
break
}
}
}
// If the array is empty or the meta element isn't in it
if indexToReplace == -1 {
// Add the meta element to the array of meta elements
a = append(a, *m)
// If the meta element is found
} else {
// Replace it
a[indexToReplace] = *m
}
return a
}
// Write the package file to the temporary directory
func (p *pkg) write(tempDir string) {
now := time.Now().UTC().Format("2006-01-02T15:04:05Z")
p.setModified(now)
pkgFilePath := filepath.Join(tempDir, contentFolderName, pkgFilename)
output, err := xml.MarshalIndent(p.xml, "", " ")
if err != nil {
panic(fmt.Sprintf(
"Error marshalling XML for package file: %s\n"+
"\tXML=%#v",
err,
p.xml))
}
// Add the xml header to the output
pkgFileContent := append([]byte(xml.Header), output...)
// It's generally nice to have files end with a newline
pkgFileContent = append(pkgFileContent, "\n"...)
if err := ioutil.WriteFile(pkgFilePath, []byte(pkgFileContent), filePermissions); err != nil {
panic(fmt.Sprintf("Error writing package file: %s", err))
}
}