diff --git a/src/renderer/html_handlebars/helpers/navigation.rs b/src/renderer/html_handlebars/helpers/navigation.rs index da840590..429e3dd3 100644 --- a/src/renderer/html_handlebars/helpers/navigation.rs +++ b/src/renderer/html_handlebars/helpers/navigation.rs @@ -4,7 +4,6 @@ use std::collections::BTreeMap; use serde_json; use handlebars::{Context, Handlebars, Helper, RenderContext, RenderError, Renderable}; - type StringMap = BTreeMap; /// Target for `find_chapter`. @@ -15,22 +14,23 @@ enum Target { impl Target { /// Returns target if found. - fn find(&self, - base_path: &String, - current_path: &String, - current_item: &StringMap, - previous_item: &StringMap, - ) -> Result, RenderError> { + fn find( + &self, + base_path: &String, + current_path: &String, + current_item: &StringMap, + previous_item: &StringMap, + ) -> Result, RenderError> { match self { &Target::Next => { - let previous_path = previous_item.get("path").ok_or_else(|| { - RenderError::new("No path found for chapter in JSON data") - })?; + let previous_path = previous_item + .get("path") + .ok_or_else(|| RenderError::new("No path found for chapter in JSON data"))?; if previous_path == base_path { return Ok(Some(current_item.clone())); } - }, + } &Target::Previous => { if current_path == base_path { @@ -43,21 +43,18 @@ impl Target { } } -fn find_chapter( - rc: &mut RenderContext, - target: Target -) -> Result, RenderError> { +fn find_chapter(rc: &mut RenderContext, target: Target) -> Result, RenderError> { debug!("Get data from context"); - let chapters = rc.evaluate_absolute("chapters").and_then(|c| { + let chapters = rc.evaluate_absolute("chapters", true).and_then(|c| { serde_json::value::from_value::>(c.clone()) .map_err(|_| RenderError::new("Could not decode the JSON data")) })?; - let base_path = rc.evaluate_absolute("path")? - .as_str() - .ok_or_else(|| RenderError::new("Type error for `path`, string expected"))? - .replace("\"", ""); + let base_path = rc.evaluate_absolute("path", true)? + .as_str() + .ok_or_else(|| RenderError::new("Type error for `path`, string expected"))? + .replace("\"", ""); let mut previous: Option = None; @@ -78,7 +75,7 @@ fn find_chapter( } } - Ok(None) + Ok(None) } fn render( @@ -91,18 +88,21 @@ fn render( let mut context = BTreeMap::new(); - chapter.get("name") - .ok_or_else(|| RenderError::new("No title found for chapter in JSON data")) - .map(|name| context.insert("title".to_owned(), json!(name)))?; + chapter + .get("name") + .ok_or_else(|| RenderError::new("No title found for chapter in JSON data")) + .map(|name| context.insert("title".to_owned(), json!(name)))?; - chapter.get("path") - .ok_or_else(|| RenderError::new("No path found for chapter in JSON data")) - .and_then(|p| { - Path::new(p).with_extension("html") - .to_str() - .ok_or_else(|| RenderError::new("Link could not be converted to str")) - .map(|p| context.insert("link".to_owned(), json!(p.replace("\\", "/")))) - })?; + chapter + .get("path") + .ok_or_else(|| RenderError::new("No path found for chapter in JSON data")) + .and_then(|p| { + Path::new(p) + .with_extension("html") + .to_str() + .ok_or_else(|| RenderError::new("Link could not be converted to str")) + .map(|p| context.insert("link".to_owned(), json!(p.replace("\\", "/")))) + })?; trace!("Render template"); @@ -138,14 +138,14 @@ pub fn next(_h: &Helper, r: &Handlebars, rc: &mut RenderContext) -> Result<(), R #[cfg(test)] mod tests { - use super::*; + use super::*; - static TEMPLATE: &'static str = - "{{#previous}}{{title}}: {{link}}{{/previous}}|{{#next}}{{title}}: {{link}}{{/next}}"; + static TEMPLATE: &'static str = + "{{#previous}}{{title}}: {{link}}{{/previous}}|{{#next}}{{title}}: {{link}}{{/next}}"; - #[test] - fn test_next_previous() { - let data = json!({ + #[test] + fn test_next_previous() { + let data = json!({ "name": "two", "path": "two.path", "chapters": [ @@ -164,18 +164,19 @@ mod tests { ] }); - let mut h = Handlebars::new(); - h.register_helper("previous", Box::new(previous)); - h.register_helper("next", Box::new(next)); + let mut h = Handlebars::new(); + h.register_helper("previous", Box::new(previous)); + h.register_helper("next", Box::new(next)); - assert_eq!( - h.template_render(TEMPLATE, &data).unwrap(), - "one: one.html|three: three.html"); - } + assert_eq!( + h.render_template(TEMPLATE, &data).unwrap(), + "one: one.html|three: three.html" + ); + } - #[test] - fn test_first() { - let data = json!({ + #[test] + fn test_first() { + let data = json!({ "name": "one", "path": "one.path", "chapters": [ @@ -194,17 +195,18 @@ mod tests { ] }); - let mut h = Handlebars::new(); - h.register_helper("previous", Box::new(previous)); - h.register_helper("next", Box::new(next)); + let mut h = Handlebars::new(); + h.register_helper("previous", Box::new(previous)); + h.register_helper("next", Box::new(next)); - assert_eq!( - h.template_render(TEMPLATE, &data).unwrap(), - "|two: two.html"); - } - #[test] - fn test_last() { - let data = json!({ + assert_eq!( + h.render_template(TEMPLATE, &data).unwrap(), + "|two: two.html" + ); + } + #[test] + fn test_last() { + let data = json!({ "name": "three", "path": "three.path", "chapters": [ @@ -223,12 +225,13 @@ mod tests { ] }); - let mut h = Handlebars::new(); - h.register_helper("previous", Box::new(previous)); - h.register_helper("next", Box::new(next)); + let mut h = Handlebars::new(); + h.register_helper("previous", Box::new(previous)); + h.register_helper("next", Box::new(next)); - assert_eq!( - h.template_render(TEMPLATE, &data).unwrap(), - "two: two.html|"); - } + assert_eq!( + h.render_template(TEMPLATE, &data).unwrap(), + "two: two.html|" + ); + } } diff --git a/src/renderer/html_handlebars/helpers/toc.rs b/src/renderer/html_handlebars/helpers/toc.rs index 922a0db5..f44ea2ac 100644 --- a/src/renderer/html_handlebars/helpers/toc.rs +++ b/src/renderer/html_handlebars/helpers/toc.rs @@ -8,7 +8,7 @@ use pulldown_cmark::{html, Event, Parser, Tag}; // Handlebars helper to construct TOC #[derive(Clone, Copy)] pub struct RenderToc { - pub no_section_label: bool + pub no_section_label: bool, } impl HelperDef for RenderToc { @@ -16,14 +16,14 @@ impl HelperDef for RenderToc { // get value from context data // rc.get_path() is current json parent path, you should always use it like this // param is the key of value you want to display - let chapters = rc.evaluate_absolute("chapters").and_then(|c| { + let chapters = rc.evaluate_absolute("chapters", true).and_then(|c| { serde_json::value::from_value::>>(c.clone()) .map_err(|_| RenderError::new("Could not decode the JSON data")) })?; - let current = rc.evaluate_absolute("path")? - .as_str() - .ok_or_else(|| RenderError::new("Type error for `path`, string expected"))? - .replace("\"", ""); + let current = rc.evaluate_absolute("path", true)? + .as_str() + .ok_or_else(|| RenderError::new("Type error for `path`, string expected"))? + .replace("\"", ""); rc.writer.write_all(b"
    ")?; @@ -107,12 +107,12 @@ impl HelperDef for RenderToc { // filter all events that are not inline code blocks let parser = Parser::new(name).filter(|event| match *event { - Event::Start(Tag::Code) | - Event::End(Tag::Code) | - Event::InlineHtml(_) | - Event::Text(_) => true, - _ => false, - }); + Event::Start(Tag::Code) + | Event::End(Tag::Code) + | Event::InlineHtml(_) + | Event::Text(_) => true, + _ => false, + }); // render markdown to html let mut markdown_parsed_name = String::with_capacity(name.len() * 3 / 2);