#epub #ebook

epub

Library to support the reading of epub files

14 stable releases

2.0.0 Dec 29, 2022
1.2.4 Nov 2, 2022
1.2.3 Nov 19, 2020
1.2.2 Nov 12, 2019
0.1.2 Jan 20, 2017

#199 in Text processing

Download history 171/week @ 2022-12-08 61/week @ 2022-12-15 80/week @ 2022-12-22 99/week @ 2022-12-29 63/week @ 2023-01-05 45/week @ 2023-01-12 80/week @ 2023-01-19 131/week @ 2023-01-26 171/week @ 2023-02-02 112/week @ 2023-02-09 169/week @ 2023-02-16 74/week @ 2023-02-23 127/week @ 2023-03-02 112/week @ 2023-03-09 129/week @ 2023-03-16 73/week @ 2023-03-23

449 downloads per month
Used in 2 crates

GPL-3.0 license

1MB
706 lines

epub-rs

Rust library to support the reading of epub files.

Install

Add this to your Cargo.toml:

[dependencies]
epub = "1.2.2"

MSRV

The minimum supported Rust version is 1.42.0.


lib.rs:

EPUB library lib to read and navigate through an epub file contents

Examples

Opening

use epub::doc::EpubDoc;
let doc = EpubDoc::new("test.epub");
assert!(doc.is_ok());
let doc = doc.unwrap();

Getting doc metatada

Metadata is a [HashMap] storing all metadata defined in the epub

# use epub::doc::EpubDoc;
# let doc = EpubDoc::new("test.epub");
# let doc = doc.unwrap();
let title = doc.mdata("title");
assert_eq!(title.unwrap(), "Todo es mío");

Accessing resources

In the resources var is stored each resource defined in the epub indexed by the id and with the full internal path and mimetype. It's a HashMap<a: String, (b: String, c: String)> where 'a' is the resource id, 'b' is the resource full path and 'c' is the resource mimetype

# use epub::doc::EpubDoc;
# use std::path::Path;
# let doc = EpubDoc::new("test.epub");
# let doc = doc.unwrap();
assert_eq!(23, doc.resources.len());
let tpage = doc.resources.get("titlepage.xhtml");
assert_eq!(tpage.unwrap().0, Path::new("OEBPS/Text/titlepage.xhtml"));
assert_eq!(tpage.unwrap().1, "application/xhtml+xml");

Navigating using the spine

Spine is a Vec storing the epub spine as resources ids

# use epub::doc::EpubDoc;
# let doc = EpubDoc::new("test.epub");
# let doc = doc.unwrap();
assert_eq!(17, doc.spine.len());
assert_eq!("titlepage.xhtml", doc.spine[0]);

Navigation using the doc internal state

use epub::doc::EpubDoc;
let doc = EpubDoc::new("test.epub");
let mut doc = doc.unwrap();
assert_eq!(0, doc.get_current_page());
assert_eq!("application/xhtml+xml", doc.get_current_mime().unwrap());

doc.go_next();
assert_eq!("000.xhtml", doc.get_current_id().unwrap());
doc.go_next();
assert_eq!("001.xhtml", doc.get_current_id().unwrap());
doc.go_prev();
assert_eq!("000.xhtml", doc.get_current_id().unwrap());

doc.set_current_page(2);
assert_eq!("001.xhtml", doc.get_current_id().unwrap());
assert_eq!(2, doc.get_current_page());
assert!(!doc.set_current_page(50));

// doc.get_current() will return a Vec<u8> with the current page content
// doc.get_current_str() will return a String with the current page content

Getting the cover

use std::fs;
use std::io::Write;
use epub::doc::EpubDoc;

let doc = EpubDoc::new("test.epub");
assert!(doc.is_ok());
let mut doc = doc.unwrap();

let cover_data = doc.get_cover().unwrap();

let f = fs::File::create("/tmp/cover.png");
assert!(f.is_ok());
let mut f = f.unwrap();
let resp = f.write_all(&cover_data);

Dependencies

~2.4–3.5MB
~80K SLoC