From be0bfa717082fff277017d3e2c1840cf361ad075 Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Sun, 19 Jul 2015 14:02:21 +0200 Subject: [PATCH] Split of the creating of the JSon object into it's own function --- src/renderer/html_handlebars.rs | 48 +++++++++++++++++++++++++++------ src/theme/book.css | 0 src/theme/book.js | 0 src/theme/index.hbs | 4 ++- src/theme/mod.rs | 10 +++++++ 5 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 src/theme/book.css create mode 100644 src/theme/book.js diff --git a/src/renderer/html_handlebars.rs b/src/renderer/html_handlebars.rs index d1be3696..5ce7c41a 100644 --- a/src/renderer/html_handlebars.rs +++ b/src/renderer/html_handlebars.rs @@ -26,14 +26,7 @@ impl Renderer for HtmlHandlebars { // Register template try!(handlebars.register_template_string("index", t.to_owned())); - let mut data = BTreeMap::new(); - let mut chapters: Vec<(String, BookItem)> = vec![]; - - // Hacky way to prevent move error... Not optimal - for (section, item) in book.clone() { - chapters.push((section, item.clone())); - } - data.insert("chapters".to_string(), chapters.to_json()); + let data = try!(make_data(book.clone(), config)); for (_, item) in book { @@ -119,3 +112,42 @@ fn create_file(working_directory: &Path, path: &Path) -> Result Ok(file) } + + +fn make_data(book: BookItems, config: &BookConfig) -> Result> { + + /* + Function to make the JSon data for the handlebars template: + + { + "language": ???, + "chapters": [ + { + "section": section, + "chapter": BookItem, + }, + { + ... + }, + ], + } + + */ + + let mut data = BTreeMap::new(); + data.insert("language".to_string(), "en".to_json()); + + let mut chapters = vec![]; + + for (section, item) in book { + let mut chapter = BTreeMap::new(); + chapter.insert("section".to_string(), section.to_json()); + chapter.insert("chapter".to_string(), item.to_json()); + + chapters.push(chapter); + } + + data.insert("chapters".to_string(), chapters.to_json()); + + Ok(data.to_json()) +} diff --git a/src/theme/book.css b/src/theme/book.css new file mode 100644 index 00000000..e69de29b diff --git a/src/theme/book.js b/src/theme/book.js new file mode 100644 index 00000000..e69de29b diff --git a/src/theme/index.hbs b/src/theme/index.hbs index 94d78319..6f5e969e 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -10,7 +10,9 @@ - + + {{ chapters }} + diff --git a/src/theme/mod.rs b/src/theme/mod.rs index e9d674ea..f572aa84 100644 --- a/src/theme/mod.rs +++ b/src/theme/mod.rs @@ -3,3 +3,13 @@ pub fn get_index_hbs() -> &'static str { let index = include_str!("index.hbs"); index } + +pub fn get_css() -> &'static str { + let css = include_str!("book.css"); + css +} + +pub fn get_js() -> &'static str { + let js = include_str!("book.js"); + js +}