-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreader.go
More file actions
133 lines (109 loc) · 3.32 KB
/
Copy pathreader.go
File metadata and controls
133 lines (109 loc) · 3.32 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
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
package epub
import (
"encoding/xml"
"slices"
"strconv"
"strings"
"github.com/raitucarp/epub/ocf"
"github.com/raitucarp/epub/pkg"
)
// Reader provides an interface for reading and accessing EPUB publication
// data. It offers methods for retrieving metadata, navigation structures,
// content documents, resources, and images.
type Reader struct {
epub *Epub
}
func newReaderFromZip(zipContainer *ocf.OCFZipContainer) (reader Reader, err error) {
reader.epub = &Epub{
packagePaths: make(map[string]string),
packagePubs: make(map[string]*pkg.Package),
metadata: make(map[string]any),
zipContainer: zipContainer,
}
err = reader.parseRootFiles(zipContainer)
if err != nil {
return
}
reader.SelectPackageRendition("default")
return
}
func (r *Reader) parseRootFiles(z *ocf.OCFZipContainer) (err error) {
for index, rootFile := range z.Container().RootFiles.RootFile {
packageFullPath := rootFile.FullPath
data, err := z.SelectFile(packageFullPath)
if err != nil {
return err
}
var packagePub pkg.Package
err = xml.Unmarshal(data, &packagePub)
if err != nil {
return err
}
renditionVars := renditionKey(rootFile, index)
r.epub.packagePaths[renditionVars] = packageFullPath
r.epub.packagePubs[renditionVars] = &packagePub
}
return nil
}
// renditionKey derives the key used to select a package rendition. The first
// rootfile is always the default rendition, as defined by the EPUB
// multiple-rendition specification. Subsequent renditions are keyed by their
// rendition selection attributes.
func renditionKey(rootFile ocf.RootFile, index int) string {
if index == 0 {
return "default"
}
parts := []string{}
if rootFile.Media != "" {
parts = append(parts, rootFile.Media)
}
if rootFile.Layout != "" {
parts = append(parts, rootFile.Layout)
}
if rootFile.Language != "" {
parts = append(parts, rootFile.Language)
}
if rootFile.AccessMode != "" {
parts = append(parts, rootFile.AccessMode)
}
if rootFile.Label != "" {
parts = append(parts, rootFile.Label)
}
if len(parts) > 0 {
return strings.Join(parts, "_")
}
return "rendition-" + strconv.Itoa(index)
}
// OpenReader opens an EPUB file from the provided file path and returns
// a Reader instance. The file must exist and be a valid EPUB container.
func OpenReader(name string) (reader Reader, err error) {
zipContainer, err := ocf.OpenReader(name)
if err != nil {
return
}
return newReaderFromZip(zipContainer)
}
// ListRenditions returns the identifiers of all package renditions available
// in the publication. The first rendition is always named "default".
func (r *Reader) ListRenditions() []string {
renditions := make([]string, 0, len(r.epub.packagePubs))
for rendition := range r.epub.packagePubs {
renditions = append(renditions, rendition)
}
slices.Sort(renditions)
// Ensure the default rendition is always listed first.
if index := slices.Index(renditions, "default"); index > 0 {
renditions = slices.Delete(renditions, index, index+1)
renditions = slices.Insert(renditions, 0, "default")
}
return renditions
}
// NewReader creates a new Reader instance from a raw EPUB byte slice.
// The byte slice must represent a valid EPUB container.
func NewReader(b []byte) (reader Reader, err error) {
zipContainer, err := ocf.NewReader(b)
if err != nil {
return
}
return newReaderFromZip(zipContainer)
}