Skip to content

Commit

Permalink
Merge pull request #47 from danigm/spine-attrs
Browse files Browse the repository at this point in the history
Store spine itemref optional attributes like linear
  • Loading branch information
danigm authored Jul 22, 2024
2 parents f719fed + 6568309 commit 9fc7119
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
21 changes: 16 additions & 5 deletions src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ impl PartialEq for NavPoint {
}
}

#[derive(Clone, Debug)]
pub struct SpineItem {
pub idref: String,
pub id: Option<String>,
pub properties: Option<String>,
pub linear: bool,
}

/// Struct to control the epub document
#[derive(Clone, Debug)]
pub struct EpubDoc<R: Read + Seek> {
Expand All @@ -68,7 +76,7 @@ pub struct EpubDoc<R: Read + Seek> {
current: usize,

/// epub spine ids
pub spine: Vec<String>,
pub spine: Vec<SpineItem>,

/// resource id -> (path, mime)
pub resources: HashMap<String, (PathBuf, String)>,
Expand Down Expand Up @@ -452,7 +460,7 @@ impl<R: Read + Seek> EpubDoc<R> {
///
/// Can return [`None`] if the epub is broken.
pub fn get_current_id(&self) -> Option<String> {
self.spine.get(self.current).cloned()
self.spine.get(self.current).cloned().map(|i| i.idref)
}

/// Changes current to the next chapter
Expand Down Expand Up @@ -592,7 +600,7 @@ impl<R: Read + Seek> EpubDoc<R> {
/// 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
pub fn resource_id_to_chapter(&self, uri: &str) -> Option<usize> {
self.spine.iter().position(|item| item == uri)
self.spine.iter().position(|item| item.idref == uri)
}

fn fill_resources(&mut self) -> Result<(), DocError> {
Expand Down Expand Up @@ -703,10 +711,13 @@ impl<R: Read + Seek> EpubDoc<R> {
}

fn insert_spine(&mut self, item: &xmlutils::XMLNode) -> Result<(), DocError> {
let id = item
let idref = item
.get_attr("idref")
.ok_or_else(|| XMLError::AttrNotFound("idref".into()))?;
self.spine.push(id);
let linear = item.get_attr("linear").unwrap_or("yes".into()) == "yes";
let properties = item.get_attr("properties");
let id = item.get_attr("id");
self.spine.push(SpineItem { idref, id, linear, properties });
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
//! # let doc = EpubDoc::new("test.epub");
//! # let doc = doc.unwrap();
//! assert_eq!(17, doc.spine.len());
//! assert_eq!("titlepage.xhtml", doc.spine[0]);
//! assert_eq!("titlepage.xhtml", doc.spine[0].idref);
//! ```
//!
//! ## Navigation using the doc internal state
Expand Down
2 changes: 1 addition & 1 deletion tests/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn doc_open() {

{
assert_eq!(17, doc.spine.len());
assert_eq!("titlepage.xhtml", doc.spine[0]);
assert_eq!("titlepage.xhtml", doc.spine[0].idref);
}

{
Expand Down

0 comments on commit 9fc7119

Please sign in to comment.