diff --git a/src/renderer/html_handlebars/hbs_toc_helper.rs b/src/renderer/html_handlebars/hbs_toc_helper.rs new file mode 100644 index 00000000..0a5dd8da --- /dev/null +++ b/src/renderer/html_handlebars/hbs_toc_helper.rs @@ -0,0 +1,87 @@ +extern crate handlebars; +extern crate rustc_serialize; + +use std::path::Path; +use std::collections::BTreeMap; + +use self::rustc_serialize::json; +use self::handlebars::{Handlebars, HelperDef, RenderError, RenderContext, Helper, Context}; + +// Handlebars helper to construct TOC +#[derive(Clone, Copy)] +pub struct RenderToc; + +impl HelperDef for RenderToc { + fn call(&self, 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 path_to_root = c.navigate(rc.get_path(), "path_to_root").to_string().replace("\"", ""); + try!(rc.writer.write("".as_bytes())); + Ok(()) + } +} diff --git a/src/renderer/html_handlebars.rs b/src/renderer/html_handlebars/mod.rs similarity index 67% rename from src/renderer/html_handlebars.rs rename to src/renderer/html_handlebars/mod.rs index 3c8abd4a..30f4cbf7 100644 --- a/src/renderer/html_handlebars.rs +++ b/src/renderer/html_handlebars/mod.rs @@ -2,6 +2,9 @@ extern crate handlebars; extern crate rustc_serialize; extern crate pulldown_cmark; +mod hbs_toc_helper; +use self::hbs_toc_helper::RenderToc; + use renderer::Renderer; use book::{BookItems, BookConfig}; use {theme, utils}; @@ -12,8 +15,8 @@ use std::error::Error; use std::io::{self, Read, Write}; use std::collections::BTreeMap; -use self::handlebars::{Handlebars, HelperDef, RenderError, RenderContext, Helper, Context, JsonRender}; -use self::rustc_serialize::json::{self, Json, ToJson}; +use self::handlebars::{Handlebars, JsonRender}; +use self::rustc_serialize::json::{Json, ToJson}; use self::pulldown_cmark::{Parser, html}; pub struct HtmlHandlebars; @@ -47,7 +50,7 @@ impl Renderer for HtmlHandlebars { try!(f.read_to_string(&mut content)); - // Render markdown using the pulldown-cmark + // Render markdown using the pulldown-cmark crate content = render_html(&content); // Remove content from previous file and render content for this one @@ -211,82 +214,3 @@ fn render_html(text: &str) -> String { html::push_html(&mut s, p); s } - -// Handlebars helper to construct TOC -#[derive(Clone, Copy)] -struct RenderToc; - -impl HelperDef for RenderToc { - fn call(&self, 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 path_to_root = c.navigate(rc.get_path(), "path_to_root").to_string().replace("\"", ""); - try!(rc.writer.write("".as_bytes())); - Ok(()) - } -}