From a77fe94c02913cc20a9cfeec6b0e4c438c5538a0 Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Mon, 3 Aug 2015 22:09:26 +0200 Subject: [PATCH 1/2] Started to clean the 'previous' and 'next' handlebars helpers #26 But got stuck, waiting for a response --- book-example/book/README.html | 6 +- book-example/book/cli/build.html | 8 +- book-example/book/cli/cli-tool.html | 8 +- book-example/book/cli/init.html | 8 +- book-example/book/format/config.html | 8 +- book-example/book/index.html | 6 +- .../html_handlebars/hbs_navigation_helper.rs | 81 ++++++++++++++----- src/theme/index.hbs | 6 +- 8 files changed, 89 insertions(+), 42 deletions(-) diff --git a/book-example/book/README.html b/book-example/book/README.html index e84bc906..1d370558 100644 --- a/book-example/book/README.html +++ b/book-example/book/README.html @@ -24,7 +24,7 @@

mdBook Documentation

- +

mdBook

mdBook is a command line tool and Rust library to create books using Markdown. @@ -42,9 +42,7 @@ Issues and feature requests can be posted on the + diff --git a/book-example/book/cli/build.html b/book-example/book/cli/build.html index d1817cce..35cd3293 100644 --- a/book-example/book/cli/build.html +++ b/book-example/book/cli/build.html @@ -24,7 +24,7 @@

mdBook Documentation

- +

The build command

The build command is used to render your book:

@@ -49,9 +49,11 @@ current working directory.

Doesn't seem to work: using JavaScript alternative until I find a way to do it in the template --> - + diff --git a/book-example/book/cli/cli-tool.html b/book-example/book/cli/cli-tool.html index 092236fc..ffb7adae 100644 --- a/book-example/book/cli/cli-tool.html +++ b/book-example/book/cli/cli-tool.html @@ -24,7 +24,7 @@

mdBook Documentation

- +

Command Line Tool

mdBook can be used either as a command line tool or a Rust library. @@ -37,9 +37,11 @@ Let's focus on the command line tool capabilities first.

Doesn't seem to work: using JavaScript alternative until I find a way to do it in the template --> - + diff --git a/book-example/book/cli/init.html b/book-example/book/cli/init.html index 6bb81684..e6e7c6e4 100644 --- a/book-example/book/cli/init.html +++ b/book-example/book/cli/init.html @@ -24,7 +24,7 @@

mdBook Documentation

- +

The init command

The init command, used like this:

@@ -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 --> - + diff --git a/book-example/book/format/config.html b/book-example/book/format/config.html index c6bf15d3..95c4cdf2 100644 --- a/book-example/book/format/config.html +++ b/book-example/book/format/config.html @@ -24,7 +24,7 @@

mdBook Documentation

- +

Configuration

You can configure the parameters for your book in the book.json file.

@@ -48,9 +48,11 @@ Doesn't seem to work: using JavaScript alternative until I find a way to do it in the template --> - + diff --git a/book-example/book/index.html b/book-example/book/index.html index e84bc906..1d370558 100644 --- a/book-example/book/index.html +++ b/book-example/book/index.html @@ -24,7 +24,7 @@

mdBook Documentation

- +

mdBook

mdBook is a command line tool and Rust library to create books using Markdown. @@ -42,9 +42,7 @@ Issues and feature requests can be posted on the + diff --git a/src/renderer/html_handlebars/hbs_navigation_helper.rs b/src/renderer/html_handlebars/hbs_navigation_helper.rs index b7a699c3..817d37e5 100644 --- a/src/renderer/html_handlebars/hbs_navigation_helper.rs +++ b/src/renderer/html_handlebars/hbs_navigation_helper.rs @@ -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> = json::decode(&chapters.to_string()).unwrap(); - - let mut previous = PathBuf::from(""); + let decoded: Vec> = json::decode(&chapters.to_string()).unwrap(); + let mut previous: Option> = 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(()) } diff --git a/src/theme/index.hbs b/src/theme/index.hbs index a2b028bc..5f8839b5 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -24,7 +24,7 @@

{{ title }}

- +
{{{ content }}}
@@ -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 --> - + {{/previous}} From 842196b91cdbaefbb0439decbba073574d8e4c21 Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Tue, 4 Aug 2015 12:51:07 +0200 Subject: [PATCH 2/2] 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 --- book-example/book/README.html | 2 + book-example/book/book.js | 9 -- book-example/book/cli/build.html | 6 +- book-example/book/cli/cli-tool.html | 6 +- book-example/book/cli/init.html | 6 +- book-example/book/format/config.html | 8 +- book-example/book/index.html | 2 + .../html_handlebars/hbs_navigation_helper.rs | 87 ++++++++++++++----- src/theme/book.js | 9 -- src/theme/index.hbs | 4 +- 10 files changed, 88 insertions(+), 51 deletions(-) diff --git a/book-example/book/README.html b/book-example/book/README.html index 1d370558..c9ff38be 100644 --- a/book-example/book/README.html +++ b/book-example/book/README.html @@ -46,9 +46,11 @@ Issues and feature requests can be posted on the + diff --git a/book-example/book/book.js b/book-example/book/book.js index d3cff82f..643755e5 100644 --- a/book-example/book/book.js +++ b/book-example/book/book.js @@ -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(); - } - }); - }); diff --git a/book-example/book/cli/build.html b/book-example/book/cli/build.html index 35cd3293..93b623c4 100644 --- a/book-example/book/cli/build.html +++ b/book-example/book/cli/build.html @@ -50,16 +50,18 @@ current working directory.

alternative until I find a way to do it in the template --> - + + diff --git a/book-example/book/cli/cli-tool.html b/book-example/book/cli/cli-tool.html index ffb7adae..353fe924 100644 --- a/book-example/book/cli/cli-tool.html +++ b/book-example/book/cli/cli-tool.html @@ -38,16 +38,18 @@ Let's focus on the command line tool capabilities first.

alternative until I find a way to do it in the template --> - + + diff --git a/book-example/book/cli/init.html b/book-example/book/cli/init.html index e6e7c6e4..2044d848 100644 --- a/book-example/book/cli/init.html +++ b/book-example/book/cli/init.html @@ -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 --> - + + diff --git a/book-example/book/format/config.html b/book-example/book/format/config.html index 95c4cdf2..f37a6042 100644 --- a/book-example/book/format/config.html +++ b/book-example/book/format/config.html @@ -49,16 +49,14 @@ alternative until I find a way to do it in the template --> - - + diff --git a/book-example/book/index.html b/book-example/book/index.html index 1d370558..c9ff38be 100644 --- a/book-example/book/index.html +++ b/book-example/book/index.html @@ -46,9 +46,11 @@ Issues and feature requests can be posted on the + diff --git a/src/renderer/html_handlebars/hbs_navigation_helper.rs b/src/renderer/html_handlebars/hbs_navigation_helper.rs index 817d37e5..cb8209e4 100644 --- a/src/renderer/html_handlebars/hbs_navigation_helper.rs +++ b/src/renderer/html_handlebars/hbs_navigation_helper.rs @@ -56,7 +56,7 @@ pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext Path::new(previous.get("path").unwrap()) .with_extension("html") ).to_str().unwrap()); - + previous_chapter.insert( "link".to_string(), path_to_root.join( @@ -67,11 +67,11 @@ pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext debug!("[*]: Inject in context"); // Inject in current context - c.extend(&previous_chapter); + let updated_context = c.extend(&previous_chapter); debug!("[*]: Render template"); // Render template - _h.template().unwrap().render(c, r, rc).unwrap(); + _h.template().unwrap().render(&updated_context, r, rc).unwrap(); } 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 // 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("\"", ""); + 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> = json::decode(&chapters.to_string()).unwrap(); - - let mut is_current = false; + let decoded: Vec> = json::decode(&chapters.to_string()).unwrap(); + let mut previous: Option> = 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") { - if path.len() > 0 { - if is_current { - let mut next = PathBuf::from(path); - next.set_extension("html"); - try!(rc.writer.write(path_to_root.as_bytes())); - try!(rc.writer.write(next.to_str().unwrap().as_bytes())); - break; - } else if path == ¤t { - is_current = true; + match item.get("path") { + + Some(path) if path.len() > 0 => { + + if let Some(previous) = previous { + if previous.get("path").unwrap() == ¤t { + + debug!("[*]: Found current chapter"); + debug!("[*]: Creating BTreeMap to inject in context"); + // 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(()) } diff --git a/src/theme/book.js b/src/theme/book.js index d3cff82f..643755e5 100644 --- a/src/theme/book.js +++ b/src/theme/book.js @@ -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(); - } - }); - }); diff --git a/src/theme/index.hbs b/src/theme/index.hbs index 5f8839b5..217e1455 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -42,9 +42,11 @@ - + {{/next}}