Split of the creating of the JSon object into it's own function

This commit is contained in:
Mathieu David 2015-07-19 14:02:21 +02:00
parent 4d4f35ecba
commit be0bfa7170
5 changed files with 53 additions and 9 deletions

View file

@ -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<File, Box<Error>
Ok(file)
}
fn make_data(book: BookItems, config: &BookConfig) -> Result<Json, Box<Error>> {
/*
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())
}

0
src/theme/book.css Normal file
View file

0
src/theme/book.js Normal file
View file

View file

@ -10,7 +10,9 @@
<link rel="stylesheet" href="book.css" media="screen" title="no title" charset="utf-8">
</head>
<body>
{{ chapters }}
<script src="book.js"></script>
</body>
</html>

View file

@ -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
}