-
Notifications
You must be signed in to change notification settings - Fork 95
/
Metadata.swift
321 lines (298 loc) · 14.1 KB
/
Metadata.swift
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
311
312
313
314
315
316
317
318
319
320
321
//
// Copyright 2023 Readium Foundation. All rights reserved.
// Use of this source code is governed by the BSD-style license
// available in the top-level LICENSE file of the project.
//
import Foundation
import ReadiumInternal
/// https://readium.org/webpub-manifest/schema/metadata.schema.json
public struct Metadata: Hashable, Loggable, WarningLogger {
/// Collection type used for collection/series metadata.
/// For convenience, the JSON schema reuse the Contributor's definition.
public typealias Collection = Contributor
public let identifier: String? // URI
public let type: String? // URI (@type)
public let conformsTo: [Publication.Profile]
public let localizedTitle: LocalizedString
public var title: String { localizedTitle.string }
public let localizedSubtitle: LocalizedString?
public var subtitle: String? { localizedSubtitle?.string }
public let accessibility: Accessibility?
public let modified: Date?
public let published: Date?
public let languages: [String] // BCP 47 tag
// Main language of the publication.
public let language: Language?
public let sortAs: String?
public let subjects: [Subject]
public let authors: [Contributor]
public let translators: [Contributor]
public let editors: [Contributor]
public let artists: [Contributor]
public let illustrators: [Contributor]
public let letterers: [Contributor]
public let pencilers: [Contributor]
public let colorists: [Contributor]
public let inkers: [Contributor]
public let narrators: [Contributor]
public let contributors: [Contributor]
public let publishers: [Contributor]
public let imprints: [Contributor]
public let description: String?
public let duration: Double?
public let numberOfPages: Int?
public let belongsTo: [String: [Collection]]
/// WARNING: This contains the reading progression as declared in the manifest, so it might be
/// `auto`. To know the effective reading progression used to lay out the content, use
/// `effectiveReadingProgression` instead.
public let readingProgression: ReadingProgression
/// Additional properties for extensions.
public var otherMetadata: [String: Any] { otherMetadataJSON.json }
// Trick to keep the struct equatable despite [String: Any]
private let otherMetadataJSON: JSONDictionary
public init(
identifier: String? = nil,
type: String? = nil,
conformsTo: [Publication.Profile] = [],
title: LocalizedStringConvertible,
subtitle: LocalizedStringConvertible? = nil,
accessibility: Accessibility? = nil,
modified: Date? = nil,
published: Date? = nil,
languages: [String] = [],
sortAs: String? = nil,
subjects: [Subject] = [],
authors: [Contributor] = [],
translators: [Contributor] = [],
editors: [Contributor] = [],
artists: [Contributor] = [],
illustrators: [Contributor] = [],
letterers: [Contributor] = [],
pencilers: [Contributor] = [],
colorists: [Contributor] = [],
inkers: [Contributor] = [],
narrators: [Contributor] = [],
contributors: [Contributor] = [],
publishers: [Contributor] = [],
imprints: [Contributor] = [],
readingProgression: ReadingProgression = .auto,
description: String? = nil,
duration: Double? = nil,
numberOfPages: Int? = nil,
belongsTo: [String: [Collection]] = [:],
belongsToCollections: [Collection] = [],
belongsToSeries: [Collection] = [],
otherMetadata: [String: Any] = [:]
) {
self.identifier = identifier
self.type = type
self.conformsTo = conformsTo
localizedTitle = title.localizedString
localizedSubtitle = subtitle?.localizedString
self.accessibility = accessibility
self.modified = modified
self.published = published
self.languages = languages
language = languages.first.map { Language(code: .bcp47($0)) }
self.sortAs = sortAs
self.subjects = subjects
self.authors = authors
self.translators = translators
self.editors = editors
self.artists = artists
self.illustrators = illustrators
self.letterers = letterers
self.pencilers = pencilers
self.colorists = colorists
self.inkers = inkers
self.narrators = narrators
self.contributors = contributors
self.publishers = publishers
self.imprints = imprints
self.readingProgression = readingProgression
self.description = description
self.duration = duration
self.numberOfPages = numberOfPages
var belongsTo = belongsTo
if !belongsToCollections.isEmpty {
belongsTo["collection"] = belongsToCollections
}
if !belongsToSeries.isEmpty {
belongsTo["series"] = belongsToSeries
}
self.belongsTo = belongsTo
otherMetadataJSON = JSONDictionary(otherMetadata) ?? JSONDictionary()
}
public init(json: Any?, warnings: WarningLogger? = nil, normalizeHREF: (String) -> String = { $0 }) throws {
guard var json = JSONDictionary(json),
let title = try? LocalizedString(json: json.pop("title"), warnings: warnings)
else {
throw JSONError.parsing(Metadata.self)
}
identifier = json.pop("identifier") as? String
type = json.pop("@type") as? String ?? json.pop("type") as? String
conformsTo = parseArray(json.pop("conformsTo"), allowingSingle: true)
.map { Publication.Profile($0) }
localizedTitle = title
localizedSubtitle = try? LocalizedString(json: json.pop("subtitle"), warnings: warnings)
accessibility = try? Accessibility(json: json.pop("accessibility"), warnings: warnings)
modified = parseDate(json.pop("modified"))
published = parseDate(json.pop("published"))
languages = parseArray(json.pop("language"), allowingSingle: true)
language = languages.first.map { Language(code: .bcp47($0)) }
sortAs = json.pop("sortAs") as? String
subjects = [Subject](json: json.pop("subject"), warnings: warnings)
authors = [Contributor](json: json.pop("author"), warnings: warnings, normalizeHREF: normalizeHREF)
translators = [Contributor](json: json.pop("translator"), warnings: warnings, normalizeHREF: normalizeHREF)
editors = [Contributor](json: json.pop("editor"), warnings: warnings, normalizeHREF: normalizeHREF)
artists = [Contributor](json: json.pop("artist"), warnings: warnings, normalizeHREF: normalizeHREF)
illustrators = [Contributor](json: json.pop("illustrator"), warnings: warnings, normalizeHREF: normalizeHREF)
letterers = [Contributor](json: json.pop("letterer"), warnings: warnings, normalizeHREF: normalizeHREF)
pencilers = [Contributor](json: json.pop("penciler"), warnings: warnings, normalizeHREF: normalizeHREF)
colorists = [Contributor](json: json.pop("colorist"), warnings: warnings, normalizeHREF: normalizeHREF)
inkers = [Contributor](json: json.pop("inker"), warnings: warnings, normalizeHREF: normalizeHREF)
narrators = [Contributor](json: json.pop("narrator"), warnings: warnings, normalizeHREF: normalizeHREF)
contributors = [Contributor](json: json.pop("contributor"), warnings: warnings, normalizeHREF: normalizeHREF)
publishers = [Contributor](json: json.pop("publisher"), warnings: warnings, normalizeHREF: normalizeHREF)
imprints = [Contributor](json: json.pop("imprint"), warnings: warnings, normalizeHREF: normalizeHREF)
readingProgression = parseRaw(json.pop("readingProgression")) ?? .auto
description = json.pop("description") as? String
duration = parsePositiveDouble(json.pop("duration"))
numberOfPages = parsePositive(json.pop("numberOfPages"))
belongsTo = (json.pop("belongsTo") as? [String: Any])?
.compactMapValues { item in [Collection](json: item, warnings: warnings, normalizeHREF: normalizeHREF) }
?? [:]
otherMetadataJSON = json
}
public var json: [String: Any] {
makeJSON([
"identifier": encodeIfNotNil(identifier),
"@type": encodeIfNotNil(type),
"conformsTo": encodeIfNotEmpty(conformsTo.map(\.uri)),
"title": localizedTitle.json,
"subtitle": encodeIfNotNil(localizedSubtitle?.json),
"accessibility": encodeIfNotEmpty(accessibility?.json),
"modified": encodeIfNotNil(modified?.iso8601),
"published": encodeIfNotNil(published?.iso8601),
"language": encodeIfNotEmpty(languages),
"sortAs": encodeIfNotNil(sortAs),
"subject": encodeIfNotEmpty(subjects.json),
"author": encodeIfNotEmpty(authors.json),
"translator": encodeIfNotEmpty(translators.json),
"editor": encodeIfNotEmpty(editors.json),
"artist": encodeIfNotEmpty(artists.json),
"illustrator": encodeIfNotEmpty(illustrators.json),
"letterer": encodeIfNotEmpty(letterers.json),
"penciler": encodeIfNotEmpty(pencilers.json),
"colorist": encodeIfNotEmpty(colorists.json),
"inker": encodeIfNotEmpty(inkers.json),
"narrator": encodeIfNotEmpty(narrators.json),
"contributor": encodeIfNotEmpty(contributors.json),
"publisher": encodeIfNotEmpty(publishers.json),
"imprint": encodeIfNotEmpty(imprints.json),
"readingProgression": readingProgression.rawValue,
"description": encodeIfNotNil(description),
"duration": encodeIfNotNil(duration),
"numberOfPages": encodeIfNotNil(numberOfPages),
"belongsTo": encodeIfNotEmpty(belongsTo.mapValues { $0.json }),
], additional: otherMetadata)
}
public var belongsToCollections: [Collection] {
belongsTo["collection"] ?? []
}
public var belongsToSeries: [Collection] {
belongsTo["series"] ?? []
}
/// Computes a `ReadingProgression` when the value of `readingProgression` is set to `auto`,
/// using the publication language.
///
/// See this issue for more details: https://github.com/readium/architecture/issues/113
public var effectiveReadingProgression: ReadingProgression {
guard readingProgression == .auto else {
return readingProgression
}
// https://github.com/readium/readium-css/blob/develop/docs/CSS16-internationalization.md#missing-page-progression-direction
guard languages.count == 1, var language = languages.first?.lowercased() else {
return .ltr
}
if ["zh-hant", "zh-tw"].contains(language) {
return .rtl
}
// The region is ignored for ar, fa and he.
language = language.split(separator: "-").first.map(String.init) ?? language
if ["ar", "fa", "he"].contains(language) {
return .rtl
}
return .ltr
}
/// Makes a copy of the `Metadata`, after modifying some of its properties.
public func copy(
identifier: String?? = nil,
type: String?? = nil,
conformsTo: [Publication.Profile]? = nil,
title: LocalizedStringConvertible? = nil,
subtitle: LocalizedStringConvertible?? = nil,
accessibility: Accessibility?? = nil,
modified: Date?? = nil,
published: Date?? = nil,
languages: [String]? = nil,
sortAs: String?? = nil,
subjects: [Subject]? = nil,
authors: [Contributor]? = nil,
translators: [Contributor]? = nil,
editors: [Contributor]? = nil,
artists: [Contributor]? = nil,
illustrators: [Contributor]? = nil,
letterers: [Contributor]? = nil,
pencilers: [Contributor]? = nil,
colorists: [Contributor]? = nil,
inkers: [Contributor]? = nil,
narrators: [Contributor]? = nil,
contributors: [Contributor]? = nil,
publishers: [Contributor]? = nil,
imprints: [Contributor]? = nil,
readingProgression: ReadingProgression? = nil,
description: String?? = nil,
duration: Double?? = nil,
numberOfPages: Int?? = nil,
belongsTo: [String: [Collection]]? = nil,
belongsToCollections: [Collection]? = nil,
belongsToSeries: [Collection]? = nil,
otherMetadata: [String: Any]? = nil
) -> Metadata {
Metadata(
identifier: identifier ?? self.identifier,
type: type ?? self.type,
conformsTo: conformsTo ?? self.conformsTo,
title: title ?? localizedTitle,
subtitle: subtitle ?? localizedSubtitle,
accessibility: accessibility ?? self.accessibility,
modified: modified ?? self.modified,
published: published ?? self.published,
languages: languages ?? self.languages,
sortAs: sortAs ?? self.sortAs,
subjects: subjects ?? self.subjects,
authors: authors ?? self.authors,
translators: translators ?? self.translators,
editors: editors ?? self.editors,
artists: artists ?? self.artists,
illustrators: illustrators ?? self.illustrators,
letterers: letterers ?? self.letterers,
pencilers: pencilers ?? self.pencilers,
colorists: colorists ?? self.colorists,
inkers: inkers ?? self.inkers,
narrators: narrators ?? self.narrators,
contributors: contributors ?? self.contributors,
publishers: publishers ?? self.publishers,
imprints: imprints ?? self.imprints,
readingProgression: readingProgression ?? self.readingProgression,
description: description ?? self.description,
duration: duration ?? self.duration,
numberOfPages: numberOfPages ?? self.numberOfPages,
belongsTo: belongsTo ?? self.belongsTo,
belongsToCollections: belongsToCollections ?? self.belongsToCollections,
belongsToSeries: belongsToSeries ?? self.belongsToSeries,
otherMetadata: otherMetadata ?? self.otherMetadata
)
}
}