-
Notifications
You must be signed in to change notification settings - Fork 0
API Reference
This document provides complete API documentation for ebook-rs (v0.8.0), bifurcated into Drop-in Replacements for epub.js and ebook-rs Native Extensions.
These ebook-rs APIs match the concepts, structures, and behavior of epub.js (e.g. ePub(url), book.loaded.*, rendition.display(), rendition.annotations), enabling web developers and Rust engineers to migrate seamlessly.
epub.js Feature / API |
🚀 ebook-rs Equivalent API |
Description |
|---|---|---|
ePub(url) / ePub(buffer)
|
Book::from_file(path) / Book::from_bytes(bytes)
|
Loads and parses package files into an in-memory Book struct. |
book.loaded.metadata |
book.metadata() |
Returns Metadata containing title, creators, publishers, languages, pub_date, etc. |
book.loaded.spine |
book.spine() |
Returns Vec<SpineItem> containing ordered chapter items (idref, href, linear). |
book.loaded.navigation.toc |
book.toc() |
Returns Vec<NavPoint> hierarchical Table of Contents points. |
book.loaded.navigation.landmarks |
book.landmarks() |
Returns Vec<Landmark> (cover, titlepage, bodymatter). |
book.loaded.navigation.pageList |
book.page_list() |
Returns Vec<PageListItem> printed page numbers. |
book.locations.generate() |
book.generate_locations(chunk_size) |
Generates discrete location progress markers across chapters. |
book.locations.percentageFromCfi() |
book.locations.percentage_from_location(loc) |
Converts location integer indices to decimal progress percentage (0.0 .. 1.0). |
rendition.display(cfi) |
book.get_section(spine_idx) / Cfi::parse()
|
Retrieves raw and processed HTML content at specified CFI location. |
rendition.annotations.add() |
book.annotations.create_highlight() / to_w3c_json()
|
Creates highlights and exports W3C Web Annotation JSON-LD format. |
Where ebook-rs extends far beyond epub.js to provide native multi-format support, zero-alloc searching, font de-obfuscation, EPUB 3 Accessibility, SMIL Media Overlays, Readium LCP/Locators, Regex Search, EPUB Validation, Fingerprinting, and Citation Export.
epub.js only supports .epub files. ebook-rs natively supports EPUB 2/3, MOBI, AZW3, FB2, KEPUB, LIT, CBZ, PDF, ODT, TXT, and Markdown files out of the box.
EPUB 3 Accessibility 1.1 & Schema.org metadata parsing:
let a11y = &book.metadata().accessibility;
println!("Access Modes: {:?}", a11y.access_modes);
println!("Summary: {:?}", a11y.accessibility_summary);
assert!(a11y.is_screen_reader_friendly());Synchronized audio-text read-aloud overlays (.smil):
for (smil_path, pkg) in &book.media_overlays {
if let Some(text_ref) = pkg.find_text_ref_by_timestamp("ch1.mp3", 8.5) {
println!("Active element ID: {:?}", text_ref.element_id);
}
}-
LCP DRM:
LcpLicense::parse()&LcpDecryptor::decrypt_bytes() -
Unified Locators:
book.to_readium_locator(spine_idx, char_offset) -
Search API:
SearchEngine::to_readium_search_json(&results, query)
let matches = book.search_regex("(?i)quantum|physics")?;let report = book.validate();
println!("Is Valid: {}, Errors: {}", report.is_valid, report.errors_count);let fp = book.fingerprint();
let match_score = fp.match_score(&other_fp);println!("BibTeX:\n{}", book.to_bibtex());
println!("APA: {}", book.to_apa());
println!("MLA: {}", book.to_mla());
println!("Chicago: {}", book.to_chicago());let code_blocks = book.extract_code_blocks();
for block in code_blocks {
println!("Lang: {}, AST Nodes: {}", block.language, block.ast_nodes.len());
}let spread = book.get_synthetic_spread(0, Some(1))?;
println!("Spread HTML: {}", spread.combined_html);let matches = book.search_toc("quantum");
let flat_toc = book.flatten_toc();let epub_bytes = book.export_epub3_bytes()?;
std::fs::write("converted.epub", epub_bytes)?;let book = Book::from_mmap("omnibus.cbz")?;let mut tree = EbookDomTree::parse("<div><script>alert(1)</script><p>Text</p></div>");
tree.strip_elements(&["script"]);
let clean = tree.to_html();let decoded = decode_bytes_with_encoding(bytes, Some("windows-1252"));let lang = book.detect_language(); // Some("eng")let cache_bytes = book.export_zstd_cache()?;
let restored = Book::from_zstd_cache(&cache_bytes)?;let tokens = book.get_tts_tokens(0)?;
let html = book.get_tts_section_html(0)?;