EPUBKit

repository·main·Indexed 43 days ago

https://github.com/witekbobrowski/epubkit

A modern Swift library for parsing and extracting information from EPUB 2 and EPUB 3 documents. It provides a thread-safe, high-level API to access metadata, manifest resources, reading order (spine), and navigation structures, with support for EPUB 3.3, EPUB 2.0, Dublin Core Metadata, and OCF.

Tokens
1.1K
Snippets
4
Records
5
Agent score
6%

What's inside EPUBKit

  1. EPUBKit specification support overview

    main

    EPUBKit is designed to handle various EPUB standards and metadata formats:

    • EPUB 3.3: Full support for modern EPUB files.
    • EPUB 2.0: Backward compatibility for older files.
    • Dublin Core Metadata: Complete extraction of metadata.
    • OCF (Open Container Format): Proper handling of ZIP archives.
    • NCX Navigation: Support for parsing legacy table of contents.
    • Media Type Detection: Automatic identification of resource types.
  2. Access detailed metadata, spine items, and table of contents

    main

    EPUBKit allows for deep inspection of the document structure:

    • Metadata: Access the metadata object to retrieve detailed creator information, including name, role, and fileAs.
    • Spine (Reading Order): Iterate through document.spine.items. Use the idref from a spine item to look up the corresponding resource in document.manifest.items to find the file path and mediaType.
    • Table of Contents: Use document.tableOfContents to access the EPUBTableOfContents structure, which consists of a label and a recursive list of children.
    // Access metadata details
    if let creator = document.metadata.creator {
        print("Author: \(creator.name)")
        print("Role: \(creator.role)")
        print("File as: \(creator.fileAs)")
    }
    
    // Iterate through spine items (reading order)
    for spineItem in document.spine.items {
        if let manifestItem = document.manifest.items[spineItem.idref] {
            print("Chapter: \(manifestItem.path)")
            print("Media Type: \(manifestItem.mediaType)")
        }
    }
    
    // Access table of contents
    func printTOC(_ toc: EPUBTableOfContents, level: Int = 0) {
        let indent = String(repeating: "  ", count: level)
        print("\(indent)- \(toc.label)")
        
        for child in toc.children {
            printTOC(child, level: level + 1)
        }
    }
    
    printTOC(document.tableOfContents)
  3. Install EPUBKit via Swift Package Manager

    main

    To add EPUBKit to your Swift project, use the Swift Package Manager. You can add it directly via Xcode or by updating your Package.swift file.

    dependencies: [
        .package(url: "https://github.com/witekbobrowski/EPUBKit.git", from: "1.0.0")
    ]
  4. Parse an EPUB file and access basic metadata

    main

    Use EPUBDocument(url:) to initialize a parser with an EPUB file URL. Once initialized, you can access top-level metadata properties like title, author, publisher, and language.

    import EPUBKit
    
    // Parse an EPUB file
    guard let epubURL = Bundle.main.url(forResource: "book", withExtension: "epub"),
          let document = EPUBDocument(url: epubURL) else {
        print("Failed to load EPUB")
        return
    }
    
    // Access document metadata
    print("Title: \(document.title)")
    print("Author: \(document.author)")
    print("Publisher: \(document.publisher)")
    print("Language: \(document.language)")
    
    // Access document structure
    print("Chapters: \(document.spine.items.count)")
    print("Resources: \(document.manifest.items.count)")