Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,7 @@ impl<R: Read + Seek> EpubDoc<R> {

fn fill_toc(&mut self, id: &str) -> Result<(), DocError> {
let toc_res = self.resources.get(id).ok_or(DocError::InvalidEpub)?; // this should be turned into it's own error type, but
let toc_base_path = toc_res.path.parent().ok_or(DocError::InvalidEpub)?;

let container = self.archive.get_entry(&toc_res.path)?;
let root = xmlutils::XMLReader::parse(container.as_slice())?;
Expand All @@ -984,14 +985,14 @@ impl<R: Read + Seek> EpubDoc<R> {
.find("navMap")
.ok_or_else(|| XMLError::AttrNotFound("navMap".into()))?;

self.toc.append(&mut self.get_navpoints(&mapnode.borrow()));
self.toc.append(&mut self.get_navpoints(toc_base_path, &mapnode.borrow()));
self.toc.sort();

Ok(())
}

/// Recursively extract all navpoints from a node.
fn get_navpoints(&self, parent: &xmlutils::XMLNode) -> Vec<NavPoint> {
fn get_navpoints(&self, ncx_base_path: &Path, parent: &xmlutils::XMLNode) -> Vec<NavPoint> {
let mut navpoints = Vec::new();

// TODO: parse metadata (dtb:totalPageCount, dtb:depth, dtb:maxPageNumber)
Expand All @@ -1006,7 +1007,7 @@ impl<R: Read + Seek> EpubDoc<R> {
.and_then(|n| n.parse::<usize>().ok());
let content = item
.find("content")
.and_then(|c| c.borrow().get_attr("src").map(|p| self.root_base.join(p)));
.and_then(|c| c.borrow().get_attr("src").map(|p| ncx_base_path.join(p)));

let label = item.find("navLabel").and_then(|l| {
l.borrow()
Expand All @@ -1019,7 +1020,7 @@ impl<R: Read + Seek> EpubDoc<R> {
let navpoint = NavPoint {
label: label_text.clone(),
content: content_path.clone(),
children: self.get_navpoints(&item),
children: self.get_navpoints(ncx_base_path, &item),
play_order: order,
};
navpoints.push(navpoint);
Expand Down