-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkepub.go
More file actions
54 lines (48 loc) · 1.56 KB
/
Copy pathkepub.go
File metadata and controls
54 lines (48 loc) · 1.56 KB
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
package quarto
import (
"context"
"regexp"
)
var (
xhtmlRe = regexp.MustCompile(`(?i)\.x?html?$`)
navRe = regexp.MustCompile(`epub:type=["']toc["']`)
)
// ToKepub converts a standard EPUB into a Kobo kepub in memory. It's the
// equivalent of running kepubify, with no external binary required.
//
// Every content document is rewritten with Kobo reading-location spans and
// the book-columns/book-inner wrappers. The navigation document is left
// untouched. The returned bytes should be written with a .kepub.epub
// extension so Kobo devices recognise the enhanced format.
func ToKepub(data []byte) ([]byte, error) {
entries, err := unzipEpub(data)
if err != nil {
return nil, err
}
files := make([]fileEntry, 0, len(entries))
for _, entry := range entries {
if entry.path == "mimetype" {
continue // re-added (stored, first) by zipEpub
}
if xhtmlRe.MatchString(entry.path) {
// Skip the navigation document; Kobo builds its TOC from it
// directly.
if !navRe.Match(entry.data) {
entry.data = []byte(kepubifyXhtml(string(entry.data)))
}
}
files = append(files, entry)
}
return zipEpub(files)
}
// GenerateKepub is a convenience that builds an EPUB3 from in and converts it
// to a Kobo kepub in one call (equivalent to ToKepub(Generate(ctx, in))). It
// is the common path for callers targeting Kobo devices; use Generate and
// ToKepub separately when you need the intermediate EPUB3.
func GenerateKepub(ctx context.Context, in Input) ([]byte, error) {
epub, err := Generate(ctx, in)
if err != nil {
return nil, err
}
return ToKepub(epub)
}