pub struct EpubDoc<R: Read + Seek> {
pub version: EpubVersion,
pub spine: Vec<SpineItem>,
pub resources: HashMap<String, (PathBuf, String)>,
pub toc: Vec<NavPoint>,
pub toc_title: String,
pub metadata: HashMap<String, Vec<String>>,
pub root_base: PathBuf,
pub root_file: PathBuf,
pub extra_css: Vec<String>,
pub unique_identifier: Option<String>,
pub cover_id: Option<String>,
/* private fields */
}
Expand description
Struct to control the epub document
Fields§
§version: EpubVersion
epub spec version
spine: Vec<SpineItem>
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
toc_title: String
title of toc
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
unique_identifier: Option<String>
unique identifier
cover_id: Option<String>
The id of the cover, if any
Implementations§
Source§impl EpubDoc<BufReader<File>>
impl EpubDoc<BufReader<File>>
Sourcepub fn new<P: AsRef<Path>>(path: P) -> Result<Self, DocError>
pub fn new<P: AsRef<Path>>(path: P) -> Result<Self, DocError>
Opens the epub file in path
.
Initialize some internal variables to be able to access to the epub spine definition and to navigate through 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.
Source§impl<R: Read + Seek> EpubDoc<R>
impl<R: Read + Seek> EpubDoc<R>
Sourcepub fn from_reader(reader: R) -> Result<Self, DocError>
pub fn from_reader(reader: R) -> Result<Self, DocError>
Opens the epub contained in reader
.
Initialize some internal variables to be able to access to the epub spine definition and to navigate through the epub.
§Examples
use epub::doc::EpubDoc;
use std::fs::File;
use std::io::{Cursor, Read};
let mut file = File::open("test.epub").unwrap();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).unwrap();
let cursor = Cursor::new(buffer);
let doc = EpubDoc::from_reader(cursor);
assert!(doc.is_ok());
§Errors
Returns an error if the epub is broken.
Sourcepub fn mdata(&self, name: &str) -> Option<String>
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");
Sourcepub fn get_cover_id(&self) -> Option<String>
pub fn get_cover_id(&self) -> Option<String>
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();
This returns the cover id, which can be used to get the cover data. The id is not guaranteed to be valid.
Sourcepub fn get_cover(&mut self) -> Option<(Vec<u8>, String)>
pub fn get_cover(&mut self) -> Option<(Vec<u8>, String)>
Returns the cover’s content and mime-type
§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);
Returns None
if the cover can’t be found.
Sourcepub fn get_release_identifier(&self) -> Option<String>
pub fn get_release_identifier(&self) -> Option<String>
Returns Release Identifier defined at https://www.w3.org/publishing/epub32/epub-packages.html#sec-metadata-elem-identifiers-pid
Sourcepub fn get_resource_by_path<P: AsRef<Path>>(
&mut self,
path: P,
) -> Option<Vec<u8>>
pub fn get_resource_by_path<P: AsRef<Path>>( &mut self, path: P, ) -> Option<Vec<u8>>
Returns the resource content by full path in the epub archive
Returns None
if the path doesn’t exist in the epub
Sourcepub fn get_resource(&mut self, id: &str) -> Option<(Vec<u8>, String)>
pub fn get_resource(&mut self, id: &str) -> Option<(Vec<u8>, String)>
Returns the resource content and mime-type by the id defined in the spine
Returns None
if the id doesn’t exists in the epub
Sourcepub fn get_resource_str_by_path<P: AsRef<Path>>(
&mut self,
path: P,
) -> Option<String>
pub fn get_resource_str_by_path<P: AsRef<Path>>( &mut self, path: P, ) -> Option<String>
Returns the resource content by full path in the epub archive, as String
Returns None
if the path doesn’t exists in the epub
Sourcepub fn get_resource_str(&mut self, id: &str) -> Option<(String, String)>
pub fn get_resource_str(&mut self, id: &str) -> Option<(String, String)>
Returns the resource content and mime-type by the id defined in the spine, as String
Returns None
if the id doesn’t exists in the epub
Sourcepub fn get_resource_mime(&self, id: &str) -> Option<String>
pub fn get_resource_mime(&self, id: &str) -> Option<String>
Sourcepub fn get_current(&mut self) -> Option<(Vec<u8>, String)>
pub fn get_current(&mut self) -> Option<(Vec<u8>, String)>
Returns the current chapter content and mime-type
The current follows the epub spine order. You can modify the current
calling to go_next
, go_prev
or set_current
methods.
Can return None
if the epub is broken.
Sourcepub fn get_current_str(&mut self) -> Option<(String, String)>
pub fn get_current_str(&mut self) -> Option<(String, String)>
Sourcepub fn get_current_with_epub_uris(&mut self) -> Result<Vec<u8>, DocError>
pub fn get_current_with_epub_uris(&mut self) -> Result<Vec<u8>, DocError>
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 Self::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/"));
§Errors
Returns DocError::InvalidEpub
if the epub is broken.
Sourcepub fn get_current_mime(&self) -> Option<String>
pub fn get_current_mime(&self) -> Option<String>
Sourcepub fn get_current_path(&self) -> Option<PathBuf>
pub fn get_current_path(&self) -> Option<PathBuf>
Sourcepub fn get_current_id(&self) -> Option<String>
pub fn get_current_id(&self) -> Option<String>
Sourcepub fn get_num_pages(&self) -> usize
pub fn get_num_pages(&self) -> usize
Sourcepub fn get_current_page(&self) -> usize
pub fn get_current_page(&self) -> usize
Returns the current chapter number, starting from 0
Sourcepub fn set_current_page(&mut self, n: usize) -> bool
pub fn set_current_page(&mut self, n: usize) -> bool
Sourcepub fn add_extra_css(&mut self, css: &str)
pub fn add_extra_css(&mut self, css: &str)
This will inject this css in every html page getted with
Self::get_current_with_epub_uris
§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));
Sourcepub fn resource_uri_to_chapter(&self, uri: &PathBuf) -> Option<usize>
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 resource 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
Sourcepub fn resource_id_to_chapter(&self, uri: &str) -> Option<usize>
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