-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinfo.go
238 lines (200 loc) · 5.82 KB
/
info.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
package epub
// Information gathers meta information about an epub as a simpler version
// of Metadata to offer a more direct access to an Epub's metadata for simple
// use cases.
type Information struct {
// Identifier contains an identifier associated with the given
// Rendition, such as a UUID, DOI or ISBN.
Identifier []Identifier
// Title represents the EPUB titles.
Title []string
// SubTitle represents the EPUB sub-titles.
SubTitle []string `json:",omitempty"`
// Language element specifies the language of the content of the
// given Rendition.
Language []string
// Contributor represents the name of a person, organization, etc.
// that played a secondary role in the creation of the content of an
// EPUB Publication.
Contributor []Author `json:",omitempty"`
// Coverage gives the extent or scope of the publication’s content.
Coverage []string `json:",omitempty"`
// Creator represents the name of a person, organization, etc.
// responsible for the creation of the content of the Rendition.
Creator []Author
// Date lists events associated to the EPUB like publication, creation...
Date []Date `json:",omitempty"`
// Description provides a description of the publication's content.
Description []string `json:",omitempty"`
// Format identifies the media type or dimensions of the resource.
Format []string `json:",omitempty"`
// Publisher identifies the publication's publisher.
Publisher []string `json:",omitempty"`
// Relation is an identifier of an auxiliary resource and its
// relationship to the publication.
Relation []string `json:",omitempty"`
// Rights provides a statement about rights, or a reference to one.
Rights []string `json:",omitempty"`
// Sources provides information regarding a prior resource from which
// the publication was derived.
Source []string `json:",omitempty"`
// Subject identifies the subject of the EPUB Publication.
Subject []string `json:",omitempty"`
// Type is used to indicate that the given EPUB Publication is of a
// specialized type.
Type []string `json:",omitempty"`
// Meta element provides a generic means of including package
// metadata.
Meta []GenericMetadata `json:",omitempty"`
// Series is the series to which this book belongs to.
Series string `json:",omitempty"`
// SeriesIndex is the position in the series to which the book belongs to.
SeriesIndex string `json:",omitempty"`
}
// Identifier represents an identifier.
type Identifier struct {
Scheme string
Value string
}
// Author represents an author.
type Author struct {
FullName string
FileAs string
Role string
}
// Date represents an event.
type Date struct {
Stamp string
Event string
}
// GenericMetadata represents a generic metadata.
type GenericMetadata struct {
Name string
Content string
}
// GetMetadataFromFile reads metadata from an epub file.
func GetMetadataFromFile(path string) (*Information, error) {
e, err := Open(path)
if err != nil {
return nil, err
}
defer e.Close()
return e.Information()
}
func getMeta(mdata *Metadata) *Information {
m := &Information{
Language: elt2str(mdata.Language),
Subject: elt2str(mdata.Subject),
Description: elt2str(mdata.Description),
Publisher: elt2str(mdata.Publisher),
Type: elt2str(mdata.Type),
Format: elt2str(mdata.Format),
Source: elt2str(mdata.Source),
Relation: elt2str(mdata.Relation),
Coverage: elt2str(mdata.Coverage),
Rights: elt2str(mdata.Rights),
}
m.Title, m.SubTitle = getTitles(mdata.Title, mdata.Meta)
for _, id := range mdata.Identifier {
m.Identifier = append(m.Identifier, Identifier{
Value: id.Value,
Scheme: id.Scheme,
})
}
for _, auth := range mdata.Creator {
m.Creator = append(m.Creator, getAuth(auth, mdata.Meta))
}
for _, auth := range mdata.Contributor {
m.Contributor = append(m.Contributor, getAuth(auth, mdata.Meta))
}
for _, date := range mdata.Date {
m.Date = append(m.Date, Date{
Stamp: date.Value,
Event: date.Event,
})
}
m.Series, m.SeriesIndex = getSeries(mdata.Meta)
for _, meta := range mdata.Meta {
if meta.Name != "" && meta.Content != "" {
m.Meta = append(m.Meta, GenericMetadata{
Name: meta.Name,
Content: meta.Content,
})
}
}
return m
}
func elt2str(elt []Element) []string {
s := make([]string, len(elt))
for i, e := range elt {
s[i] = e.Value
}
return s
}
func getAuth(auth AuthorElt, meta []MetaLegacy) Author {
a := Author{
FullName: auth.Value,
Role: auth.Role,
FileAs: auth.FileAs,
}
for _, m := range meta {
if m.Refines != "#"+auth.ID {
continue
}
switch m.Property {
case "role":
a.Role = m.Value
case "file-as":
a.FileAs = m.Value
}
}
return a
}
func getTitles(elt []Element, meta []MetaLegacy) (title []string, subtitle []string) {
nextElt:
for _, e := range elt {
for _, m := range meta {
if m.Refines != "#"+e.ID {
continue
}
if m.Property == "title-type" {
switch m.Value {
case "subtitle":
subtitle = append(subtitle, e.Value)
break nextElt
}
}
break
}
title = append(title, e.Value)
}
return
}
// getSeries extracts series information from Meta. it supports 'claibre's-like
// EPUB 2 series coding or EPUB30-like collection metadata. If both are
// available, EPUB30 is prefered.
func getSeries(meta []MetaLegacy) (series string, seriesIndex string) {
for _, m := range meta {
switch m.Name {
case "calibre:series":
series = m.Content
case "calibre:series_index":
seriesIndex = m.Content
}
if m.Property == "belongs-to-collection" {
series = m.Value
for _, mm := range meta {
if mm.Refines != "#"+m.ID {
continue
}
// TODO: filter-out cases where property 'collection-type' is
// not empty and not "series" (seems that it can be collection
// or set)
if mm.Property == "group-position" {
seriesIndex = mm.Value
}
}
}
}
return
}