Cleaned up the handlebars helpers, it's a lot more customizable because the user can put whatever template he wants inside and I just expose link and name of previous / next chapter. JavaScript part is removed. #26

This commit is contained in:
Mathieu David 2015-08-04 12:51:07 +02:00
parent a77fe94c02
commit 842196b91c
10 changed files with 88 additions and 51 deletions

View File

@ -46,9 +46,11 @@ Issues and feature requests can be posted on the <a href="https://github.com/aze
<!-- --> <!-- -->
<!-- --> <!-- -->
<a href="cli/cli-tool.html" class="nav-chapters next"> <a href="cli/cli-tool.html" class="nav-chapters next">
<i class="fa fa-angle-right"></i> <i class="fa fa-angle-right"></i>
</a> </a>
<!-- --> <!-- -->
</div> </div>

View File

@ -14,13 +14,4 @@ $( document ).ready(function() {
} }
}); });
// Hide navigation icons when there is no next or previous link
// JavaScript Solution until I find a way to do this in the template
$(".nav-chapters").each(function(){
if(!$(this).attr('href')){
this.remove();
}
});
}); });

View File

@ -50,16 +50,18 @@ current working directory.</p>
alternative until I find a way to do it in the template alternative until I find a way to do it in the template
--> -->
<a href="null" class="nav-chapters previous"> <a href="../cli/init.html" class="nav-chapters previous">
<i class="fa fa-angle-left"></i>null <i class="fa fa-angle-left"></i>
</a> </a>
<!-- --> <!-- -->
<!-- --> <!-- -->
<a href="../format/config.html" class="nav-chapters next"> <a href="../format/config.html" class="nav-chapters next">
<i class="fa fa-angle-right"></i> <i class="fa fa-angle-right"></i>
</a> </a>
<!-- --> <!-- -->
</div> </div>

View File

@ -38,16 +38,18 @@ Let's focus on the command line tool capabilities first.</p>
alternative until I find a way to do it in the template alternative until I find a way to do it in the template
--> -->
<a href="null" class="nav-chapters previous"> <a href="../README.html" class="nav-chapters previous">
<i class="fa fa-angle-left"></i>null <i class="fa fa-angle-left"></i>
</a> </a>
<!-- --> <!-- -->
<!-- --> <!-- -->
<a href="../cli/init.html" class="nav-chapters next"> <a href="../cli/init.html" class="nav-chapters next">
<i class="fa fa-angle-right"></i> <i class="fa fa-angle-right"></i>
</a> </a>
<!-- --> <!-- -->
</div> </div>

View File

@ -67,16 +67,18 @@ not create the corresponding files, init command should create the files for him
alternative until I find a way to do it in the template alternative until I find a way to do it in the template
--> -->
<a href="null" class="nav-chapters previous"> <a href="../cli/cli-tool.html" class="nav-chapters previous">
<i class="fa fa-angle-left"></i>null <i class="fa fa-angle-left"></i>
</a> </a>
<!-- --> <!-- -->
<!-- --> <!-- -->
<a href="../cli/build.html" class="nav-chapters next"> <a href="../cli/build.html" class="nav-chapters next">
<i class="fa fa-angle-right"></i> <i class="fa fa-angle-right"></i>
</a> </a>
<!-- --> <!-- -->
</div> </div>

View File

@ -49,16 +49,14 @@
alternative until I find a way to do it in the template alternative until I find a way to do it in the template
--> -->
<a href="null" class="nav-chapters previous"> <a href="../cli/build.html" class="nav-chapters previous">
<i class="fa fa-angle-left"></i>null <i class="fa fa-angle-left"></i>
</a> </a>
<!-- --> <!-- -->
<!-- --> <!-- -->
<a href="" class="nav-chapters next">
<i class="fa fa-angle-right"></i>
</a>
<!-- --> <!-- -->
</div> </div>

View File

@ -46,9 +46,11 @@ Issues and feature requests can be posted on the <a href="https://github.com/aze
<!-- --> <!-- -->
<!-- --> <!-- -->
<a href="cli/cli-tool.html" class="nav-chapters next"> <a href="cli/cli-tool.html" class="nav-chapters next">
<i class="fa fa-angle-right"></i> <i class="fa fa-angle-right"></i>
</a> </a>
<!-- --> <!-- -->
</div> </div>

View File

@ -56,7 +56,7 @@ pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext
Path::new(previous.get("path").unwrap()) Path::new(previous.get("path").unwrap())
.with_extension("html") .with_extension("html")
).to_str().unwrap()); ).to_str().unwrap());
previous_chapter.insert( previous_chapter.insert(
"link".to_string(), "link".to_string(),
path_to_root.join( path_to_root.join(
@ -67,11 +67,11 @@ pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext
debug!("[*]: Inject in context"); debug!("[*]: Inject in context");
// Inject in current context // Inject in current context
c.extend(&previous_chapter); let updated_context = c.extend(&previous_chapter);
debug!("[*]: Render template"); debug!("[*]: Render template");
// Render template // Render template
_h.template().unwrap().render(c, r, rc).unwrap(); _h.template().unwrap().render(&updated_context, r, rc).unwrap();
} }
break; break;
@ -88,36 +88,81 @@ pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext
pub fn next(c: &Context, _h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
pub fn next(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
debug!("[fn]: next (handlebars helper)");
debug!("[*]: Get data from context");
// get value from context data // get value from context data
// rc.get_path() is current json parent path, you should always use it like this // 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 // param is the key of value you want to display
let chapters = c.navigate(rc.get_path(), "chapters"); 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("\"", "");
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 // Decode json format
let decoded: Vec<BTreeMap<String,String>> = json::decode(&chapters.to_string()).unwrap(); let decoded: Vec<BTreeMap<String, String>> = json::decode(&chapters.to_string()).unwrap();
let mut previous: Option<BTreeMap<String, String>> = None;
let mut is_current = false;
debug!("[*]: Search for current Chapter");
// Search for current chapter and return previous entry
for item in decoded { for item in decoded {
if let Some(path) = item.get("path") { match item.get("path") {
if path.len() > 0 {
if is_current { Some(path) if path.len() > 0 => {
let mut next = PathBuf::from(path);
next.set_extension("html"); if let Some(previous) = previous {
try!(rc.writer.write(path_to_root.as_bytes())); if previous.get("path").unwrap() == &current {
try!(rc.writer.write(next.to_str().unwrap().as_bytes()));
break; debug!("[*]: Found current chapter");
} else if path == &current { debug!("[*]: Creating BTreeMap to inject in context");
is_current = true; // Create new BTreeMap to extend the context: 'title' and 'link'
let mut next_chapter = BTreeMap::new();
debug!("[*]: Inserting title: {}", item.get("name").unwrap());
next_chapter.insert("title".to_string(), item.get("name").unwrap().to_json());
debug!("[*]: Inserting link: {}",
path_to_root.join(
Path::new(item.get("path").unwrap())
.with_extension("html")
).to_str().unwrap());
next_chapter.insert(
"link".to_string(),
path_to_root.join(
Path::new(item.get("path").unwrap())
.with_extension("html")
).to_str().unwrap().to_json()
);
debug!("[*]: Inject in context");
// Inject in current context
let updated_context = c.extend(&next_chapter);
debug!("[*]: Render template");
// Render template
_h.template().unwrap().render(&updated_context, r, rc).unwrap();
break
}
} }
}
previous = Some(item.clone());
},
_ => continue,
} }
} }
Ok(()) Ok(())
} }

View File

@ -14,13 +14,4 @@ $( document ).ready(function() {
} }
}); });
// Hide navigation icons when there is no next or previous link
// JavaScript Solution until I find a way to do this in the template
$(".nav-chapters").each(function(){
if(!$(this).attr('href')){
this.remove();
}
});
}); });

View File

@ -42,9 +42,11 @@
<!-- {{!/if}} --> <!-- {{!/if}} -->
<!-- {{!#if (next "")}} --> <!-- {{!#if (next "")}} -->
<a href="{{#next}}{{/next}}" class="nav-chapters next"> {{#next}}
<a href="{{link}}" class="nav-chapters next">
<i class="fa fa-angle-right"></i> <i class="fa fa-angle-right"></i>
</a> </a>
{{/next}}
<!-- {{!/if}} --> <!-- {{!/if}} -->
</div> </div>