From 31f638eae3f5ed8f69b29b1fca649a4d0a985ab2 Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Fri, 31 Jul 2015 15:06:08 +0200 Subject: [PATCH] Add active class to currently viewed chapter in the sidebar. Closes #20 --- book-example/book/README.html | 2 +- book-example/book/cli/build.html | 2 +- book-example/book/cli/cli-tool.html | 2 +- book-example/book/cli/init.html | 2 +- book-example/book/index.html | 2 +- .../html_handlebars/hbs_toc_helper.rs | 10 +++++++- src/renderer/html_handlebars/mod.rs | 23 ++++--------------- 7 files changed, 18 insertions(+), 25 deletions(-) diff --git a/book-example/book/README.html b/book-example/book/README.html index 34e918e4..b4b14455 100644 --- a/book-example/book/README.html +++ b/book-example/book/README.html @@ -14,7 +14,7 @@
diff --git a/book-example/book/cli/build.html b/book-example/book/cli/build.html index 36601a07..05879026 100644 --- a/book-example/book/cli/build.html +++ b/book-example/book/cli/build.html @@ -14,7 +14,7 @@
diff --git a/book-example/book/cli/cli-tool.html b/book-example/book/cli/cli-tool.html index fa8c85b8..f9a77206 100644 --- a/book-example/book/cli/cli-tool.html +++ b/book-example/book/cli/cli-tool.html @@ -14,7 +14,7 @@
diff --git a/book-example/book/cli/init.html b/book-example/book/cli/init.html index bc5225a4..6975b848 100644 --- a/book-example/book/cli/init.html +++ b/book-example/book/cli/init.html @@ -14,7 +14,7 @@
diff --git a/book-example/book/index.html b/book-example/book/index.html index 34e918e4..b4b14455 100644 --- a/book-example/book/index.html +++ b/book-example/book/index.html @@ -14,7 +14,7 @@
diff --git a/src/renderer/html_handlebars/hbs_toc_helper.rs b/src/renderer/html_handlebars/hbs_toc_helper.rs index 0a5dd8da..1400799a 100644 --- a/src/renderer/html_handlebars/hbs_toc_helper.rs +++ b/src/renderer/html_handlebars/hbs_toc_helper.rs @@ -18,6 +18,7 @@ impl HelperDef for RenderToc { // 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("
    ".as_bytes())); @@ -58,7 +59,14 @@ impl HelperDef for RenderToc { .to_str().unwrap().as_bytes() )); - try!(rc.writer.write("\">".as_bytes())); + try!(rc.writer.write("\"".as_bytes())); + + println!("[DEBUG] path: {}\n current: {}", path, ¤t); + if path == ¤t { + try!(rc.writer.write("class=\"active\"".as_bytes())); + } + + try!(rc.writer.write(">".as_bytes())); true } else { false diff --git a/src/renderer/html_handlebars/mod.rs b/src/renderer/html_handlebars/mod.rs index 30f4cbf7..62b23b07 100644 --- a/src/renderer/html_handlebars/mod.rs +++ b/src/renderer/html_handlebars/mod.rs @@ -53,6 +53,10 @@ impl Renderer for HtmlHandlebars { // Render markdown using the pulldown-cmark crate content = render_html(&content); + // Remove content from previous file and render content for this one + data.remove("path"); + data.insert("path".to_string(), item.path.to_str().unwrap().to_json()); + // Remove content from previous file and render content for this one data.remove("content"); data.insert("content".to_string(), content.to_json()); @@ -169,25 +173,6 @@ fn create_file(working_directory: &Path, path: &Path) -> Result fn make_data(book: BookItems, config: &BookConfig) -> Result, Box> { - /* - Function to make the JSon data for the handlebars template: - - { - "language": ..., - "title": ... - "chapters": [ - { - "section": section, - "chapter": BookItem, - }, - { - ... - }, - ], - } - - */ - let mut data = BTreeMap::new(); data.insert("language".to_string(), "en".to_json()); data.insert("title".to_string(), config.title.to_json());