From 6a4b8d51b4c5320e651747d51da44cbb0a49eebc Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Wed, 29 Jul 2015 22:26:13 +0200 Subject: [PATCH] create a copy of the first rendered page and use that as index.html... Closes #16 --- src/renderer/html_handlebars.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/renderer/html_handlebars.rs b/src/renderer/html_handlebars.rs index 2c2cd702..88e66639 100644 --- a/src/renderer/html_handlebars.rs +++ b/src/renderer/html_handlebars.rs @@ -36,6 +36,7 @@ impl Renderer for HtmlHandlebars { let mut data = try!(make_data(book.clone(), config)); // Render a file for every entry in the book + let mut index = true; for (_, item) in book { if item.path != PathBuf::new() { @@ -60,6 +61,15 @@ impl Renderer for HtmlHandlebars { // Write to file let mut file = try!(create_file(config.dest(), &item.path)); try!(file.write_all(&rendered.into_bytes())); + + // Create an index.html from the first element in SUMMARY.md + if index { + try!(fs::copy( + config.dest().join(path_to_link(&item.path).expect("Failed at creation of index.html")), + config.dest().join("index.html") + )); + index = false; + } } } @@ -196,13 +206,13 @@ fn render_html(text: &str) -> String { s } -fn path_to_link(path: &Path) -> Result> { +fn path_to_link(path: &Path) -> Option { // Extract filename let mut file_name; if let Some(name) = path.file_stem() { file_name = String::from(name.to_str().unwrap()); } - else { return Err(Box::new(io::Error::new(io::ErrorKind::Other, "No filename"))) } + else { return None } file_name.push_str(".html"); @@ -212,7 +222,7 @@ fn path_to_link(path: &Path) -> Result> { // Clean paths with './' - Ok(path) + Some(path) } // Handlebars helper to construct TOC @@ -254,12 +264,10 @@ impl HelperDef for RenderToc { if path.len() > 0 { try!(rc.writer.write(" { try!(rc.writer.write(link.to_str().unwrap().as_bytes())); }, - Err(_) => { try!(rc.writer.write("#".as_bytes())); }, + if let Some(link) = path_to_link(Path::new(item.get("path").expect("Error: path should be Some(_)"))) { + try!(rc.writer.write(link.to_str().unwrap().as_bytes())); + } else { + try!(rc.writer.write("#".as_bytes())); } try!(rc.writer.write("\">".as_bytes()));