From 0a1e39a613c2788653bf365a0500b684117e7bee Mon Sep 17 00:00:00 2001 From: James Campos Date: Sat, 16 May 2020 22:03:45 -0700 Subject: filter empty chapters --- src/epub.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/epub.rs b/src/epub.rs index 90a53b5..4758a24 100644 --- a/src/epub.rs +++ b/src/epub.rs @@ -18,11 +18,7 @@ impl Epub { nav: Vec::new(), chapters: Vec::new(), }; - let nav = epub.get_nav(); - epub.nav.reserve_exact(nav.len()); - epub.chapters.reserve_exact(nav.len()); - for (path, label) in nav { - epub.nav.push(label); + let (nav, chapters) = epub.get_nav().into_iter().filter_map(|(path, label)| { let xml = epub.get_text(&path); // https://github.com/RazrFalcon/roxmltree/issues/12 // UnknownEntityReference for HTML entities @@ -30,8 +26,15 @@ impl Epub { let body = doc.root_element().last_element_child().unwrap(); let mut chapter = String::new(); Epub::render(&mut chapter, body); - epub.chapters.push(chapter); - } + if chapter.is_empty() { + None + } else { + Some((label, chapter)) + } + }) + .unzip(); + epub.nav = nav; + epub.chapters = chapters; Ok(epub) } fn render(buf: &mut String, n: Node) { -- cgit v1.2.3