forked from bmaupin/go-epub
-
Notifications
You must be signed in to change notification settings - Fork 7
/
toc.go
310 lines (269 loc) · 7.99 KB
/
toc.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package epub
import (
"encoding/xml"
"fmt"
"log"
"path/filepath"
"regexp"
"strconv"
)
const (
tocNavBodyTemplate = `
<nav epub:type="toc">
<h1>Table of Contents</h1>
<ol>
</ol>
</nav>
`
tocNavFilename = "nav.xhtml"
tocNavItemID = "nav"
tocNavItemProperties = "nav"
tocNavEpubType = "toc"
tocNcxFilename = "toc.ncx"
tocNcxItemID = "ncx"
tocNcxTemplate = `
<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">
<head>
<meta name="dtb:uid" content="" />
<meta name="dtb:depth" content="" />
</head>
<docTitle>
<text></text>
</docTitle>
<docAuthor>
<text></text>
</docAuthor>
<navMap>
</navMap>
</ncx>`
xmlnsEpub = "http://www.idpf.org/2007/ops"
)
// toc implements the EPUB table of contents
type toc struct {
// This holds the body XML for the EPUB v3 TOC file (nav.xhtml). Since this is
// an XHTML file, the rest of the structure is handled by the xhtml type
//
// Spec: http://www.idpf.org/epub/301/spec/epub-contentdocs.html#sec-xhtml-nav
navXML *tocNavBody
// This holds the XML for the EPUB v2 TOC file (toc.ncx). This is added so the
// resulting EPUB v3 file will still work with devices that only support EPUB v2
//
// Spec: http://www.idpf.org/epub/20/spec/OPF_2.0.1_draft.htm#Section2.4.1
ncxXML *tocNcxRoot
title string // EPUB title
author string // EPUB author
}
type tocNavBody struct {
XMLName xml.Name `xml:"nav"`
EpubType string `xml:"epub:type,attr"`
H1 string `xml:"h1"`
Links []*tocNavItem `xml:"ol>li"`
}
type tocNavItem struct {
A tocNavLink `xml:"a"`
Children []*tocNavItem `xml:"ol>li,omitempty"`
}
type tocNavLink struct {
XMLName xml.Name `xml:"a"`
Href string `xml:"href,attr"`
Data string `xml:",chardata"`
}
type tocNcxRoot struct {
XMLName xml.Name `xml:"http://www.daisy.org/z3986/2005/ncx/ ncx"`
Version string `xml:"version,attr"`
Meta tocNcxMeta `xml:"head>meta"`
Title string `xml:"docTitle>text"`
Author string `xml:"docAuthor>text"`
NavMap []*tocNcxNavPoint `xml:"navMap>navPoint"`
}
type tocNcxContent struct {
Src string `xml:"src,attr"`
}
type tocNcxMeta struct {
Name string `xml:"name,attr"`
Content string `xml:"content,attr"`
}
type tocNcxNavPoint struct {
XMLName xml.Name `xml:"navPoint"`
ID string `xml:"id,attr"`
Text string `xml:"navLabel>text"`
Content tocNcxContent `xml:"content"`
Children []*tocNcxNavPoint `xml:"navPoint,omitempty"`
}
// Constructor for toc
func newToc() (*toc, error) {
t := &toc{}
var err error
t.navXML, err = newTocNavXML()
if err != nil {
return nil, fmt.Errorf("can't create navXML: %w", err)
}
t.ncxXML, err = newTocNcxXML()
if err != nil {
return nil, fmt.Errorf("can't create navXML: %w", err)
}
return t, nil
}
// Constructor for tocNavBody
func newTocNavXML() (*tocNavBody, error) {
b := &tocNavBody{
EpubType: tocNavEpubType,
}
err := xml.Unmarshal([]byte(tocNavBodyTemplate), &b)
if err != nil {
return nil, fmt.Errorf("Error unmarshalling tocNavBody: %w\n"+"\ttocNavBody=%#v\n"+"\ttocNavBodyTemplate=%s", err, *b, tocNavBodyTemplate)
}
return b, nil
}
// Constructor for tocNcxRoot
func newTocNcxXML() (*tocNcxRoot, error) {
n := &tocNcxRoot{}
err := xml.Unmarshal([]byte(tocNcxTemplate), &n)
if err != nil {
return nil, fmt.Errorf("Error unmarshalling tocNcxRoot: %w\n"+"\ttocNcxRoot=%#v\n"+"\ttocNcxTemplate=%s", err, *n, tocNcxTemplate)
}
return n, nil
}
// TODO: user should not add -1 as filename
// Add a section to the TOC (navXML as well as ncxXML)
func (t *toc) addSubSection(parent string, index int, title string, relativePath string) {
relativePath = filepath.ToSlash(relativePath)
if parent == "-1" {
l := &tocNavItem{
A: tocNavLink{
Href: relativePath,
Data: title,
},
Children: nil,
}
np := &tocNcxNavPoint{
ID: "navPoint-" + strconv.Itoa(index),
Text: title,
Content: tocNcxContent{
Src: relativePath,
},
Children: nil,
}
t.navXML.Links = append(t.navXML.Links, l)
t.ncxXML.NavMap = append(t.ncxXML.NavMap, np)
} else {
parentRelativePath := filepath.Join(xhtmlFolderName, parent)
l := &tocNavItem{
A: tocNavLink{
Href: relativePath,
Data: title,
},
}
np := &tocNcxNavPoint{
ID: "navPoint-" + strconv.Itoa(index),
Text: title,
Content: tocNcxContent{
Src: relativePath,
},
Children: nil,
}
err := navAppender(t.navXML.Links, parentRelativePath, l)
if err != nil {
log.Println(err)
}
err = ncxAppender(t.ncxXML.NavMap, parentRelativePath, np)
if err != nil {
log.Println(err)
}
}
}
func (t *toc) setIdentifier(identifier string) {
t.ncxXML.Meta.Content = identifier
}
func (t *toc) setTitle(title string) {
t.title = title
}
// Write the TOC files
func (t *toc) write(tempDir string) error {
err := t.writeNavDoc(tempDir)
if err != nil {
return err
}
err = t.writeNcxDoc(tempDir)
if err != nil {
return err
}
return nil
}
// Write the the EPUB v3 TOC file (nav.xhtml) to the temporary directory
func (t *toc) writeNavDoc(tempDir string) error {
navBodyContent, err := xml.MarshalIndent(t.navXML, " ", " ")
if err != nil {
return fmt.Errorf("Error marshalling XML for EPUB v3 TOC file: %w\n"+"\tXML=%#v", err, t.navXML)
}
// subsection without children itself left an empty tag <ol></ol>
// that not acceptable for epub v3
// this regex will remove those line from tocnav.
// TODO: find a better solution
re := regexp.MustCompile(`\s*<ol>\s*</ol>`)
bodyWithoutEmptyTags := re.ReplaceAllString(string(navBodyContent), "")
n, err := newXhtml(bodyWithoutEmptyTags)
if err != nil {
return fmt.Errorf("can't create xhtml for TOC file: %w", err)
}
n.setXmlnsEpub(xmlnsEpub)
n.setTitle(t.title)
navFilePath := filepath.Join(tempDir, contentFolderName, tocNavFilename)
err = n.write(navFilePath)
if err != nil {
return fmt.Errorf("can't write TOC file: %w", err)
}
return nil
}
// Write the EPUB v2 TOC file (toc.ncx) to the temporary directory
func (t *toc) writeNcxDoc(tempDir string) error {
t.ncxXML.Title = t.title
t.ncxXML.Author = t.author
ncxFileContent, err := xml.MarshalIndent(t.ncxXML, "", " ")
if err != nil {
return fmt.Errorf("Error marshalling XML for EPUB v2 TOC file: %w\n"+"+\tXML=%#v", err, t.ncxXML)
}
// Add the xml header to the output
ncxFileContent = append([]byte(xml.Header), ncxFileContent...)
// It's generally nice to have files end with a newline
ncxFileContent = append(ncxFileContent, "\n"...)
ncxFilePath := filepath.Join(tempDir, contentFolderName, tocNcxFilename)
if err := filesystem.WriteFile(ncxFilePath, []byte(ncxFileContent), filePermissions); err != nil {
return fmt.Errorf("Error writing EPUB v2 TOC file: %w", err)
}
return nil
}
// Append tocNcxNavPoint to parent children for toc in Epub v2
func ncxAppender(t []*tocNcxNavPoint, parentFilename string, targetsection *tocNcxNavPoint) error {
// Search for the epubSection with filename equal to parentFilename
for _, ncx := range t {
if ncx.Content.Src == parentFilename {
// Append targetsection as children of the found section
ncx.Children = append(ncx.Children, targetsection)
return nil
}
// Recursively check all children of the current section
err := ncxAppender(ncx.Children, parentFilename, targetsection)
if err == nil {
return nil
}
}
return fmt.Errorf("parent section not found")
}
// Append tocNavItem to parent children for toc in Epub v3
func navAppender(t []*tocNavItem, parentFilename string, targetsection *tocNavItem) error {
// Search for the epubSection with filename equal to parentFilename
for _, nav := range t {
if nav.A.Href == parentFilename {
// Append targetsection as children of the found section
nav.Children = append(nav.Children, targetsection)
return nil
}
// Recursively check all children of the current section
err := navAppender(nav.Children, parentFilename, targetsection)
if err == nil {
return nil
}
}
return fmt.Errorf("parent section not found")
}