No description
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
The Ducktor 3c9cdaab62 k
2026-08-03 22:06:49 +08:00
.cargo k 2026-03-04 16:30:26 +08:00
.zed k 2026-03-04 16:19:30 +08:00
examples Improve EPUB path resolution and update Cargo deps 2026-06-17 12:14:35 +08:00
src k 2026-08-03 22:06:49 +08:00
wasm refactoring 2026-06-17 10:49:37 +08:00
.gitignore k 2026-03-04 16:19:30 +08:00
Cargo.lock Improve EPUB path resolution and update Cargo deps 2026-06-17 12:14:35 +08:00
Cargo.toml k 2026-08-03 22:06:49 +08:00
iia.log k 2026-03-04 16:19:30 +08:00
LICENSE Update zip crate to 5.1.1 and remove wasm-bindgen dependency 2025-10-08 20:51:45 +08:00
README.md k 2026-08-03 22:06:49 +08:00
wsw.log k 2026-03-04 16:19:30 +08:00

epubie-lib

⚠️ This library is opinionated.

If you want to poke around in the guts of the EPUB spec, this isn't the crate for you.
If you want to treat EPUBs as books — with chapters, files, and metadata — welcome!


What's new

  • ✅ EPUB 2 support — reads NCX-based navigation and detects the version automatically
  • ✅ EPUB writing — build a new EPUB 3 from scratch with a fluent builder API
  • ✅ EPUB 2 → 3 conversion — one-liner upgrade of legacy files
  • ✅ Restructured codebase — clean reader/, writer/, types/, compat/ modules
  • 🦀 WASM writer — EpubWriterWasm lets you create EPUBs from JavaScript too

Installation

[dependencies]
epubie-lib = "0.2.0"

Reading

use epubie_lib::Epub;

let epub = Epub::new("book.epub".to_string())?;
// Equivalent:
let epub = Epub::new("book.epub")?;

println!("Version:  {}", epub.get_version());   // "EPUB 2.0.1" or "EPUB 3"
println!("Title:    {}", epub.get_title().unwrap_or("No title"));
println!("Author:   {}", epub.get_creator().unwrap_or("Unknown"));

for chapter in epub.get_chapters() {
    println!("  Chapter: {}", chapter.get_title());
}

for entry in epub.get_table_of_contents() {
    println!("  TOC: {} -> {}", entry.get_title(), entry.get_href());
}

Supported formats

Feature EPUB 2 EPUB 3
Metadata (title, creator, …) ✅ ✅
Chapter extraction ✅ ✅
Table of contents NCX nav
Cover image ✅ ✅
Version detection auto auto

Writing

use epubie_lib::writer::EpubBuilder;

let bytes = EpubBuilder::new()
    .title("My Book")
    .creator("Jane Author")
    .language("en")
    .publisher("Self-published")
    .date("2024-06-01")
    .stylesheet("body { font-family: Georgia, serif; }")
    .add_chapter("Introduction", "<h1>Hello</h1><p>Welcome to my book.</p>")
    .add_chapter("Chapter One", "<h1>Chapter 1</h1><p>It was a dark night…</p>")
    .build()?;

std::fs::write("output.epub", bytes)?;

Builder methods

Method Description
.title(s) Book title
.creator(s) Add an author (call multiple times)
.language(s) BCP 47 language code (e.g. "en")
.publisher(s), .description(s), … Optional Dublin Core fields
.date(s) Publication date (YYYY-MM-DD)
.identifier(s) Unique ID (auto-generated if absent)
.tag(s) Add a subject tag
.stylesheet(css) Global CSS linked from all chapters
.add_chapter(title, html_body) Append a chapter
.add_chapter_with_css(title, html, css) Chapter with inline <style>
.cover_image(bytes, media_type) Cover image (JPEG / PNG / …)
.renderer(custom) Use custom EpubRenderer strategy
.build() Produce Vec<u8>

EPUB 2 → EPUB 3 conversion

use epubie_lib::{Epub, compat};

let old = Epub::new("old-book.epub".to_string())?;
let new_bytes = compat::epub2_to_epub3(&old)?;
std::fs::write("new-book.epub", new_bytes)?;

The converter copies all metadata, re-encodes chapters as clean XHTML, generates a spec-compliant nav.xhtml, and carries over the cover image when present.


Module layout

src/
├── lib.rs          — public re-exports and crate docs
├── epub.rs         — Epub type (thin facade over reader)
├── types/
│   ├── mod.rs      — EpubVersion enum + re-exports
│   ├── metadata.rs — Metadata struct
│   ├── content.rs  — EpubFile, Chapter
│   └── toc.rs      — TableOfContents, TocEntry
├── reader/
│   ├── mod.rs      — parse_epub_bytes(), internal grouping logic
│   ├── opf.rs      — serde structs for container.xml / OPF
│   ├── epub2.rs    — NCX parser
│   ├── epub3.rs    — nav.xhtml parser
│   └── utils.rs    — path helpers (resolve_path, …)
├── writer/
│   └── mod.rs      — EpubBuilder
└── compat/
    └── mod.rs      — epub2_to_epub3()

WebAssembly

Both the reader and writer are available from JavaScript via the WASM package.

// Reading
const epub = EpubWasm.from_bytes(fileBytes);
console.log(epub.title(), epub.version(), epub.is_epub2());

// Reading → convert to EPUB 3
const epub3Bytes = epub.to_epub3();

// Writing
const writer = EpubWriterWasm.new();
writer.set_title("My Book");
writer.set_creator("Jane Author");
writer.set_language("en");
writer.add_chapter("Ch 1", "<p>Hello!</p>");
const epubBytes = writer.build();

Bundle size note: the WASM binary is not size-optimized. Suitable for tooling and server-side use; think twice before shipping it to end-user browsers.


License

MIT