diff --git a/src/renderer/html_handlebars/hbs_navigation_helper.rs b/src/renderer/html_handlebars/hbs_navigation_helper.rs new file mode 100644 index 00000000..e14034dc --- /dev/null +++ b/src/renderer/html_handlebars/hbs_navigation_helper.rs @@ -0,0 +1,83 @@ +extern crate handlebars; +extern crate rustc_serialize; + +use std::path::{PathBuf}; +use std::collections::BTreeMap; + +use self::rustc_serialize::json; +use self::handlebars::{Handlebars, RenderError, RenderContext, Helper, Context}; + +// Handlebars helper for navigation + +pub fn previous(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 chapters = c.navigate(rc.get_path(), "chapters"); + let current = c.navigate(rc.get_path(), "path").to_string().replace("\"", ""); + let path_to_root = c.navigate(rc.get_path(), "path_to_root").to_string().replace("\"", ""); + + try!(rc.writer.write(path_to_root.as_bytes())); + + // Decode json format + let decoded: Vec> = json::decode(&chapters.to_string()).unwrap(); + + let mut previous = PathBuf::new(); + + for item in decoded { + + if let Some(path) = item.get("path") { + previous = if path.len() > 0 { + + if path == ¤t { + previous.set_extension("html"); + try!(rc.writer.write(previous.to_str().unwrap().as_bytes())); + break; + } + + PathBuf::from(path) + + } else { previous } + } + } + + Ok(()) +} + + + +pub fn next(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 chapters = c.navigate(rc.get_path(), "chapters"); + let current = c.navigate(rc.get_path(), "path").to_string().replace("\"", ""); + let path_to_root = c.navigate(rc.get_path(), "path_to_root").to_string().replace("\"", ""); + + try!(rc.writer.write(path_to_root.as_bytes())); + + // Decode json format + let decoded: Vec> = json::decode(&chapters.to_string()).unwrap(); + + let mut is_current = false; + + for item in decoded { + + if let Some(path) = item.get("path") { + if path.len() > 0 { + if is_current { + let mut next = PathBuf::from(path); + next.set_extension("html"); + try!(rc.writer.write(next.to_str().unwrap().as_bytes())); + break; + } else if path == ¤t { + is_current = true; + } + } + } + } + + Ok(()) +} diff --git a/src/renderer/html_handlebars/hbs_toc_helper.rs b/src/renderer/html_handlebars/hbs_toc_helper.rs index 1400799a..1246f9d3 100644 --- a/src/renderer/html_handlebars/hbs_toc_helper.rs +++ b/src/renderer/html_handlebars/hbs_toc_helper.rs @@ -61,7 +61,6 @@ impl HelperDef for RenderToc { try!(rc.writer.write("\"".as_bytes())); - println!("[DEBUG] path: {}\n current: {}", path, ¤t); if path == ¤t { try!(rc.writer.write("class=\"active\"".as_bytes())); } diff --git a/src/renderer/html_handlebars/mod.rs b/src/renderer/html_handlebars/mod.rs index 62b23b07..28786263 100644 --- a/src/renderer/html_handlebars/mod.rs +++ b/src/renderer/html_handlebars/mod.rs @@ -3,6 +3,7 @@ extern crate rustc_serialize; extern crate pulldown_cmark; mod hbs_toc_helper; +mod hbs_navigation_helper; use self::hbs_toc_helper::RenderToc; use renderer::Renderer; @@ -32,8 +33,10 @@ impl Renderer for HtmlHandlebars { // Register template try!(handlebars.register_template_string("index", t.to_owned())); - // Register helper + // Register helpers handlebars.register_helper("toc", Box::new(RenderToc)); + handlebars.register_helper("previous", Box::new(hbs_navigation_helper::previous)); + handlebars.register_helper("next", Box::new(hbs_navigation_helper::next)); let mut data = try!(make_data(book.clone(), config)); diff --git a/src/theme/book.css b/src/theme/book.css index 2c93872a..f8c20713 100644 --- a/src/theme/book.css +++ b/src/theme/book.css @@ -126,10 +126,6 @@ pre { border-radius: 3px; } -.nav-previous-next { - margin-top: 60px; -} - .left { float: left; }