Skip to content

Instantly share code, notes, and snippets.

@stonezhl
stonezhl / metadata_in_opf_sample.opf
Last active August 1, 2019 17:50
A sample of the metadata 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">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<!--The DCMES elements-->
<dc:identifier id="uid">url:https://standardebooks.org/ebooks/lewis-carroll/alices-adventures-in-wonderland</dc:identifier>
<dc:title id="title">Alice's Adventures in Wonderland</dc:title>
<dc:language>en-GB</dc:language>
<dc:creator id="author">Lewis Carroll</dc:creator>
<dc:publisher id="publisher">Standard Ebooks</dc:publisher>
<dc:date>2015-05-12T00:01:00Z</dc:date>
@stonezhl
stonezhl / opf_sample.opf
Last active August 1, 2019 17:44
A sample of 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">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<!--The DCMES elements-->
<!--The meta elements-->
<!--The link elements-->
</metadata>
<manifest>
<!--The item elements-->
</manifest>
@stonezhl
stonezhl / execute_at_xpath_opf_path.swift
Last active August 16, 2019 15:39
Execute 'at_xpath' to fetch the OPF path.
let path = document.at_xpath(xpath, namespaces: namespace)?.text
@stonezhl
stonezhl / xpath_query_opf_path.swift
Last active August 1, 2019 17:42
A XPath query to fetch the path of the OPF file.
let xpath = "//ctn:rootfile[@full-path]/@full-path"
@stonezhl
stonezhl / ctn_namespace.swift
Last active July 30, 2019 03:22
The container namespace in container.xml.
let namespace = ["ctn": "urn:oasis:names:tc:opendocument:xmlns:container"]
@stonezhl
stonezhl / epubbook_1.swift
Last active August 16, 2019 15:29
EPUBBook Class - 1
import Foundation
class EPUBBook {
let container: ContainerDocument
init?(contentsOf baseURL: URL) {
// Find the location of container.xml
let containerURL = baseURL.appendingPathComponent("META-INF/container.xml")
// Parse the container file
guard let container = ContainerDocument(url: containerURL) else { return nil }
@stonezhl
stonezhl / container_xml_sample.xml
Last active July 30, 2019 03:14
A sample of container.xml.
<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="urn:oasis:names:tc:opendocument:xmlns:container" version="1.0">
<rootfiles>
<rootfile full-path="epub/content.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
@stonezhl
stonezhl / epub_parser.swift
Last active August 3, 2019 15:05
EPUBParser Class
import ZIPFoundation
class EPUBParser {
static func parseFile(at sourceURL: URL) -> EPUBBook? {
// Get the ePub's filename
let filename = sourceURL.deletingPathExtension().lastPathComponent
// The directory to save the unzipped files
let destinationURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true).appendingPathComponent(filename)
// Get the file manager
let fileManager = FileManager()