From 99945542ca4af0184f8544ea7516537077e04769 Mon Sep 17 00:00:00 2001 From: Behnam Esfahbod Date: Sat, 2 Sep 2017 13:19:24 -0700 Subject: [PATCH] [renderer] Add normalize_path() On the web, the normalized path separator is forward-slash (`/`), so we use the built-in `is_separator()` method to replace any path separator with the forward-slash, to ensure consistent output on unix and windows machines. --- src/renderer/html_handlebars/hbs_renderer.rs | 33 ++++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 2f79e71e..c267ba66 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -65,7 +65,8 @@ impl HtmlHandlebars { let filepath = Path::new(&ch.path).with_extension("html"); let rendered = self.post_process(rendered, - filepath.to_str().unwrap_or(""), + &normalize_path(filepath.to_str() + .expect(&format!("Bad file name: {}", filepath.display()))), ctx.book.get_html_config().get_playpen_config()); // Write to file @@ -474,15 +475,7 @@ fn id_from_content(content: &str) -> String { content = content.replace(sub, ""); } - let mut id = String::new(); - for c in content.chars() { - if c.is_alphanumeric() || c == '_' { - id.push(c.to_ascii_lowercase()); - } else if c.is_whitespace() { - id.push('-'); - } - } - id + normalize_id(&content) } // anchors to the same page (href="#anchor") do not work because of @@ -590,6 +583,26 @@ struct RenderItemContext<'a> { is_index: bool, } +pub fn normalize_path(path: &str) -> String { + use std::path::is_separator; + path.chars() + .map(|ch| if is_separator(ch) { '/' } else { ch }) + .collect::() +} + +pub fn normalize_id(content: &str) -> String { + content.chars() + .filter_map(|ch| + if ch.is_alphanumeric() || ch == '_' { + Some(ch.to_ascii_lowercase()) + } else if ch.is_whitespace() { + Some('-') + } else { + None + } + ) + .collect::() +} #[cfg(test)]