aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJames Campos <james.r.campos@gmail.com>2020-05-16 22:03:45 -0700
committerJames Campos <james.r.campos@gmail.com>2020-05-16 22:03:45 -0700
commit0a1e39a613c2788653bf365a0500b684117e7bee (patch)
tree0415e863cba6188e4ee0d8b1c9758d5133c9e0a2 /src
parentd09560fa20287e5033cca5229649eac140f81e63 (diff)
downloadbk-0a1e39a613c2788653bf365a0500b684117e7bee.tar.gz
filter empty chapters
Diffstat (limited to 'src')
-rw-r--r--src/epub.rs17
1 files 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) {