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
spine: Vec<String>
epub spine ids
resources: HashMap<String, (PathBuf, String)>
resource id -> (path, mime)
toc: Vec<NavPoint>
table of content, list of NavPoint
in the toc.ncx
metadata: HashMap<String, Vec<String>>
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_base: PathBuf
root file base path
root_file: PathBuf
root file full path
extra_css: Vec<String>
Custom css list to inject in every xhtml file
Methods
impl EpubDoc
[src]
impl EpubDoc
pub fn new<P: AsRef<Path>>(path: P) -> Result<EpubDoc, Error>
[src]
pub fn new<P: AsRef<Path>>(path: P) -> Result<EpubDoc, Error>
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.
pub fn mdata(&self, name: &str) -> Option<String>
[src]
pub fn mdata(&self, name: &str) -> Option<String>
Returns the first metadata found with this name.
#Examples
let title = doc.mdata("title"); assert_eq!(title.unwrap(), "Todo es mío");
pub fn get_cover_id(&self) -> Result<String, Error>
[src]
pub fn get_cover_id(&self) -> Result<String, Error>
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.
pub fn get_cover(&mut self) -> Result<Vec<u8>, Error>
[src]
pub fn get_cover(&mut self) -> Result<Vec<u8>, Error>
Returns the cover as Vec
Examples
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.
pub fn get_resource_by_path<P: AsRef<Path>>(
&mut self,
path: P
) -> Result<Vec<u8>, Error>
[src]
pub fn get_resource_by_path<P: AsRef<Path>>(
&mut self,
path: P
) -> Result<Vec<u8>, Error>
Returns the resource content by full path in the epub archive
Errors
Returns an error if the path doesn't exists in the epub
pub fn get_resource(&mut self, id: &str) -> Result<Vec<u8>, Error>
[src]
pub fn get_resource(&mut self, id: &str) -> Result<Vec<u8>, Error>
Returns the resource content by the id defined in the spine
Errors
Returns an error if the id doesn't exists in the epub
pub fn get_resource_str_by_path<P: AsRef<Path>>(
&mut self,
path: P
) -> Result<String, Error>
[src]
pub fn get_resource_str_by_path<P: AsRef<Path>>(
&mut self,
path: P
) -> Result<String, Error>
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
pub fn get_resource_str(&mut self, id: &str) -> Result<String, Error>
[src]
pub fn get_resource_str(&mut self, id: &str) -> Result<String, Error>
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
pub fn get_resource_mime(&self, id: &str) -> Result<String, Error>
[src]
pub fn get_resource_mime(&self, id: &str) -> Result<String, Error>
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.
pub fn get_resource_mime_by_path<P: AsRef<Path>>(
&self,
path: P
) -> Result<String, Error>
[src]
pub fn get_resource_mime_by_path<P: AsRef<Path>>(
&self,
path: P
) -> Result<String, Error>
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.
pub fn get_current(&mut self) -> Result<Vec<u8>, Error>
[src]
pub fn get_current(&mut self) -> Result<Vec<u8>, Error>
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.
pub fn get_current_str(&mut self) -> Result<String, Error>
[src]
pub fn get_current_str(&mut self) -> Result<String, Error>
pub fn get_current_with_epub_uris(&mut self) -> Result<Vec<u8>, Error>
[src]
pub fn get_current_with_epub_uris(&mut self) -> Result<Vec<u8>, Error>
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/"));
pub fn get_current_mime(&self) -> Result<String, Error>
[src]
pub fn get_current_mime(&self) -> Result<String, Error>
Returns the current chapter mimetype
Examples
let m = doc.get_current_mime(); assert_eq!("application/xhtml+xml", m.unwrap());
pub fn get_current_path(&self) -> Result<PathBuf, Error>
[src]
pub fn get_current_path(&self) -> Result<PathBuf, Error>
Returns the current chapter full path
Examples
let p = doc.get_current_path(); assert_eq!(Path::new("OEBPS/Text/titlepage.xhtml"), p.unwrap());
pub fn get_current_id(&self) -> Result<String, Error>
[src]
pub fn get_current_id(&self) -> Result<String, Error>
Returns the current chapter id
Examples
let id = doc.get_current_id(); assert_eq!("titlepage.xhtml", id.unwrap());
pub fn go_next(&mut self) -> Result<(), Error>
[src]
pub fn go_next(&mut self) -> Result<(), Error>
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
pub fn go_prev(&mut self) -> Result<(), Error>
[src]
pub fn go_prev(&mut self) -> Result<(), Error>
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
pub fn get_num_pages(&self) -> usize
[src]
pub fn get_num_pages(&self) -> usize
pub fn get_current_page(&self) -> usize
[src]
pub fn get_current_page(&self) -> usize
Returns the current chapter number, starting from 0
pub fn set_current_page(&mut self, n: usize) -> Result<(), Error>
[src]
pub fn set_current_page(&mut self, n: usize) -> Result<(), Error>
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
pub fn add_extra_css(&mut self, css: &str)
[src]
pub fn add_extra_css(&mut self, css: &str)
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));
pub fn resource_uri_to_chapter(&self, uri: &PathBuf) -> Option<usize>
[src]
pub fn resource_uri_to_chapter(&self, uri: &PathBuf) -> Option<usize>
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
pub fn resource_id_to_chapter(&self, uri: &str) -> Option<usize>
[src]
pub fn resource_id_to_chapter(&self, uri: &str) -> Option<usize>
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