Bookbinder 1.0.3

Bookbinder 1.0.3

stonezhl维护。



 
依赖项
Kanna~> 5.0.0
ZIPFoundation~> 0.9
 

Bookbinder

iOS的Swift ePub解析框架。

您可以阅读如何在iOS上解析EPub文件以了解更多信息。

要求

  • Swift 5.0+
  • iOS 10.0+
  • ARC

基本用法

  1. 使用默认配置创建bookbinder实例
    // default configuration uses `NSTemporaryDirectory` as root directory to unzip ePub file
    let bookbinder = Bookbinder()
    
  2. 逐行解析EPub文件
    let ebook = bookbinder.bindBook(at: ePubFileURL)
    
  3. 现成的电子书接口
    // cover image
    let coverImageURLs = ebook.coverImageURLs
    // toc
    let tocURL = ebook.tocURL
    // ncx
    let ncx = ebook.ncx
    // primary spine items
    let pages = ebook.pages
    // others
    let mainAuthor = ebook.opf.metadata.creators.first
    ...
    
  4. BookbinderTests中的Playground
    // study `Bookbinder` from unit test
    let ebook = EPUBBook(identifier: "Alice's_Adventures_in_Wonderland", contentsOf: url)
    expect(ebook).notTo(beNil())
    expect(ebook?.identifier).to(equal("Alice's_Adventures_in_Wonderland"))
    expect(ebook?.baseURL).to(equal(url))
    expect(ebook?.resourceBaseURL).to(equal(url.appendingPathComponent("epub")))
    expect(ebook?.container).notTo(beNil())
    expect(ebook?.opf).notTo(beNil())
    expect(ebook?.uniqueID).to(equal("url:https://standardebooks.org/ebooks/lewis-carroll/alices-adventures-in-wonderland"))
    expect(ebook?.releaseID).to(equal("\(ebook?.uniqueID ?? "")@2017-03-09T17:21:15Z"))
    expect(ebook?.publicationDate).to(equal(ISO8601DateFormatter().date(from: "2015-05-12T00:01:00Z")))
    ...
    

高级用法

  1. 使用自定义配置创建bookbinder实例
    let configuration = BookbinderConfiguration(rootURL: customRootURL)
    let bookbinder = Bookbinder(configuration: configuration)
    
  2. EPUBBook的子类
    class CustomBook: EPUBBook {
        lazy var firstAuthors: [String] = {
            return opf.metadata.creators
        }()
        
        lazy var secondAuthors: [String] = {
            return opf.metadata.contributors
        }()
        
        ...
    }
    
    let bookbinder = Bookbinder()
    let ebook = bookbinder.bindBook(at: url, to: CustomBook.self)
    
  3. 自定义OPF XPath
    import Kanna
    
    struct GuideRef {
        let title: String
        let href: String
        let type: String
        
        init(_ reference: XMLElement) {
            title = reference["title"] ?? ""
            href = reference["href"] ?? ""
            type = reference["type"] ?? ""
        }
    }
    
    class CustomBook: EPUBBook {
        // http://www.idpf.org/epub/20/spec/OPF_2.0.1_draft.htm#Section2.6
        lazy var guideRefs: [GuideRef] = {
            var refs = [GuideRef]()
            let xpath = "/opf:package/opf:guide/opf:reference"
            let references = opf.document.xpath(xpath, namespaces: XPath.opf.namespace)
            for reference in references {
                refs.append(GuideRef(reference))
            }
            return refs
        }()
        
        ...
    }
    

安装

Cathage

请将其添加到您的 Cartfile

github "stonezhl/Bookbinder" ~> 1.0.0

CocoaPods

请将其添加到您的 Podfile

use_frameworks!
pod 'Bookbinder', '~> 1.0.0'

许可协议

Bookbinder 采用 MIT 许可协议发布。详细信息请见 LICENSE 文件。