Pulled page rendering out into its own method

This commit is contained in:
Michael Bryan 2017-06-15 17:43:44 +08:00
parent 9e9a08806d
commit 0d0deb7c40
1 changed files with 106 additions and 87 deletions

View File

@ -25,43 +25,10 @@ impl HtmlHandlebars {
pub fn new() -> Self {
HtmlHandlebars
}
}
impl Renderer for HtmlHandlebars {
fn render(&self, book: &MDBook) -> Result<(), Box<Error>> {
debug!("[fn]: render");
let mut handlebars = Handlebars::new();
// Load theme
let theme = theme::Theme::new(book.get_theme_path());
// Register template
debug!("[*]: Register handlebars template");
handlebars
.register_template_string("index", String::from_utf8(theme.index)?)?;
// Register helpers
debug!("[*]: Register handlebars helpers");
handlebars.register_helper("toc", Box::new(helpers::toc::RenderToc));
handlebars.register_helper("previous", Box::new(helpers::navigation::previous));
handlebars.register_helper("next", Box::new(helpers::navigation::next));
let mut data = make_data(book)?;
// Print version
let mut print_content: String = String::new();
// Check if dest directory exists
debug!("[*]: Check if destination directory exists");
if fs::create_dir_all(book.get_destination().expect("If the HTML renderer is called, one would assume the HtmlConfig is set... (2)")).is_err() {
return Err(Box::new(io::Error::new(io::ErrorKind::Other,
"Unexpected error when constructing destination path")));
}
// Render a file for every entry in the book
let mut index = true;
for item in book.iter() {
fn render_item(&self, item: &BookItem, book: &MDBook, data: &mut serde_json::Map<String, serde_json::Value>,
print_content: &mut String, handlebars: &mut Handlebars, index: &mut bool)
-> Result<(), Box<Error>> {
match *item {
BookItem::Chapter(_, ref ch) |
BookItem::Affix(ref ch) => {
@ -112,7 +79,7 @@ impl Renderer for HtmlHandlebars {
book.write_file(filename, &rendered.into_bytes())?;
// Create an index.html from the first element in SUMMARY.md
if index {
if *index {
debug!("[*]: index.html");
let mut content = String::new();
@ -139,12 +106,52 @@ impl Renderer for HtmlHandlebars {
.expect("If the HTML renderer is called, one would assume the HtmlConfig is set... (4)")
.join(&ch.path.with_extension("html"))
);
index = false;
*index = false;
}
}
},
_ => {},
}
Ok(())
}
}
impl Renderer for HtmlHandlebars {
fn render(&self, book: &MDBook) -> Result<(), Box<Error>> {
debug!("[fn]: render");
let mut handlebars = Handlebars::new();
// Load theme
let theme = theme::Theme::new(book.get_theme_path());
// Register template
debug!("[*]: Register handlebars template");
handlebars
.register_template_string("index", String::from_utf8(theme.index)?)?;
// Register helpers
debug!("[*]: Register handlebars helpers");
handlebars.register_helper("toc", Box::new(helpers::toc::RenderToc));
handlebars.register_helper("previous", Box::new(helpers::navigation::previous));
handlebars.register_helper("next", Box::new(helpers::navigation::next));
let mut data = make_data(book)?;
// Print version
let mut print_content: String = String::new();
// Check if dest directory exists
debug!("[*]: Check if destination directory exists");
if fs::create_dir_all(book.get_destination().expect("If the HTML renderer is called, one would assume the HtmlConfig is set... (2)")).is_err() {
return Err(Box::new(io::Error::new(io::ErrorKind::Other,
"Unexpected error when constructing destination path")));
}
// Render a file for every entry in the book
let mut index = true;
for item in book.iter() {
self.render_item(item, book, &mut data, &mut print_content, &mut handlebars, &mut index)?;
}
// Print version
@ -204,7 +211,7 @@ impl Renderer for HtmlHandlebars {
.expect("File has a file name")
.to_str()
.expect("Could not convert to str")
}
},
};
book.write_file(name, &data)?;
@ -244,7 +251,13 @@ fn make_data(book: &MDBook) -> Result<serde_json::Map<String, serde_json::Value>
for style in book.get_additional_css() {
match style.strip_prefix(book.get_root()) {
Ok(p) => css.push(p.to_str().expect("Could not convert to str")),
Err(_) => css.push(style.file_name().expect("File has a file name").to_str().expect("Could not convert to str")),
Err(_) => {
css.push(style
.file_name()
.expect("File has a file name")
.to_str()
.expect("Could not convert to str"))
},
}
}
data.insert("additional_css".to_owned(), json!(css));
@ -256,7 +269,13 @@ fn make_data(book: &MDBook) -> Result<serde_json::Map<String, serde_json::Value>
for script in book.get_additional_js() {
match script.strip_prefix(book.get_root()) {
Ok(p) => js.push(p.to_str().expect("Could not convert to str")),
Err(_) => js.push(script.file_name().expect("File has a file name").to_str().expect("Could not convert to str")),
Err(_) => {
js.push(script
.file_name()
.expect("File has a file name")
.to_str()
.expect("Could not convert to str"))
},
}
}
data.insert("additional_js".to_owned(), json!(js));