Last active
August 1, 2019 17:54
-
-
Save stonezhl/7c3efea1c8cb98fee8e649ad304df305 to your computer and use it in GitHub Desktop.
OPFManifest Struct
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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