2015-07-31 18:34:43 +02:00
|
|
|
extern crate handlebars;
|
|
|
|
|
extern crate rustc_serialize;
|
|
|
|
|
|
2015-08-03 22:09:26 +02:00
|
|
|
use std::path::{PathBuf, Path};
|
2015-07-31 18:34:43 +02:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
|
2015-08-03 22:09:26 +02:00
|
|
|
use self::rustc_serialize::json::{self, ToJson};
|
|
|
|
|
use self::handlebars::{Handlebars, RenderError, RenderContext, Helper, Context, Renderable};
|
2015-07-31 18:34:43 +02:00
|
|
|
|
|
|
|
|
// Handlebars helper for navigation
|
|
|
|
|
|
2015-08-03 22:09:26 +02:00
|
|
|
pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
|
|
|
|
|
debug!("[fn]: previous (handlebars helper)");
|
2015-07-31 18:34:43 +02:00
|
|
|
|
2015-08-03 22:09:26 +02:00
|
|
|
debug!("[*]: Get data from context");
|
2015-07-31 18:34:43 +02:00
|
|
|
// 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");
|
|
|
|
|
|
2015-08-03 22:09:26 +02:00
|
|
|
let current = c.navigate(rc.get_path(), "path")
|
|
|
|
|
.to_string()
|
|
|
|
|
.replace("\"", "");
|
2015-07-31 18:34:43 +02:00
|
|
|
|
2015-08-03 22:09:26 +02:00
|
|
|
let path_to_root = PathBuf::from(
|
|
|
|
|
c.navigate(rc.get_path(), "path_to_root")
|
|
|
|
|
.to_string()
|
|
|
|
|
.replace("\"", "")
|
|
|
|
|
);
|
2015-07-31 18:34:43 +02:00
|
|
|
|
2015-08-03 22:09:26 +02:00
|
|
|
debug!("[*]: Decode chapters from JSON");
|
|
|
|
|
// Decode json format
|
|
|
|
|
let decoded: Vec<BTreeMap<String, String>> = json::decode(&chapters.to_string()).unwrap();
|
|
|
|
|
let mut previous: Option<BTreeMap<String, String>> = None;
|
2015-07-31 18:34:43 +02:00
|
|
|
|
2015-08-03 22:09:26 +02:00
|
|
|
debug!("[*]: Search for current Chapter");
|
|
|
|
|
// Search for current chapter and return previous entry
|
2015-07-31 18:34:43 +02:00
|
|
|
for item in decoded {
|
|
|
|
|
|
2015-08-03 22:09:26 +02:00
|
|
|
match item.get("path") {
|
|
|
|
|
Some(path) if path.len() > 0 => {
|
2015-07-31 18:34:43 +02:00
|
|
|
if path == ¤t {
|
|
|
|
|
|
2015-08-03 22:09:26 +02:00
|
|
|
debug!("[*]: Found current chapter");
|
|
|
|
|
if let Some(previous) = previous{
|
|
|
|
|
|
|
|
|
|
debug!("[*]: Creating BTreeMap to inject in context");
|
|
|
|
|
// Create new BTreeMap to extend the context: 'title' and 'link'
|
|
|
|
|
let mut previous_chapter = BTreeMap::new();
|
|
|
|
|
|
|
|
|
|
debug!("[*]: Inserting title: {}", previous.get("name").unwrap());
|
|
|
|
|
previous_chapter.insert("title".to_string(), previous.get("name").unwrap().to_json());
|
|
|
|
|
|
|
|
|
|
debug!("[*]: Inserting link: {}",
|
|
|
|
|
path_to_root.join(
|
|
|
|
|
Path::new(previous.get("path").unwrap())
|
|
|
|
|
.with_extension("html")
|
|
|
|
|
).to_str().unwrap());
|
|
|
|
|
|
|
|
|
|
previous_chapter.insert(
|
|
|
|
|
"link".to_string(),
|
|
|
|
|
path_to_root.join(
|
|
|
|
|
Path::new(previous.get("path").unwrap())
|
|
|
|
|
.with_extension("html")
|
|
|
|
|
).to_str().unwrap().to_json()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
debug!("[*]: Inject in context");
|
|
|
|
|
// Inject in current context
|
|
|
|
|
c.extend(&previous_chapter);
|
|
|
|
|
|
|
|
|
|
debug!("[*]: Render template");
|
|
|
|
|
// Render template
|
|
|
|
|
_h.template().unwrap().render(c, r, rc).unwrap();
|
|
|
|
|
}
|
2015-07-31 18:34:43 +02:00
|
|
|
|
2015-08-03 22:09:26 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
previous = Some(item.clone());
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
_ => continue,
|
2015-07-31 18:34:43 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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("\"", "");
|
|
|
|
|
|
|
|
|
|
// 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");
|
2015-08-01 14:12:55 +02:00
|
|
|
try!(rc.writer.write(path_to_root.as_bytes()));
|
2015-07-31 18:34:43 +02:00
|
|
|
try!(rc.writer.write(next.to_str().unwrap().as_bytes()));
|
|
|
|
|
break;
|
|
|
|
|
} else if path == ¤t {
|
|
|
|
|
is_current = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|