-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathopf.go
176 lines (162 loc) · 4.28 KB
/
opf.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
// Copyright 2012 Ruben Pollan <[email protected]>
// Use of this source code is governed by a LGPL licence
// version 3 or later that can be found in the LICENSE file.
package epubgo
import (
"errors"
"io"
"reflect"
"strings"
)
type xmlOPF struct {
Metadata meta `xml:"metadata"`
Manifest []manifest `xml:"manifest>item"`
Spine spine `xml:"spine"`
}
type meta struct {
Title []string `xml:"title"`
Language []string `xml:"language"`
Identifier []identifier `xml:"identifier"`
Creator []author `xml:"creator"`
Subject []string `xml:"subject"`
Description []string `xml:"description"`
Publisher []string `xml:"publisher"`
Contributor []author `xml:"contributor"`
Date []date `xml:"date"`
Type []string `xml:"type"`
Format []string `xml:"format"`
Source []string `xml:"source"`
Relation []string `xml:"relation"`
Coverage []string `xml:"coverage"`
Rights []string `xml:"rights"`
Meta []metafield `xml:"meta"`
}
type identifier struct {
Data string `xml:",chardata"`
ID string `xml:"id,attr"`
Scheme string `xml:"scheme,attr"`
}
type author struct {
Data string `xml:",chardata"`
FileAs string `xml:"file-as,attr"`
Role string `xml:"role,attr"`
}
type date struct {
// TODO: convert date to date type?
Data string `xml:",chardata"`
Event string `xml:"event,attr"`
}
type metafield struct {
Name string `xml:"name,attr"`
Content string `xml:"content,attr"`
}
type manifest struct {
ID string `xml:"id,attr"`
Href string `xml:"href,attr"`
MediaType string `xml:"media-type,attr"`
Fallback string `xml:"media-fallback,attr"`
Properties string `xml:"properties,attr"`
MediaOverlay string `xml:"media-overlay,attr"`
}
type spine struct {
ID string `xml:"id,attr"`
Toc string `xml:"toc,attr"`
PageProgression string `xml:"page-progression-direction,attr"`
Items []spineItem `xml:"itemref"`
}
type spineItem struct {
IDref string `xml:"idref,attr"`
Linear string `xml:"linear,attr"`
ID string `xml:"id,attr"`
Properties string `xml:"properties,attr"`
}
func parseOPF(opf io.Reader) (*xmlOPF, error) {
var o xmlOPF
err := decodeXML(opf, &o)
if err != nil {
return nil, err
}
return &o, nil
}
func (opf xmlOPF) ncxPath() string {
if opf.Spine.Toc != "" {
fileID := opf.Spine.Toc
path := opf.filePath(fileID)
if path != "" {
return path
}
}
fileID := "ncx"
return opf.filePath(fileID)
}
func (opf xmlOPF) filePath(id string) string {
for _, item := range opf.Manifest {
if item.ID == id {
return item.Href
}
}
return ""
}
func (opf xmlOPF) toMData() mdata {
m := opf.Metadata
metadata := make(mdata)
v := reflect.ValueOf(m)
typeOf := v.Type()
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
if field.Len() == 0 {
continue
}
fieldName := strings.ToLower(typeOf.Field(i).Name)
data := make([]mdataElement, field.Len())
for j := 0; j < field.Len(); j++ {
element := field.Index(j).Interface()
data[j] = elementToMData(element)
}
metadata[fieldName] = data
}
return metadata
}
func elementToMData(element interface{}) (result mdataElement) {
result.attr = make(map[string]string)
switch element.(type) {
case string:
result.content, _ = element.(string)
case identifier:
ident, _ := element.(identifier)
result.content = ident.Data
result.attr["id"] = ident.ID
result.attr["scheme"] = ident.Scheme
case author:
auth, _ := element.(author)
result.content = auth.Data
result.attr["file-as"] = auth.FileAs
result.attr["role"] = auth.Role
case date:
d, _ := element.(date)
result.content = d.Data
result.attr["event"] = d.Event
case metafield:
m, _ := element.(metafield)
result.content = m.Content
result.attr["name"] = m.Name
result.attr["content"] = m.Content
}
return
}
func (opf xmlOPF) spineLength() int {
return len(opf.Spine.Items)
}
func (opf xmlOPF) spineURL(index int) string {
idref := opf.Spine.Items[index].IDref
url, _ := opf.getURL(idref)
return url
}
func (opf xmlOPF) getURL(id string) (string, error) {
for _, item := range opf.Manifest {
if item.ID == id {
return item.Href, nil
}
}
return "", errors.New("ID " + id + " not in the manifest")
}