Skip to content

Instantly share code, notes, and snippets.

@stonezhl
stonezhl / opfdocument.swift
Last active August 16, 2019 15:41
OPFDocument Struct
import Kanna
struct OPFDocument {
let metadata: OPFMetadata
let manifest: OPFManifest
let spine: OPFSpine
// Other elements, like collection, guide, etc.
let document: XMLDocument
@stonezhl
stonezhl / epubbook_2.swift
Last active August 21, 2019 03:30
EPUBBook Class - 2
import Foundation
class EPUBBook {
// ...
let opf: OPFDocument
init?(contentsOf baseURL: URL) {
// ...
// Parse the OPF file
@stonezhl
stonezhl / container_document.swift
Last active August 16, 2019 15:33
ContainerDocument Struct
import Kanna
struct ContainerDocument {
let opfPath: String
let document: XMLDocument
init?(url: URL) {
do {
// Just one line to get the DOM document of a XML file
@stonezhl
stonezhl / sax_dom_table.csv
Last active July 30, 2019 02:25
A table of SAX and DOM.
SAX DOM
Large XML file to parse or memory limitation Small XML file or no memory limitation
Need to be fast at run time The data structure is complicated
Only need to parse a specific piece Need to do elements query
@stonezhl
stonezhl / xml_parsers_table.csv
Last active July 30, 2019 02:05
A table of the XML parsers.
Framework Native Processing Query Note Link
XMLParser Y SAX - Available for iOS & macOS https://developer.apple.com/documentation/foundation/xmlparser
XMLDocument Y DOM XPath & XQuery Only available for macOS https://developer.apple.com/documentation/foundation/xmldocument
libxml2 Y SAX & DOM XPath & CSS Selector C library http://xmlsoft.org/
Kanna N SAX & DOM XPath & CSS Selector Built on libxml2 https://github.com/tid-kijyun/Kanna
SWXMLHash N DOM Hash Query Built on XMLParser https://github.com/drmohundro/SWXMLHash
XMLParsing N DOM Decoder Built on XMLParser https://github.com/ShawnMoore/XMLParsing
@stonezhl
stonezhl / opfspine.swift
Last active August 21, 2019 03:29
OPFSpine Struct
import Kanna
struct OPFSpine {
private(set) var idrefs = [String]()
init?(package: XMLElement) {
guard let spine = package.at_xpath("opf:spine", namespaces: XPath.opf.namespace) else { return nil }
idrefs = spine.xpath("opf:itemref[@idref]/@idref", namespaces: opfNamespace)
.map { $0.text }
.compactMap { $0 }
@stonezhl
stonezhl / spine_in_opf_sample.opf
Last active July 30, 2019 04:27
A sample of the spine element in an OPF file.
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" dir="ltr" prefix="se: http://standardebooks.org/vocab/1.0" unique-identifier="uid" version="3.0" xml:lang="en-US">
<spine toc="ncx">
<itemref idref="titlepage.xhtml"/>
<itemref idref="chapter-1.xhtml"/>
<itemref idref="chapter-2.xhtml"/>
<itemref idref="chapter-3.xhtml"/>
<itemref idref="uncopyright.xhtml"/>
<!--Other itemrefs-->
</spine>
@stonezhl
stonezhl / opfmanifest.swift
Last active August 1, 2019 17:54
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 {
@stonezhl
stonezhl / manifest_in_opf_sample.opf
Last active July 30, 2019 04:22
A sample of the manifest element in an OPF file.
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" dir="ltr" prefix="se: http://standardebooks.org/vocab/1.0" unique-identifier="uid" version="3.0" xml:lang="en-US">
<manifest>
<item href="toc.ncx" id="ncx" media-type="application/x-dtbncx+xml"/>
<item href="text/uncopyright.xhtml" id="uncopyright.xhtml" media-type="application/xhtml+xml"/>
<item href="toc.xhtml" id="toc.xhtml" media-type="application/xhtml+xml" properties="nav"/>
<item href="text/chapter-2.xhtml" id="chapter-2.xhtml" media-type="application/xhtml+xml"/>
<item href="images/cover.jpg" id="cover.jpg" media-type="image/jpeg" properties="cover-image"/>
<item href="text/chapter-3.xhtml" id="chapter-3.xhtml" media-type="application/xhtml+xml"/>
<item href="images/titlepage.png" id="titlepage.png" media-type="image/png"/>
@stonezhl
stonezhl / opfmetadata.swift
Last active August 1, 2019 17:52
OPFMetadata Struct
import Kanna
struct OPFMetadata {
// DCMES Required Elements
private(set) var identifiers = [String]()
private(set) var titles = [String]()
private(set) var languages = [String]()
// DCMES Optional Elements
private(set) var creators = [String]()
private(set) var date: String?