diff --git a/src/book/bookconfig.rs b/src/book/bookconfig.rs index 32c40305..2e958cb0 100644 --- a/src/book/bookconfig.rs +++ b/src/book/bookconfig.rs @@ -2,8 +2,8 @@ use std::path::{Path, PathBuf}; #[derive(Debug, Clone)] pub struct BookConfig { - title: String, - author: String, + pub title: String, + pub author: String, dest: PathBuf, src: PathBuf, indent_spaces: i32, @@ -40,14 +40,4 @@ impl BookConfig { self.src = src.to_owned(); self } - - pub fn set_title(&mut self, title: &str) -> &mut Self { - self.title = title.to_owned(); - self - } - - pub fn set_author(&mut self, author: &str) -> &mut Self { - self.author = author.to_owned(); - self - } } diff --git a/src/book/mdbook.rs b/src/book/mdbook.rs index a81b43eb..1890bcc4 100644 --- a/src/book/mdbook.rs +++ b/src/book/mdbook.rs @@ -122,12 +122,12 @@ impl MDBook { } pub fn set_title(mut self, title: &str) -> Self { - self.config.set_title(title); + self.config.title = title.to_owned(); self } pub fn set_author(mut self, author: &str) -> Self { - self.config.set_author(author); + self.config.author = author.to_owned(); self } diff --git a/src/renderer/html_handlebars.rs b/src/renderer/html_handlebars.rs index 3f10a956..f78642f5 100644 --- a/src/renderer/html_handlebars.rs +++ b/src/renderer/html_handlebars.rs @@ -12,9 +12,9 @@ use std::error::Error; use std::io::{self, Read, Write}; use std::collections::BTreeMap; -use self::handlebars::Handlebars; +use self::handlebars::{Handlebars, HelperDef, RenderError, RenderContext, Helper, Context, JsonRender}; use self::rustc_serialize::json::{Json, ToJson}; - +use self::rustc_serialize::{json, Decodable, Decoder}; use self::pulldown_cmark::Parser; use self::pulldown_cmark::html; @@ -31,6 +31,9 @@ impl Renderer for HtmlHandlebars { // Register template try!(handlebars.register_template_string("index", t.to_owned())); + // Register helper + handlebars.register_helper("toc", Box::new(RenderToc)); + let mut data = try!(make_data(book.clone(), config)); // Render a file for every entry in the book @@ -152,7 +155,8 @@ fn make_data(book: BookItems, config: &BookConfig) -> Result Result String { html::push_html(&mut s, p); s } + +// Handlebars helper to construct TOC +// implement by a structure impls HelperDef +#[derive(Clone, Copy)] +struct RenderToc; + +impl HelperDef for RenderToc { + fn call(&self, c: &Context, h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> { + let param = h.params().get(0).unwrap(); + + // 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 value = c.navigate(rc.get_path(), param); + try!(rc.writer.write("".as_bytes())); + Ok(()) + } +} diff --git a/src/theme/book.css b/src/theme/book.css index 4650e917..ddf48caa 100644 --- a/src/theme/book.css +++ b/src/theme/book.css @@ -1,5 +1,6 @@ html, body { font-family: "Open Sans", sans-serif; + color: #333; } @media only screen { diff --git a/src/theme/index.hbs b/src/theme/index.hbs index 72272ab3..529f6b9c 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -14,13 +14,7 @@