Added handlebars helpers for navigation, need to find a place where to put them on the page #12
This commit is contained in:
parent
31f638eae3
commit
c64f3ac973
|
@ -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<BTreeMap<String,String>> = 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<BTreeMap<String,String>> = 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(())
|
||||||
|
}
|
|
@ -61,7 +61,6 @@ impl HelperDef for RenderToc {
|
||||||
|
|
||||||
try!(rc.writer.write("\"".as_bytes()));
|
try!(rc.writer.write("\"".as_bytes()));
|
||||||
|
|
||||||
println!("[DEBUG] path: {}\n current: {}", path, ¤t);
|
|
||||||
if path == ¤t {
|
if path == ¤t {
|
||||||
try!(rc.writer.write("class=\"active\"".as_bytes()));
|
try!(rc.writer.write("class=\"active\"".as_bytes()));
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ extern crate rustc_serialize;
|
||||||
extern crate pulldown_cmark;
|
extern crate pulldown_cmark;
|
||||||
|
|
||||||
mod hbs_toc_helper;
|
mod hbs_toc_helper;
|
||||||
|
mod hbs_navigation_helper;
|
||||||
use self::hbs_toc_helper::RenderToc;
|
use self::hbs_toc_helper::RenderToc;
|
||||||
|
|
||||||
use renderer::Renderer;
|
use renderer::Renderer;
|
||||||
|
@ -32,8 +33,10 @@ impl Renderer for HtmlHandlebars {
|
||||||
// Register template
|
// Register template
|
||||||
try!(handlebars.register_template_string("index", t.to_owned()));
|
try!(handlebars.register_template_string("index", t.to_owned()));
|
||||||
|
|
||||||
// Register helper
|
// Register helpers
|
||||||
handlebars.register_helper("toc", Box::new(RenderToc));
|
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));
|
let mut data = try!(make_data(book.clone(), config));
|
||||||
|
|
||||||
|
|
|
@ -126,10 +126,6 @@ pre {
|
||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.nav-previous-next {
|
|
||||||
margin-top: 60px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.left {
|
.left {
|
||||||
float: left;
|
float: left;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue