Skip to content

Instantly share code, notes, and snippets.

@stonezhl
Last active August 1, 2019 17:54
Show Gist options
  • Select an option

  • Save stonezhl/7c3efea1c8cb98fee8e649ad304df305 to your computer and use it in GitHub Desktop.

Select an option

Save stonezhl/7c3efea1c8cb98fee8e649ad304df305 to your computer and use it in GitHub Desktop.
OPFManifest Struct
import Kanna
struct OPFManifest {
private(set) var items = [String: ManifestItem]()
init?(package: XMLElement) {
let opfNamespace = ["opf": "http://www.idpf.org/2007/opf"]
guard let manifest = package.at_xpath("opf:manifest", namespaces: opfNamespace) else { return nil }
let itemElements = manifest.xpath("opf:item", namespaces: opfNamespace)
for itemElement in itemElements {
guard let item = ManifestItem(itemElement) else { continue }
items[item.id] = item
}
}
}
public struct ManifestItem {
let id: String
let href: String
let mediaType: String
let properties: String?
// Others like duration, fallback, etc.
init?(_ item: XMLElement) {
guard let itemId = item["id"] else { return nil }
guard let itemHref = item["href"] else { return nil }
guard let itemMediaType = item["media-type"] else { return nil }
id = itemId
href = itemHref
mediaType = itemMediaType
properties = item["properties"]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment