Struct epub::doc::EpubDoc[][src]

pub struct EpubDoc {
    pub spine: Vec<String>,
    pub resources: HashMap<String, (PathBuf, String)>,
    pub toc: Vec<NavPoint>,
    pub metadata: HashMap<String, Vec<String>>,
    pub root_base: PathBuf,
    pub root_file: PathBuf,
    pub extra_css: Vec<String>,
    // some fields omitted
}

Struct to control the epub document

Fields

epub spine ids

resource id -> (path, mime)

table of content, list of NavPoint in the toc.ncx

The epub metadata stored as key -> value

#Examples

let title = doc.metadata.get("title");
assert_eq!(title.unwrap(), &vec!["Todo es mío".to_string()]);

root file base path

root file full path

Custom css list to inject in every xhtml file

Methods

impl EpubDoc
[src]

Opens the epub file in path.

Initialize some internal variables to be able to access to the epub spine definition and to navigate trhough the epub.

Examples

use epub::doc::EpubDoc;

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

Errors

Returns an error if the epub is broken or if the file doesn't exists.

Returns the first metadata found with this name.

#Examples

let title = doc.mdata("title");
assert_eq!(title.unwrap(), "Todo es mío");

Returns the id of the epub cover.

The cover is searched in the doc metadata, by the tag <meta name="cover" value"..">

Examples

use epub::doc::EpubDoc;

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

let cover_id = doc.get_cover_id().unwrap();

Errors

Returns an error if the cover path can't be found.

Returns the cover as Vec

Examples

This example is not tested
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);

Errors

Returns an error if the cover can't be found.

Returns the resource content by full path in the epub archive

Errors

Returns an error if the path doesn't exists in the epub

Returns the resource content by the id defined in the spine

Errors

Returns an error if the id doesn't exists in the epub

Returns the resource content by full path in the epub archive, as String

Errors

Returns an error if the path doesn't exists in the epub

Returns the resource content by the id defined in the spine, as String

Errors

Returns an error if the id doesn't exists in the epub

Returns the resource mime-type

Examples

let mime = doc.get_resource_mime("portada.png");
assert_eq!("image/png", mime.unwrap());

Errors

Fails if the resource can't be found.

Returns the resource mime searching by source full path

Examples

let mime = doc.get_resource_mime_by_path("OEBPS/Images/portada.png");
assert_eq!("image/png", mime.unwrap());

Errors

Fails if the resource can't be found.

Returns the current chapter content

The current follows the epub spine order. You can modify the current calling to go_next, go_prev or set_current methods.

Errors

This call shouldn't fail, but can return an error if the epub doc is broken.

Returns the current chapter data, with resource uris renamed so they have the epub:// prefix and all are relative to the root file

This method is useful to render the content with a html engine, because inside the epub local paths are relatives, so you can provide that content, because the engine will look for the relative path in the filesystem and that file isn't there. You should provide files with epub:// using the get_resource_by_path

Examples

let current = doc.get_current_with_epub_uris().unwrap();
let text = String::from_utf8(current).unwrap();
assert!(text.contains("epub://OEBPS/Images/portada.png"));
doc.go_next();
let current = doc.get_current_with_epub_uris().unwrap();
let text = String::from_utf8(current).unwrap();
assert!(text.contains("epub://OEBPS/Styles/stylesheet.css"));
assert!(text.contains("http://creativecommons.org/licenses/by-sa/3.0/"));

Returns the current chapter mimetype

Examples

let m = doc.get_current_mime();
assert_eq!("application/xhtml+xml", m.unwrap());

Returns the current chapter full path

Examples

let p = doc.get_current_path();
assert_eq!(Path::new("OEBPS/Text/titlepage.xhtml"), p.unwrap());

Returns the current chapter id

Examples

let id = doc.get_current_id();
assert_eq!("titlepage.xhtml", id.unwrap());

Changes current to the next chapter

Examples

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

let len = doc.spine.len();
for i in 1..len {
    doc.go_next();
}
assert!(doc.go_next().is_err());

Errors

If the page is the last, will not change and an error will be returned

Changes current to the prev chapter

Examples

assert!(doc.go_prev().is_err());

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

Errors

If the page is the first, will not change and an error will be returned

Returns the number of chapters

Examples

assert_eq!(17, doc.get_num_pages());

Returns the current chapter number, starting from 0

Changes the current page

Examples

assert_eq!(0, doc.get_current_page());
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).is_err());

Errors

If the page isn't valid, will not change and an error will be returned

This will inject this css in every html page getted with the get_current_with_epub_uris call

Examples

let extracss = "body { background-color: black; color: white }";
doc.add_extra_css(extracss);
let current = doc.get_current_with_epub_uris().unwrap();
let text = String::from_utf8(current).unwrap();
assert!(text.contains(extracss));

Function to convert a resource path to a chapter number in the spine If the resourse isn't in the spine list, None will be returned

This method is useful to convert a toc NavPoint content to a chapter number to be able to navigate easily

Function to convert a resource id to a chapter number in the spine If the resourse isn't in the spine list, None will be returned

Auto Trait Implementations

impl Send for EpubDoc

impl Sync for EpubDoc