From 1111ff3cebe053c9534cd5f3045f8b2dca7908e3 Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Wed, 29 Jul 2015 23:32:01 +0200 Subject: [PATCH] Fixes 2 bugs with relative paths. Fixed by injecting a variable path_to_root into the json data for the handlebars template. Fixes #17 --- src/renderer/html_handlebars.rs | 27 +++++++++++++++++++++++---- src/theme/index.hbs | 6 +++--- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/renderer/html_handlebars.rs b/src/renderer/html_handlebars.rs index 88e66639..cd67ebd3 100644 --- a/src/renderer/html_handlebars.rs +++ b/src/renderer/html_handlebars.rs @@ -55,6 +55,10 @@ impl Renderer for HtmlHandlebars { data.remove("content"); data.insert("content".to_string(), content.to_json()); + // Remove path to root from previous file and render content for this one + data.remove("path_to_root"); + data.insert("path_to_root".to_string(), path_to_root(&item.path).to_json()); + // Rendere the handlebars template with the data let rendered = try!(handlebars.render("index", &data)); @@ -225,22 +229,35 @@ fn path_to_link(path: &Path) -> Option { Some(path) } +fn path_to_root(path: &Path) -> String { + // Remove filename and add "../" for every directory + + path.to_path_buf().parent().expect("") + .components().fold(String::new(), |mut s, c| { + match c { + Component::Normal(_) => s.push_str("../"), + _ => {} + } + s + }) +} + // Handlebars helper to construct TOC #[derive(Clone, Copy)] struct RenderToc; impl HelperDef for RenderToc { - fn call(&self, c: &Context, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> { - let param = h.params().get(0).unwrap(); + fn call(&self, c: &Context, _h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> { // 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 value = c.navigate(rc.get_path(), param); + let chapters = c.navigate(rc.get_path(), "chapters"); + let path_to_root = c.navigate(rc.get_path(), "path_to_root").to_string().replace("\"", ""); try!(rc.writer.write("