Pulled index rendering out into its own method

This commit is contained in:
Michael Bryan 2017-06-20 07:53:46 +08:00
parent 4af10ce60c
commit 75f0196c55

View file

@ -1,7 +1,7 @@
use renderer::html_handlebars::helpers; use renderer::html_handlebars::helpers;
use renderer::Renderer; use renderer::Renderer;
use book::MDBook; use book::MDBook;
use book::bookitem::BookItem; use book::bookitem::{BookItem, Chapter};
use utils; use utils;
use theme::{self, Theme}; use theme::{self, Theme};
use regex::{Regex, Captures}; use regex::{Regex, Captures};
@ -28,7 +28,7 @@ impl HtmlHandlebars {
} }
fn render_item(&self, item: &BookItem, book: &MDBook, data: &mut serde_json::Map<String, serde_json::Value>, 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, destination: &Path) print_content: &mut String, handlebars: &mut Handlebars, is_index: bool, destination: &Path)
-> Result<(), Box<Error>> { -> Result<(), Box<Error>> {
// FIXME: This should be made DRY-er and rely less on mutable state // FIXME: This should be made DRY-er and rely less on mutable state
match *item { match *item {
@ -75,7 +75,18 @@ impl HtmlHandlebars {
book.write_file(filename, &rendered.into_bytes())?; book.write_file(filename, &rendered.into_bytes())?;
// Create an index.html from the first element in SUMMARY.md // Create an index.html from the first element in SUMMARY.md
if *index { if is_index {
self.render_index(book, ch, destination)?;
}
}
},
_ => {},
}
Ok(())
}
fn render_index(&self, book: &MDBook, ch: &Chapter, destination: &Path) -> Result<(), Box<Error>> {
debug!("[*]: index.html"); debug!("[*]: index.html");
let mut content = String::new(); let mut content = String::new();
@ -99,12 +110,6 @@ impl HtmlHandlebars {
.expect("If the HTML renderer is called, one would assume the HtmlConfig is \ .expect("If the HTML renderer is called, one would assume the HtmlConfig is \
set... (4)") set... (4)")
.join(&ch.path.with_extension("html"))); .join(&ch.path.with_extension("html")));
*index = false;
}
}
},
_ => {},
}
Ok(()) Ok(())
} }
@ -216,9 +221,9 @@ impl Renderer for HtmlHandlebars {
"Unexpected error when constructing destination path"))); "Unexpected error when constructing destination path")));
} }
let mut index = true; for (i, item) in book.iter().enumerate() {
for item in book.iter() { let is_index = i == 0;
self.render_item(item, book, &mut data, &mut print_content, &mut handlebars, &mut index, &destination)?; self.render_item(item, book, &mut data, &mut print_content, &mut handlebars, is_index, &destination)?;
} }
// Print version // Print version