From 0b267abb97922fd6e9f07f63e77b5b066cdaaa9a Mon Sep 17 00:00:00 2001 From: James Campos Date: Mon, 21 Sep 2020 06:16:11 -0700 Subject: normalize whitespace --- src/epub.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/epub.rs b/src/epub.rs index d09b100..2df924c 100644 --- a/src/epub.rs +++ b/src/epub.rs @@ -66,7 +66,7 @@ impl Epub { frag: Vec::new(), }; render(body, &mut c); - if c.text.is_empty() { + if c.text.trim().is_empty() { continue; } let relative = path.rsplit('/').next().unwrap(); @@ -164,8 +164,14 @@ impl Chapter { fn render(n: Node, c: &mut Chapter) { if n.is_text() { let text = n.text().unwrap(); - if !text.trim().is_empty() { - c.text.push_str(text); + let content: Vec<_> = text.split_ascii_whitespace().collect(); + + if text.starts_with(char::is_whitespace) { + c.text.push(' '); + } + c.text.push_str(&content.join(" ")); + if text.ends_with(char::is_whitespace) { + c.text.push(' '); } return; } -- cgit v1.2.3