Started to clean the 'previous' and 'next' handlebars helpers #26 But got stuck, waiting for a response
This commit is contained in:
parent
91b0a99d81
commit
a77fe94c02
|
@ -42,9 +42,7 @@ Issues and feature requests can be posted on the <a href="https://github.com/aze
|
|||
Doesn't seem to work: using JavaScript
|
||||
alternative until I find a way to do it in the template
|
||||
-->
|
||||
<a href="" class="nav-chapters previous">
|
||||
<i class="fa fa-angle-left"></i>
|
||||
</a>
|
||||
|
||||
<!-- -->
|
||||
|
||||
<!-- -->
|
||||
|
|
|
@ -49,9 +49,11 @@ current working directory.</p>
|
|||
Doesn't seem to work: using JavaScript
|
||||
alternative until I find a way to do it in the template
|
||||
-->
|
||||
<a href="../cli/init.html" class="nav-chapters previous">
|
||||
<i class="fa fa-angle-left"></i>
|
||||
|
||||
<a href="null" class="nav-chapters previous">
|
||||
<i class="fa fa-angle-left"></i>null
|
||||
</a>
|
||||
|
||||
<!-- -->
|
||||
|
||||
<!-- -->
|
||||
|
|
|
@ -37,9 +37,11 @@ Let's focus on the command line tool capabilities first.</p>
|
|||
Doesn't seem to work: using JavaScript
|
||||
alternative until I find a way to do it in the template
|
||||
-->
|
||||
<a href="../README.html" class="nav-chapters previous">
|
||||
<i class="fa fa-angle-left"></i>
|
||||
|
||||
<a href="null" class="nav-chapters previous">
|
||||
<i class="fa fa-angle-left"></i>null
|
||||
</a>
|
||||
|
||||
<!-- -->
|
||||
|
||||
<!-- -->
|
||||
|
|
|
@ -66,9 +66,11 @@ not create the corresponding files, init command should create the files for him
|
|||
Doesn't seem to work: using JavaScript
|
||||
alternative until I find a way to do it in the template
|
||||
-->
|
||||
<a href="../cli/cli-tool.html" class="nav-chapters previous">
|
||||
<i class="fa fa-angle-left"></i>
|
||||
|
||||
<a href="null" class="nav-chapters previous">
|
||||
<i class="fa fa-angle-left"></i>null
|
||||
</a>
|
||||
|
||||
<!-- -->
|
||||
|
||||
<!-- -->
|
||||
|
|
|
@ -48,9 +48,11 @@
|
|||
Doesn't seem to work: using JavaScript
|
||||
alternative until I find a way to do it in the template
|
||||
-->
|
||||
<a href="../cli/build.html" class="nav-chapters previous">
|
||||
<i class="fa fa-angle-left"></i>
|
||||
|
||||
<a href="null" class="nav-chapters previous">
|
||||
<i class="fa fa-angle-left"></i>null
|
||||
</a>
|
||||
|
||||
<!-- -->
|
||||
|
||||
<!-- -->
|
||||
|
|
|
@ -42,9 +42,7 @@ Issues and feature requests can be posted on the <a href="https://github.com/aze
|
|||
Doesn't seem to work: using JavaScript
|
||||
alternative until I find a way to do it in the template
|
||||
-->
|
||||
<a href="" class="nav-chapters previous">
|
||||
<i class="fa fa-angle-left"></i>
|
||||
</a>
|
||||
|
||||
<!-- -->
|
||||
|
||||
<!-- -->
|
||||
|
|
|
@ -1,47 +1,88 @@
|
|||
extern crate handlebars;
|
||||
extern crate rustc_serialize;
|
||||
|
||||
use std::path::{PathBuf};
|
||||
use std::path::{PathBuf, Path};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use self::rustc_serialize::json;
|
||||
use self::handlebars::{Handlebars, RenderError, RenderContext, Helper, Context};
|
||||
use self::rustc_serialize::json::{self, ToJson};
|
||||
use self::handlebars::{Handlebars, RenderError, RenderContext, Helper, Context, Renderable};
|
||||
|
||||
// Handlebars helper for navigation
|
||||
|
||||
pub fn previous(c: &Context, _h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
|
||||
pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
|
||||
debug!("[fn]: previous (handlebars helper)");
|
||||
|
||||
debug!("[*]: Get data from context");
|
||||
// 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()));
|
||||
let current = c.navigate(rc.get_path(), "path")
|
||||
.to_string()
|
||||
.replace("\"", "");
|
||||
|
||||
let path_to_root = PathBuf::from(
|
||||
c.navigate(rc.get_path(), "path_to_root")
|
||||
.to_string()
|
||||
.replace("\"", "")
|
||||
);
|
||||
|
||||
debug!("[*]: Decode chapters from JSON");
|
||||
// Decode json format
|
||||
let decoded: Vec<BTreeMap<String,String>> = json::decode(&chapters.to_string()).unwrap();
|
||||
|
||||
let mut previous = PathBuf::from("");
|
||||
let decoded: Vec<BTreeMap<String, String>> = json::decode(&chapters.to_string()).unwrap();
|
||||
let mut previous: Option<BTreeMap<String, String>> = None;
|
||||
|
||||
debug!("[*]: Search for current Chapter");
|
||||
// Search for current chapter and return previous entry
|
||||
for item in decoded {
|
||||
|
||||
if let Some(path) = item.get("path") {
|
||||
previous = if path.len() > 0 {
|
||||
|
||||
match item.get("path") {
|
||||
Some(path) if path.len() > 0 => {
|
||||
if path == ¤t {
|
||||
previous.set_extension("html");
|
||||
try!(rc.writer.write(previous.to_str().unwrap().as_bytes()));
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
PathBuf::from(path)
|
||||
|
||||
} else { previous }
|
||||
else {
|
||||
previous = Some(item.clone());
|
||||
}
|
||||
},
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
@ -34,9 +34,11 @@
|
|||
Doesn't seem to work: {{!#if (previous)}} using JavaScript
|
||||
alternative until I find a way to do it in the template
|
||||
-->
|
||||
<a href="{{#previous}}{{/previous}}" class="nav-chapters previous">
|
||||
{{#previous}}
|
||||
<a href="{{link}}" class="nav-chapters previous">
|
||||
<i class="fa fa-angle-left"></i>
|
||||
</a>
|
||||
{{/previous}}
|
||||
<!-- {{!/if}} -->
|
||||
|
||||
<!-- {{!#if (next "")}} -->
|
||||
|
|
Loading…
Reference in New Issue