Git Product home page Git Product logo

epubkit's Introduction

Logo

Swift CocoaPods Carthage https://github.com/apple/swift-package-manager Platforms Travis Contact

๐Ÿ“š A simple swift library for parsing EPUB documents

Note: This library is still in its early stages! I will experiment and change the API until I am satisfied with the result. I do not reccomend using this library in larger projects, although feedback will be highly appreciated ๐Ÿ™‡

Installation

CocoaPods

Add the following to your Podfile:

pod 'EPUBKit', '~> 0.3.1'

Carthage

Add to Cartfile:

github "witekbobrowski/EPUBKit" ~> 0.3.1

Swift Package Manager

Add to Package.swift:

.Package(url: "https://github.com/witekbobrowski/EPUBKit.git", from: "0.3.1")

Usage

Basic

Just import EPUBKit in your swift file.

import EPUBKit

Initialize document instance with URL of your EPUB document.

guard
    let path = Bundle.main.url(forResource: "steve_jobs", withExtension: "epub"),
    let document = EPUBDocument(url: path)
else { return }

If the document gets parsed correctly, you have access to full document metadata, contents, etc.

print(document.title)
> Steve Jobs
print(document.author)
> Walter Isaacson

Advanced

Lets say we are developing an app for iOS and have a view controller that handles epub documents in some way, for example displays a list.

In the first place you could add these two properties to the view controller (dont forget to import the library).

let parser: EPUBParser
let urls: [URL]

var documents: [EPUBDocument] = []

And feed the VC with the missing properties through the dependency injection in init.

init(parser: EPUBParser, urls: [URL]) {
    self.parser = parser
    self.urls = urls
    super.init(nibName: nil, bundle: nil)
}

Now after the view loads we could set ourselfs as the delegate of the parser (after extending view controller with EPUBParserDelegate protocol, otherwise we get an error).

parser.delegate = self

And iterate over the array of url in hope of parsing every document correctly and append them to previously defined array.

urls.forEach { url in
    guard let document = try? parser.parse(documentAt: url) else { return }
    documents.append(document)
}

And that is basically it. Now for example, you could pass parsed documents to the table view.

What are the adventages of taking this approach? Firstly its reusing the parser object. Using the previously mentioned EPUBDocument's init(url:) initializer we avoid instantiating it every time for each document. Now we have also a lot more insight on the parsing process itself, we could either check on errors in the standard swift way using do-catch statement, or using delegation and parser(:,didFailParsingDocumentAt:,with:) that passes an error if such occurs. And finally we could improve user experience with something like starting to load cover before the process of parsing finishes.

As the library evolves and API gets richer and richer the possibilities of advanced usage of this library will come more and more handy.

Note: Documentation is not yet ready, but you should find it easy to explore the API by yourself ๐Ÿ™ƒ

epubkit's People

Contributors

witekbobrowski avatar madyankin avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.