From 0d0deb7c405fd2dd39a45ee83d3e44028d9a3e82 Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Thu, 15 Jun 2017 17:43:44 +0800 Subject: [PATCH 01/13] Pulled page rendering out into its own method --- src/renderer/html_handlebars/hbs_renderer.rs | 193 ++++++++++--------- 1 file changed, 106 insertions(+), 87 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index b8cdfdbd..658af110 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -25,6 +25,96 @@ impl HtmlHandlebars { pub fn new() -> Self { HtmlHandlebars } + + fn render_item(&self, item: &BookItem, book: &MDBook, data: &mut serde_json::Map, + print_content: &mut String, handlebars: &mut Handlebars, index: &mut bool) + -> Result<(), Box> { + match *item { + BookItem::Chapter(_, ref ch) | + BookItem::Affix(ref ch) => { + if ch.path != PathBuf::new() { + + let path = book.get_source().join(&ch.path); + + debug!("[*]: Opening file: {:?}", path); + let mut f = File::open(&path)?; + let mut content: String = String::new(); + + debug!("[*]: Reading file"); + f.read_to_string(&mut content)?; + + // Parse for playpen links + if let Some(p) = path.parent() { + content = helpers::playpen::render_playpen(&content, p); + } + + // Render markdown using the pulldown-cmark crate + content = utils::render_markdown(&content); + print_content.push_str(&content); + + // Update the context with data for this file + let path = + ch.path + .to_str() + .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))?; + data.insert("path".to_owned(), json!(path)); + data.insert("content".to_owned(), json!(content)); + data.insert("chapter_title".to_owned(), json!(ch.name)); + data.insert("path_to_root".to_owned(), json!(utils::fs::path_to_root(&ch.path))); + + // Render the handlebars template with the data + debug!("[*]: Render template"); + let rendered = handlebars.render("index", &data)?; + + let filename = Path::new(&ch.path).with_extension("html"); + + // Do several kinds of post-processing + let rendered = build_header_links(rendered, filename.to_str().unwrap_or("")); + let rendered = fix_anchor_links(rendered, filename.to_str().unwrap_or("")); + let rendered = fix_code_blocks(rendered); + let rendered = add_playpen_pre(rendered); + + // Write to file + info!("[*] Creating {:?} ✓", filename.display()); + book.write_file(filename, &rendered.into_bytes())?; + + // Create an index.html from the first element in SUMMARY.md + if *index { + debug!("[*]: index.html"); + + let mut content = String::new(); + + let _source = File::open( + book.get_destination() + .expect("If the HTML renderer is called, one would assume the HtmlConfig is set... (3)") + .join(&ch.path.with_extension("html")) + )?.read_to_string(&mut content); + + // This could cause a problem when someone displays + // code containing + // on the front page, however this case should be very very rare... + content = content + .lines() + .filter(|line| !line.contains(" {}, + } + + Ok(()) + } } impl Renderer for HtmlHandlebars { @@ -61,90 +151,7 @@ impl Renderer for HtmlHandlebars { // Render a file for every entry in the book let mut index = true; for item in book.iter() { - - match *item { - BookItem::Chapter(_, ref ch) | - BookItem::Affix(ref ch) => { - if ch.path != PathBuf::new() { - - let path = book.get_source().join(&ch.path); - - debug!("[*]: Opening file: {:?}", path); - let mut f = File::open(&path)?; - let mut content: String = String::new(); - - debug!("[*]: Reading file"); - f.read_to_string(&mut content)?; - - // Parse for playpen links - if let Some(p) = path.parent() { - content = helpers::playpen::render_playpen(&content, p); - } - - // Render markdown using the pulldown-cmark crate - content = utils::render_markdown(&content); - print_content.push_str(&content); - - // Update the context with data for this file - let path = - ch.path - .to_str() - .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))?; - data.insert("path".to_owned(), json!(path)); - data.insert("content".to_owned(), json!(content)); - data.insert("chapter_title".to_owned(), json!(ch.name)); - data.insert("path_to_root".to_owned(), json!(utils::fs::path_to_root(&ch.path))); - - // Render the handlebars template with the data - debug!("[*]: Render template"); - let rendered = handlebars.render("index", &data)?; - - let filename = Path::new(&ch.path).with_extension("html"); - - // Do several kinds of post-processing - let rendered = build_header_links(rendered, filename.to_str().unwrap_or("")); - let rendered = fix_anchor_links(rendered, filename.to_str().unwrap_or("")); - let rendered = fix_code_blocks(rendered); - let rendered = add_playpen_pre(rendered); - - // Write to file - info!("[*] Creating {:?} ✓", filename.display()); - book.write_file(filename, &rendered.into_bytes())?; - - // Create an index.html from the first element in SUMMARY.md - if index { - debug!("[*]: index.html"); - - let mut content = String::new(); - - let _source = File::open( - book.get_destination() - .expect("If the HTML renderer is called, one would assume the HtmlConfig is set... (3)") - .join(&ch.path.with_extension("html")) - )?.read_to_string(&mut content); - - // This could cause a problem when someone displays - // code containing - // on the front page, however this case should be very very rare... - content = content - .lines() - .filter(|line| !line.contains(" {}, - } + 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 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 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)); From f946ef6327ad7f316bd0be73f71bd6bf5cf18144 Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Thu, 15 Jun 2017 18:03:10 +0800 Subject: [PATCH 02/13] Pulled some more little bits out into their own helper functions --- src/renderer/html_handlebars/hbs_renderer.rs | 152 +++++++++++-------- 1 file changed, 87 insertions(+), 65 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 658af110..0b2c7080 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -2,7 +2,8 @@ use renderer::html_handlebars::helpers; use renderer::Renderer; use book::MDBook; use book::bookitem::BookItem; -use {utils, theme}; +use utils; +use theme::{self, Theme}; use regex::{Regex, Captures}; use std::ascii::AsciiExt; @@ -115,69 +116,17 @@ impl HtmlHandlebars { Ok(()) } -} -impl Renderer for HtmlHandlebars { - fn render(&self, book: &MDBook) -> Result<(), Box> { - 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 - - // Update the context with data for this file - data.insert("path".to_owned(), json!("print.md")); - data.insert("content".to_owned(), json!(print_content)); - data.insert("path_to_root".to_owned(), json!(utils::fs::path_to_root(Path::new("print.md")))); - - // Render the handlebars template with the data - debug!("[*]: Render template"); - - let rendered = handlebars.render("index", &data)?; - - // do several kinds of post-processing + fn post_processing(&self, rendered: String) -> String { let rendered = build_header_links(rendered, "print.html"); let rendered = fix_anchor_links(rendered, "print.html"); let rendered = fix_code_blocks(rendered); let rendered = add_playpen_pre(rendered); - book.write_file(Path::new("print").with_extension("html"), &rendered.into_bytes())?; - info!("[*] Creating print.html ✓"); + rendered + } - // Copy static files (js, css, images, ...) - - debug!("[*] Copy static files"); + fn copy_static_files(&self, book: &MDBook, theme: &Theme) -> Result<(), Box> { book.write_file("book.js", &theme.js)?; book.write_file("book.css", &theme.css)?; book.write_file("favicon.png", &theme.favicon)?; @@ -196,9 +145,11 @@ impl Renderer for HtmlHandlebars { book.write_file("_FontAwesome/fonts/fontawesome-webfont.woff2", theme::FONT_AWESOME_WOFF2)?; book.write_file("_FontAwesome/fonts/FontAwesome.ttf", theme::FONT_AWESOME_TTF)?; - for custom_file in book.get_additional_css() - .iter() - .chain(book.get_additional_js().iter()) { + Ok(()) + } + + + fn write_custom_file(&self, custom_file: &Path, book: &MDBook) -> Result<(), Box> { let mut data = Vec::new(); let mut f = File::open(custom_file)?; f.read_to_end(&mut data)?; @@ -215,14 +166,85 @@ impl Renderer for HtmlHandlebars { }; book.write_file(name, &data)?; + + Ok(()) + } + + /// Update the context with data for this file + fn configure_print_version(&self, data: &mut serde_json::Map, print_content: &str) { + data.insert("path".to_owned(), json!("print.md")); + data.insert("content".to_owned(), json!(print_content)); + data.insert("path_to_root".to_owned(), json!(utils::fs::path_to_root(Path::new("print.md")))); + } + + fn register_hbs_helpers(&self, handlebars: &mut Handlebars) { + 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)); + } +} + + +impl Renderer for HtmlHandlebars { + fn render(&self, book: &MDBook) -> Result<(), Box> { + 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.clone())?)?; + + // Register helpers + debug!("[*]: Register handlebars helpers"); + self.register_hbs_helpers(&mut handlebars); + + let mut data = make_data(book)?; + + // Print version + let mut print_content: String = String::new(); + + debug!("[*]: Check if destination directory exists"); + let destination = book.get_destination() + .expect("If the HTML renderer is called, one would assume the HtmlConfig is set... (2)"); + + if fs::create_dir_all(&destination).is_err() { + return Err(Box::new(io::Error::new(io::ErrorKind::Other, + "Unexpected error when constructing destination path"))); + } + + 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 + self.configure_print_version(&mut data, &print_content); + + // Render the handlebars template with the data + debug!("[*]: Render template"); + + let rendered = handlebars.render("index", &data)?; + let rendered = self.post_processing(rendered); + + book.write_file(Path::new("print").with_extension("html"), &rendered.into_bytes())?; + info!("[*] Creating print.html ✓"); + + // Copy static files (js, css, images, ...) + debug!("[*] Copy static files"); + self.copy_static_files(book, &theme)?; + + for custom_file in book.get_additional_css() + .iter() + .chain(book.get_additional_js().iter()) { + self.write_custom_file(custom_file, book)?; } // Copy all remaining files - utils::fs::copy_files_except_ext( - book.get_source(), - book.get_destination() - .expect("If the HTML renderer is called, one would assume the HtmlConfig is set... (5)"), true, &["md"] - )?; + utils::fs::copy_files_except_ext(book.get_source(), &destination, true, &["md"])?; Ok(()) } From 2568986fd58c382acbea3b48583a4ccb7bfb529a Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Thu, 15 Jun 2017 18:17:16 +0800 Subject: [PATCH 03/13] fixed a typo --- src/renderer/html_handlebars/hbs_renderer.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 0b2c7080..6c8928be 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -182,6 +182,16 @@ impl HtmlHandlebars { handlebars.register_helper("previous", Box::new(helpers::navigation::previous)); handlebars.register_helper("next", Box::new(helpers::navigation::next)); } + + fn copy_additional_css(&self, book: &MDBook) -> Result<(), Box> { + for custom_file in book.get_additional_css() + .iter() + .chain(book.get_additional_js().iter()) { + self.write_custom_file(custom_file, book)?; + } + + Ok(()) + } } @@ -198,7 +208,6 @@ impl Renderer for HtmlHandlebars { handlebars .register_template_string("index", String::from_utf8(theme.index.clone())?)?; - // Register helpers debug!("[*]: Register handlebars helpers"); self.register_hbs_helpers(&mut handlebars); @@ -236,12 +245,7 @@ impl Renderer for HtmlHandlebars { // Copy static files (js, css, images, ...) debug!("[*] Copy static files"); self.copy_static_files(book, &theme)?; - - for custom_file in book.get_additional_css() - .iter() - .chain(book.get_additional_js().iter()) { - self.write_custom_file(custom_file, book)?; - } + self.copy_additional_css(book)?; // Copy all remaining files utils::fs::copy_files_except_ext(book.get_source(), &destination, true, &["md"])?; From b7aa78c3c0c739b8166b6e21927ae522ade80f7b Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Thu, 15 Jun 2017 18:39:41 +0800 Subject: [PATCH 04/13] Minor refactoring --- src/renderer/html_handlebars/hbs_renderer.rs | 22 ++++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 6c8928be..db2ae69e 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -28,7 +28,7 @@ impl HtmlHandlebars { } fn render_item(&self, item: &BookItem, book: &MDBook, data: &mut serde_json::Map, - print_content: &mut String, handlebars: &mut Handlebars, index: &mut bool) + print_content: &mut String, handlebars: &mut Handlebars, index: &mut bool, destination: &Path) -> Result<(), Box> { match *item { BookItem::Chapter(_, ref ch) | @@ -66,15 +66,10 @@ impl HtmlHandlebars { // Render the handlebars template with the data debug!("[*]: Render template"); let rendered = handlebars.render("index", &data)?; + let rendered = self.post_processing(rendered); let filename = Path::new(&ch.path).with_extension("html"); - // Do several kinds of post-processing - let rendered = build_header_links(rendered, filename.to_str().unwrap_or("")); - let rendered = fix_anchor_links(rendered, filename.to_str().unwrap_or("")); - let rendered = fix_code_blocks(rendered); - let rendered = add_playpen_pre(rendered); - // Write to file info!("[*] Creating {:?} ✓", filename.display()); book.write_file(filename, &rendered.into_bytes())?; @@ -86,9 +81,7 @@ impl HtmlHandlebars { let mut content = String::new(); let _source = File::open( - book.get_destination() - .expect("If the HTML renderer is called, one would assume the HtmlConfig is set... (3)") - .join(&ch.path.with_extension("html")) + destination.join(&ch.path.with_extension("html")) )?.read_to_string(&mut content); // This could cause a problem when someone displays @@ -148,7 +141,6 @@ impl HtmlHandlebars { Ok(()) } - fn write_custom_file(&self, custom_file: &Path, book: &MDBook) -> Result<(), Box> { let mut data = Vec::new(); let mut f = File::open(custom_file)?; @@ -200,10 +192,8 @@ impl Renderer for HtmlHandlebars { 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.clone())?)?; @@ -214,12 +204,12 @@ impl Renderer for HtmlHandlebars { let mut data = make_data(book)?; // Print version - let mut print_content: String = String::new(); + let mut print_content = String::new(); - debug!("[*]: Check if destination directory exists"); let destination = book.get_destination() .expect("If the HTML renderer is called, one would assume the HtmlConfig is set... (2)"); + debug!("[*]: Check if destination directory exists"); if fs::create_dir_all(&destination).is_err() { return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Unexpected error when constructing destination path"))); @@ -227,7 +217,7 @@ impl Renderer for HtmlHandlebars { let mut index = true; for item in book.iter() { - self.render_item(item, book, &mut data, &mut print_content, &mut handlebars, &mut index)?; + self.render_item(item, book, &mut data, &mut print_content, &mut handlebars, &mut index, &destination)?; } // Print version From deab3ba7514b1525441394a7fbd7af863381336a Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Fri, 16 Jun 2017 06:50:13 +0800 Subject: [PATCH 05/13] Tiny whitespace changes --- src/renderer/html_handlebars/hbs_renderer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index db2ae69e..3351c7ab 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -49,7 +49,6 @@ impl HtmlHandlebars { content = helpers::playpen::render_playpen(&content, p); } - // Render markdown using the pulldown-cmark crate content = utils::render_markdown(&content); print_content.push_str(&content); @@ -58,6 +57,7 @@ impl HtmlHandlebars { ch.path .to_str() .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))?; + data.insert("path".to_owned(), json!(path)); data.insert("content".to_owned(), json!(content)); data.insert("chapter_title".to_owned(), json!(ch.name)); From 4af10ce60cfae9114faa523bdbe0bcbd472d0f2a Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Sat, 17 Jun 2017 21:15:54 +0800 Subject: [PATCH 06/13] Renamed a couple functions to be more descriptive and ran rustfmt --- src/renderer/html_handlebars/hbs_renderer.rs | 131 +++++++++---------- 1 file changed, 63 insertions(+), 68 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 3351c7ab..95acbd77 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -30,6 +30,7 @@ impl HtmlHandlebars { fn render_item(&self, item: &BookItem, book: &MDBook, data: &mut serde_json::Map, print_content: &mut String, handlebars: &mut Handlebars, index: &mut bool, destination: &Path) -> Result<(), Box> { + // FIXME: This should be made DRY-er and rely less on mutable state match *item { BookItem::Chapter(_, ref ch) | BookItem::Affix(ref ch) => { @@ -53,10 +54,9 @@ impl HtmlHandlebars { print_content.push_str(&content); // Update the context with data for this file - let path = - ch.path - .to_str() - .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))?; + let path = ch.path + .to_str() + .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))?; data.insert("path".to_owned(), json!(path)); data.insert("content".to_owned(), json!(content)); @@ -66,7 +66,7 @@ impl HtmlHandlebars { // Render the handlebars template with the data debug!("[*]: Render template"); let rendered = handlebars.render("index", &data)?; - let rendered = self.post_processing(rendered); + let rendered = self.post_process(rendered); let filename = Path::new(&ch.path).with_extension("html"); @@ -80,15 +80,14 @@ impl HtmlHandlebars { let mut content = String::new(); - let _source = File::open( - destination.join(&ch.path.with_extension("html")) - )?.read_to_string(&mut content); + let _source = File::open(destination.join(&ch.path.with_extension("html"))) + ? + .read_to_string(&mut content); // This could cause a problem when someone displays // code containing // on the front page, however this case should be very very rare... - content = content - .lines() + content = content.lines() .filter(|line| !line.contains(" String { + fn post_process(&self, rendered: String) -> String { let rendered = build_header_links(rendered, "print.html"); let rendered = fix_anchor_links(rendered, "print.html"); let rendered = fix_code_blocks(rendered); @@ -142,24 +141,23 @@ impl HtmlHandlebars { } fn write_custom_file(&self, custom_file: &Path, book: &MDBook) -> Result<(), Box> { - let mut data = Vec::new(); - let mut f = File::open(custom_file)?; - f.read_to_end(&mut data)?; + let mut data = Vec::new(); + let mut f = File::open(custom_file)?; + f.read_to_end(&mut data)?; - let name = match custom_file.strip_prefix(book.get_root()) { - Ok(p) => p.to_str().expect("Could not convert to str"), - Err(_) => { - custom_file - .file_name() - .expect("File has a file name") - .to_str() - .expect("Could not convert to str") - }, - }; + let name = match custom_file.strip_prefix(book.get_root()) { + Ok(p) => p.to_str().expect("Could not convert to str"), + Err(_) => { + custom_file.file_name() + .expect("File has a file name") + .to_str() + .expect("Could not convert to str") + }, + }; - book.write_file(name, &data)?; + book.write_file(name, &data)?; - Ok(()) + Ok(()) } /// Update the context with data for this file @@ -175,11 +173,15 @@ impl HtmlHandlebars { handlebars.register_helper("next", Box::new(helpers::navigation::next)); } - fn copy_additional_css(&self, book: &MDBook) -> Result<(), Box> { - for custom_file in book.get_additional_css() - .iter() - .chain(book.get_additional_js().iter()) { - self.write_custom_file(custom_file, book)?; + /// Copy across any additional CSS and JavaScript files which the book + /// has been configured to use. + fn copy_additional_css_and_js(&self, book: &MDBook) -> Result<(), Box> { + let custom_files = book.get_additional_css() + .iter() + .chain(book.get_additional_js().iter()); + + for custom_file in custom_files { + self.write_custom_file(custom_file, book)?; } Ok(()) @@ -195,8 +197,7 @@ impl Renderer for HtmlHandlebars { let theme = theme::Theme::new(book.get_theme_path()); debug!("[*]: Register handlebars template"); - handlebars - .register_template_string("index", String::from_utf8(theme.index.clone())?)?; + handlebars.register_template_string("index", String::from_utf8(theme.index.clone())?)?; debug!("[*]: Register handlebars helpers"); self.register_hbs_helpers(&mut handlebars); @@ -207,7 +208,7 @@ impl Renderer for HtmlHandlebars { let mut print_content = String::new(); let destination = book.get_destination() - .expect("If the HTML renderer is called, one would assume the HtmlConfig is set... (2)"); + .expect("If the HTML renderer is called, one would assume the HtmlConfig is set... (2)"); debug!("[*]: Check if destination directory exists"); if fs::create_dir_all(&destination).is_err() { @@ -227,7 +228,7 @@ impl Renderer for HtmlHandlebars { debug!("[*]: Render template"); let rendered = handlebars.render("index", &data)?; - let rendered = self.post_processing(rendered); + let rendered = self.post_process(rendered); book.write_file(Path::new("print").with_extension("html"), &rendered.into_bytes())?; info!("[*] Creating print.html ✓"); @@ -235,7 +236,7 @@ impl Renderer for HtmlHandlebars { // Copy static files (js, css, images, ...) debug!("[*] Copy static files"); self.copy_static_files(book, &theme)?; - self.copy_additional_css(book)?; + self.copy_additional_css_and_js(book)?; // Copy all remaining files utils::fs::copy_files_except_ext(book.get_source(), &destination, true, &["md"])?; @@ -268,11 +269,10 @@ fn make_data(book: &MDBook) -> Result 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")) + css.push(style.file_name() + .expect("File has a file name") + .to_str() + .expect("Could not convert to str")) }, } } @@ -286,11 +286,10 @@ fn make_data(book: &MDBook) -> Result 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")) + js.push(script.file_name() + .expect("File has a file name") + .to_str() + .expect("Could not convert to str")) }, } } @@ -338,8 +337,7 @@ fn build_header_links(html: String, filename: &str) -> String { let regex = Regex::new(r"(.*?)").unwrap(); let mut id_counter = HashMap::new(); - regex - .replace_all(&html, |caps: &Captures| { + regex.replace_all(&html, |caps: &Captures| { let level = &caps[1]; let text = &caps[2]; let mut id = text.to_string(); @@ -359,16 +357,16 @@ fn build_header_links(html: String, filename: &str) -> String { } let id = id.chars() .filter_map(|c| if c.is_alphanumeric() || c == '-' || c == '_' { - if c.is_ascii() { - Some(c.to_ascii_lowercase()) - } else { - Some(c) - } - } else if c.is_whitespace() && c.is_ascii() { - Some('-') - } else { - None - }) + if c.is_ascii() { + Some(c.to_ascii_lowercase()) + } else { + Some(c) + } + } else if c.is_whitespace() && c.is_ascii() { + Some('-') + } else { + None + }) .collect::(); let id_count = *id_counter.get(&id).unwrap_or(&0); @@ -394,8 +392,7 @@ fn build_header_links(html: String, filename: &str) -> String { // that in a very inelegant way fn fix_anchor_links(html: String, filename: &str) -> String { let regex = Regex::new(r##"]+)href="#([^"]+)"([^>]*)>"##).unwrap(); - regex - .replace_all(&html, |caps: &Captures| { + regex.replace_all(&html, |caps: &Captures| { let before = &caps[1]; let anchor = &caps[2]; let after = &caps[3]; @@ -420,8 +417,7 @@ fn fix_anchor_links(html: String, filename: &str) -> String { // This function replaces all commas by spaces in the code block classes fn fix_code_blocks(html: String) -> String { let regex = Regex::new(r##"]+)class="([^"]+)"([^>]*)>"##).unwrap(); - regex - .replace_all(&html, |caps: &Captures| { + regex.replace_all(&html, |caps: &Captures| { let before = &caps[1]; let classes = &caps[2].replace(",", " "); let after = &caps[3]; @@ -433,8 +429,7 @@ fn fix_code_blocks(html: String) -> String { fn add_playpen_pre(html: String) -> String { let regex = Regex::new(r##"((?s)]?class="([^"]+)".*?>(.*?))"##).unwrap(); - regex - .replace_all(&html, |caps: &Captures| { + regex.replace_all(&html, |caps: &Captures| { let text = &caps[1]; let classes = &caps[2]; let code = &caps[3]; From 75f0196c557dc1b1032741a73b9f550633482ec3 Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Tue, 20 Jun 2017 07:53:46 +0800 Subject: [PATCH 07/13] Pulled index rendering out into its own method --- src/renderer/html_handlebars/hbs_renderer.rs | 65 +++++++++++--------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 95acbd77..bf349ece 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -1,7 +1,7 @@ use renderer::html_handlebars::helpers; use renderer::Renderer; use book::MDBook; -use book::bookitem::BookItem; +use book::bookitem::{BookItem, Chapter}; use utils; use theme::{self, Theme}; use regex::{Regex, Captures}; @@ -28,7 +28,7 @@ impl HtmlHandlebars { } fn render_item(&self, item: &BookItem, book: &MDBook, data: &mut serde_json::Map, - 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> { // FIXME: This should be made DRY-er and rely less on mutable state match *item { @@ -75,31 +75,8 @@ impl HtmlHandlebars { book.write_file(filename, &rendered.into_bytes())?; // Create an index.html from the first element in SUMMARY.md - if *index { - debug!("[*]: index.html"); - - let mut content = String::new(); - - let _source = File::open(destination.join(&ch.path.with_extension("html"))) - ? - .read_to_string(&mut content); - - // This could cause a problem when someone displays - // code containing - // on the front page, however this case should be very very rare... - content = content.lines() - .filter(|line| !line.contains(" Result<(), Box> { + debug!("[*]: index.html"); + + let mut content = String::new(); + + let _source = File::open(destination.join(&ch.path.with_extension("html"))) + ? + .read_to_string(&mut content); + + // This could cause a problem when someone displays + // code containing + // on the front page, however this case should be very very rare... + content = content.lines() + .filter(|line| !line.contains(" String { let rendered = build_header_links(rendered, "print.html"); let rendered = fix_anchor_links(rendered, "print.html"); @@ -216,9 +221,9 @@ impl Renderer for HtmlHandlebars { "Unexpected error when constructing destination path"))); } - let mut index = true; - for item in book.iter() { - self.render_item(item, book, &mut data, &mut print_content, &mut handlebars, &mut index, &destination)?; + for (i, item) in book.iter().enumerate() { + let is_index = i == 0; + self.render_item(item, book, &mut data, &mut print_content, &mut handlebars, is_index, &destination)?; } // Print version From e2a7adaa79af22f55380db8f2fa1eccecdf880d3 Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Tue, 20 Jun 2017 08:40:01 +0800 Subject: [PATCH 08/13] Introduced a RenderItemContext to make item rendering easier I also accidentally ran `rustfmt` instead of `rustfmt-nightly`, so there are a lot of unnecessary style changes :( --- src/renderer/html_handlebars/hbs_renderer.rs | 277 ++++++++++++------- 1 file changed, 177 insertions(+), 100 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index bf349ece..5948e9ab 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -27,16 +27,15 @@ impl HtmlHandlebars { HtmlHandlebars } - fn render_item(&self, item: &BookItem, book: &MDBook, data: &mut serde_json::Map, - print_content: &mut String, handlebars: &mut Handlebars, is_index: bool, destination: &Path) - -> Result<(), Box> { + fn render_item(&self, item: &BookItem, mut ctx: RenderItemContext, print_content: &mut String) + -> Result<(), Box> { // FIXME: This should be made DRY-er and rely less on mutable state match *item { BookItem::Chapter(_, ref ch) | BookItem::Affix(ref ch) => { if ch.path != PathBuf::new() { - let path = book.get_source().join(&ch.path); + let path = ctx.book.get_source().join(&ch.path); debug!("[*]: Opening file: {:?}", path); let mut f = File::open(&path)?; @@ -54,29 +53,31 @@ impl HtmlHandlebars { print_content.push_str(&content); // Update the context with data for this file - let path = ch.path - .to_str() - .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))?; + let path = ch.path.to_str().ok_or_else(|| { + io::Error::new(io::ErrorKind::Other, "Could not convert path to str") + })?; - data.insert("path".to_owned(), json!(path)); - data.insert("content".to_owned(), json!(content)); - data.insert("chapter_title".to_owned(), json!(ch.name)); - data.insert("path_to_root".to_owned(), json!(utils::fs::path_to_root(&ch.path))); + ctx.data.insert("path".to_owned(), json!(path)); + ctx.data.insert("content".to_owned(), json!(content)); + ctx.data.insert("chapter_title".to_owned(), json!(ch.name)); + ctx.data.insert( + "path_to_root".to_owned(), + json!(utils::fs::path_to_root(&ch.path)), + ); // Render the handlebars template with the data debug!("[*]: Render template"); - let rendered = handlebars.render("index", &data)?; + let rendered = ctx.handlebars.render("index", &ctx.data)?; let rendered = self.post_process(rendered); let filename = Path::new(&ch.path).with_extension("html"); // Write to file info!("[*] Creating {:?} ✓", filename.display()); - book.write_file(filename, &rendered.into_bytes())?; + ctx.book.write_file(filename, &rendered.into_bytes())?; - // Create an index.html from the first element in SUMMARY.md - if is_index { - self.render_index(book, ch, destination)?; + if ctx.is_index { + self.render_index(&ctx.book, ch, &ctx.destination)?; } } }, @@ -86,32 +87,37 @@ impl HtmlHandlebars { Ok(()) } + /// Create an index.html from the first element in SUMMARY.md fn render_index(&self, book: &MDBook, ch: &Chapter, destination: &Path) -> Result<(), Box> { - debug!("[*]: index.html"); + debug!("[*]: index.html"); - let mut content = String::new(); + let mut content = String::new(); - let _source = File::open(destination.join(&ch.path.with_extension("html"))) - ? - .read_to_string(&mut content); + File::open(destination.join(&ch.path.with_extension("html")))? + .read_to_string(&mut content)?; - // This could cause a problem when someone displays - // code containing - // on the front page, however this case should be very very rare... - content = content.lines() - .filter(|line| !line.contains(" + // on the front page, however this case should be very very rare... + content = content + .lines() + .filter(|line| !line.contains(" String { @@ -129,18 +135,45 @@ impl HtmlHandlebars { book.write_file("favicon.png", &theme.favicon)?; book.write_file("jquery.js", &theme.jquery)?; book.write_file("highlight.css", &theme.highlight_css)?; - book.write_file("tomorrow-night.css", &theme.tomorrow_night_css)?; - book.write_file("ayu-highlight.css", &theme.ayu_highlight_css)?; + book.write_file( + "tomorrow-night.css", + &theme.tomorrow_night_css, + )?; + book.write_file( + "ayu-highlight.css", + &theme.ayu_highlight_css, + )?; book.write_file("highlight.js", &theme.highlight_js)?; book.write_file("clipboard.min.js", &theme.clipboard_js)?; book.write_file("store.js", &theme.store_js)?; - book.write_file("_FontAwesome/css/font-awesome.css", theme::FONT_AWESOME)?; - book.write_file("_FontAwesome/fonts/fontawesome-webfont.eot", theme::FONT_AWESOME_EOT)?; - book.write_file("_FontAwesome/fonts/fontawesome-webfont.svg", theme::FONT_AWESOME_SVG)?; - book.write_file("_FontAwesome/fonts/fontawesome-webfont.ttf", theme::FONT_AWESOME_TTF)?; - book.write_file("_FontAwesome/fonts/fontawesome-webfont.woff", theme::FONT_AWESOME_WOFF)?; - book.write_file("_FontAwesome/fonts/fontawesome-webfont.woff2", theme::FONT_AWESOME_WOFF2)?; - book.write_file("_FontAwesome/fonts/FontAwesome.ttf", theme::FONT_AWESOME_TTF)?; + book.write_file( + "_FontAwesome/css/font-awesome.css", + theme::FONT_AWESOME, + )?; + book.write_file( + "_FontAwesome/fonts/fontawesome-webfont.eot", + theme::FONT_AWESOME_EOT, + )?; + book.write_file( + "_FontAwesome/fonts/fontawesome-webfont.svg", + theme::FONT_AWESOME_SVG, + )?; + book.write_file( + "_FontAwesome/fonts/fontawesome-webfont.ttf", + theme::FONT_AWESOME_TTF, + )?; + book.write_file( + "_FontAwesome/fonts/fontawesome-webfont.woff", + theme::FONT_AWESOME_WOFF, + )?; + book.write_file( + "_FontAwesome/fonts/fontawesome-webfont.woff2", + theme::FONT_AWESOME_WOFF2, + )?; + book.write_file( + "_FontAwesome/fonts/FontAwesome.ttf", + theme::FONT_AWESOME_TTF, + )?; Ok(()) } @@ -153,7 +186,8 @@ impl HtmlHandlebars { let name = match custom_file.strip_prefix(book.get_root()) { Ok(p) => p.to_str().expect("Could not convert to str"), Err(_) => { - custom_file.file_name() + custom_file + .file_name() .expect("File has a file name") .to_str() .expect("Could not convert to str") @@ -181,9 +215,10 @@ impl HtmlHandlebars { /// Copy across any additional CSS and JavaScript files which the book /// has been configured to use. fn copy_additional_css_and_js(&self, book: &MDBook) -> Result<(), Box> { - let custom_files = book.get_additional_css() - .iter() - .chain(book.get_additional_js().iter()); + let custom_files = book.get_additional_css().iter().chain( + book.get_additional_js() + .iter(), + ); for custom_file in custom_files { self.write_custom_file(custom_file, book)?; @@ -202,7 +237,10 @@ impl Renderer for HtmlHandlebars { let theme = theme::Theme::new(book.get_theme_path()); debug!("[*]: Register handlebars template"); - handlebars.register_template_string("index", String::from_utf8(theme.index.clone())?)?; + handlebars.register_template_string( + "index", + String::from_utf8(theme.index.clone())?, + )?; debug!("[*]: Register handlebars helpers"); self.register_hbs_helpers(&mut handlebars); @@ -212,18 +250,26 @@ impl Renderer for HtmlHandlebars { // Print version let mut print_content = String::new(); - let destination = book.get_destination() - .expect("If the HTML renderer is called, one would assume the HtmlConfig is set... (2)"); + let destination = book.get_destination().expect( + "If the HTML renderer is called, one would assume the HtmlConfig is set... (2)", + ); debug!("[*]: Check if destination directory exists"); if fs::create_dir_all(&destination).is_err() { - return Err(Box::new(io::Error::new(io::ErrorKind::Other, - "Unexpected error when constructing destination path"))); + return Err(Box::new( + io::Error::new(io::ErrorKind::Other, "Unexpected error when constructing destination path"), + )); } for (i, item) in book.iter().enumerate() { - let is_index = i == 0; - self.render_item(item, book, &mut data, &mut print_content, &mut handlebars, is_index, &destination)?; + let ctx = RenderItemContext { + book: book, + handlebars: &handlebars, + destination: destination.to_path_buf(), + data: data.clone(), + is_index: i == 0, + }; + self.render_item(item, ctx, &mut print_content)?; } // Print version @@ -235,7 +281,10 @@ impl Renderer for HtmlHandlebars { let rendered = handlebars.render("index", &data)?; let rendered = self.post_process(rendered); - book.write_file(Path::new("print").with_extension("html"), &rendered.into_bytes())?; + book.write_file( + Path::new("print").with_extension("html"), + &rendered.into_bytes(), + )?; info!("[*] Creating print.html ✓"); // Copy static files (js, css, images, ...) @@ -274,10 +323,13 @@ fn make_data(book: &MDBook) -> Result 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")) + css.push( + style + .file_name() + .expect("File has a file name") + .to_str() + .expect("Could not convert to str"), + ) }, } } @@ -291,10 +343,13 @@ fn make_data(book: &MDBook) -> Result 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")) + js.push( + script + .file_name() + .expect("File has a file name") + .to_str() + .expect("Could not convert to str"), + ) }, } } @@ -310,17 +365,17 @@ fn make_data(book: &MDBook) -> Result match *item { BookItem::Affix(ref ch) => { chapter.insert("name".to_owned(), json!(ch.name)); - let path = ch.path - .to_str() - .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))?; + let path = ch.path.to_str().ok_or_else(|| { + io::Error::new(io::ErrorKind::Other, "Could not convert path to str") + })?; chapter.insert("path".to_owned(), json!(path)); }, BookItem::Chapter(ref s, ref ch) => { chapter.insert("section".to_owned(), json!(s)); chapter.insert("name".to_owned(), json!(ch.name)); - let path = ch.path - .to_str() - .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))?; + let path = ch.path.to_str().ok_or_else(|| { + io::Error::new(io::ErrorKind::Other, "Could not convert path to str") + })?; chapter.insert("path".to_owned(), json!(path)); }, BookItem::Spacer => { @@ -342,26 +397,30 @@ fn build_header_links(html: String, filename: &str) -> String { let regex = Regex::new(r"(.*?)").unwrap(); let mut id_counter = HashMap::new(); - regex.replace_all(&html, |caps: &Captures| { + regex + .replace_all(&html, |caps: &Captures| { let level = &caps[1]; let text = &caps[2]; let mut id = text.to_string(); - let repl_sub = vec!["", - "", - "", - "", - "", - "", - "<", - ">", - "&", - "'", - """]; + let repl_sub = vec![ + "", + "", + "", + "", + "", + "", + "<", + ">", + "&", + "'", + """, + ]; for sub in repl_sub { id = id.replace(sub, ""); } let id = id.chars() - .filter_map(|c| if c.is_alphanumeric() || c == '-' || c == '_' { + .filter(|c| if c.is_alphanumeric() || c == '-' || c == '_') + .map(|c| { if c.is_ascii() { Some(c.to_ascii_lowercase()) } else { @@ -383,11 +442,13 @@ fn build_header_links(html: String, filename: &str) -> String { id }; - format!("{text}", - level = level, - id = id, - text = text, - filename = filename) + format!( + "{text}", + level = level, + id = id, + text = text, + filename = filename + ) }) .into_owned() } @@ -397,16 +458,19 @@ fn build_header_links(html: String, filename: &str) -> String { // that in a very inelegant way fn fix_anchor_links(html: String, filename: &str) -> String { let regex = Regex::new(r##"]+)href="#([^"]+)"([^>]*)>"##).unwrap(); - regex.replace_all(&html, |caps: &Captures| { + regex + .replace_all(&html, |caps: &Captures| { let before = &caps[1]; let anchor = &caps[2]; let after = &caps[3]; - format!("", - before = before, - filename = filename, - anchor = anchor, - after = after) + format!( + "", + before = before, + filename = filename, + anchor = anchor, + after = after + ) }) .into_owned() } @@ -422,7 +486,8 @@ fn fix_anchor_links(html: String, filename: &str) -> String { // This function replaces all commas by spaces in the code block classes fn fix_code_blocks(html: String) -> String { let regex = Regex::new(r##"]+)class="([^"]+)"([^>]*)>"##).unwrap(); - regex.replace_all(&html, |caps: &Captures| { + regex + .replace_all(&html, |caps: &Captures| { let before = &caps[1]; let classes = &caps[2].replace(",", " "); let after = &caps[3]; @@ -434,7 +499,8 @@ fn fix_code_blocks(html: String) -> String { fn add_playpen_pre(html: String) -> String { let regex = Regex::new(r##"((?s)]?class="([^"]+)".*?>(.*?))"##).unwrap(); - regex.replace_all(&html, |caps: &Captures| { + regex + .replace_all(&html, |caps: &Captures| { let text = &caps[1]; let classes = &caps[2]; let code = &caps[3]; @@ -447,14 +513,16 @@ fn add_playpen_pre(html: String) -> String { } else { // we need to inject our own main let (attrs, code) = partition_source(code); - format!("
# #![allow(unused_variables)]
+                    format!(
+                        "
# #![allow(unused_variables)]
 {}#fn main() {{
 \
                              {}
 #}}
", - classes, - attrs, - code) + classes, + attrs, + code + ) } } else { // not language-rust, so no-op @@ -484,3 +552,12 @@ fn partition_source(s: &str) -> (String, String) { (before, after) } + + +struct RenderItemContext<'a> { + handlebars: &'a Handlebars, + book: &'a MDBook, + destination: PathBuf, + data: serde_json::Map, + is_index: bool, +} From ac16d7aef1ed3c8d184c19fa7e5c48aba121de8e Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Tue, 20 Jun 2017 10:54:32 +0800 Subject: [PATCH 09/13] Added some tests for the original build_header_links function --- src/renderer/html_handlebars/hbs_renderer.rs | 27 ++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 5948e9ab..fe78c920 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -419,8 +419,7 @@ fn build_header_links(html: String, filename: &str) -> String { id = id.replace(sub, ""); } let id = id.chars() - .filter(|c| if c.is_alphanumeric() || c == '-' || c == '_') - .map(|c| { + .filter_map(|c| if c.is_alphanumeric() || c == '-' || c == '_' { if c.is_ascii() { Some(c.to_ascii_lowercase()) } else { @@ -561,3 +560,27 @@ struct RenderItemContext<'a> { data: serde_json::Map, is_index: bool, } + + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn original_build_header_links() { + let inputs = vec![ + ("blah blah

Foo

", r#"blah blah

Foo

"#), + ("

Foo

", r#"

Foo

"#), + ("

Foo^bar

", r#"

Foo^bar

"#), + ("

", r#"

"#), + ("

", r#"

"#), + ("

Foo

Foo

", r#"

Foo

Foo

"#), + ]; + + for (src, should_be) in inputs { + let got = build_header_links(src.to_string(), "bar.rs"); + assert_eq!(got, should_be); + } + } +} \ No newline at end of file From fa9554698845b56663c43126fdb7606534dee613 Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Tue, 20 Jun 2017 11:06:30 +0800 Subject: [PATCH 10/13] Broke the header link wrapping out into smaller functions --- src/renderer/html_handlebars/hbs_renderer.rs | 114 +++++++++++-------- 1 file changed, 64 insertions(+), 50 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index fe78c920..c5050e86 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -87,7 +87,7 @@ impl HtmlHandlebars { Ok(()) } - /// Create an index.html from the first element in SUMMARY.md + /// Create an index.html from the first element in SUMMARY.md fn render_index(&self, book: &MDBook, ch: &Chapter, destination: &Path) -> Result<(), Box> { debug!("[*]: index.html"); @@ -399,59 +399,73 @@ fn build_header_links(html: String, filename: &str) -> String { regex .replace_all(&html, |caps: &Captures| { - let level = &caps[1]; - let text = &caps[2]; - let mut id = text.to_string(); - let repl_sub = vec![ - "", - "", - "", - "", - "", - "", - "<", - ">", - "&", - "'", - """, - ]; - for sub in repl_sub { - id = id.replace(sub, ""); - } - let id = id.chars() - .filter_map(|c| if c.is_alphanumeric() || c == '-' || c == '_' { - if c.is_ascii() { - Some(c.to_ascii_lowercase()) - } else { - Some(c) - } - } else if c.is_whitespace() && c.is_ascii() { - Some('-') - } else { - None - }) - .collect::(); + let level = caps[1].parse().expect( + "Regex should ensure we only ever get numbers here", + ); - let id_count = *id_counter.get(&id).unwrap_or(&0); - id_counter.insert(id.clone(), id_count + 1); - - let id = if id_count > 0 { - format!("{}-{}", id, id_count) - } else { - id - }; - - format!( - "{text}", - level = level, - id = id, - text = text, - filename = filename - ) + wrap_header_with_link(level, &caps[2], &mut id_counter, filename) }) .into_owned() } +fn wrap_header_with_link(level: usize, content: &str, id_counter: &mut HashMap, filename: &str) + -> String { + let id = id_from_content(content); + + let id_count = *id_counter.get(&id).unwrap_or(&0); + id_counter.insert(id.clone(), id_count + 1); + + let id = if id_count > 0 { + format!("{}-{}", id, id_count) + } else { + id + }; + + format!( + r#"{text}"#, + level = level, + id = id, + text = content, + filename = filename + ) +} + +fn id_from_content(content: &str) -> String { + let mut content = content.to_string(); + + // Skip any tags or html-encoded stuff + let repl_sub = vec![ + "", + "", + "", + "", + "", + "", + "<", + ">", + "&", + "'", + """, + ]; + for sub in repl_sub { + content = content.replace(sub, ""); + } + + content.chars() + .filter_map(|c| if c.is_alphanumeric() || c == '-' || c == '_' { + if c.is_ascii() { + Some(c.to_ascii_lowercase()) + } else { + Some(c) + } + } else if c.is_whitespace() && c.is_ascii() { + Some('-') + } else { + None + }) + .collect() +} + // anchors to the same page (href="#anchor") do not work because of // pointing to the root folder. This function *fixes* // that in a very inelegant way @@ -583,4 +597,4 @@ mod tests { assert_eq!(got, should_be); } } -} \ No newline at end of file +} From 8c30de16d6da6b9911e7675033e4e3d659b5863e Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Tue, 20 Jun 2017 11:15:12 +0800 Subject: [PATCH 11/13] Used the Entry API to make id counter incrementing nicer --- src/renderer/html_handlebars/hbs_renderer.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index c5050e86..b6be764f 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -393,6 +393,8 @@ fn make_data(book: &MDBook) -> Result Ok(data) } +/// Goes through the rendered HTML, making sure all header tags are wrapped in +/// an anchor so people can link to sections directly. fn build_header_links(html: String, filename: &str) -> String { let regex = Regex::new(r"(.*?)").unwrap(); let mut id_counter = HashMap::new(); @@ -408,19 +410,21 @@ fn build_header_links(html: String, filename: &str) -> String { .into_owned() } +/// Wraps a single header tag with a link, making sure each tag gets its own +/// unique ID by appending an auto-incremented number (if necessary). fn wrap_header_with_link(level: usize, content: &str, id_counter: &mut HashMap, filename: &str) -> String { - let id = id_from_content(content); + let raw_id = id_from_content(content); - let id_count = *id_counter.get(&id).unwrap_or(&0); - id_counter.insert(id.clone(), id_count + 1); + let id_count = id_counter.entry(raw_id.clone()).or_insert(0); - let id = if id_count > 0 { - format!("{}-{}", id, id_count) - } else { - id + let id = match *id_count { + 0 => raw_id, + other => format!("{}-{}", raw_id, other), }; + *id_count += 1; + format!( r#"{text}"#, level = level, From 33f3bec3019d49da62c88b094b10b3c7e592907c Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Tue, 20 Jun 2017 11:23:53 +0800 Subject: [PATCH 12/13] Cleaned up the filter_map for normalizing id's using a more readable procedural style --- src/renderer/html_handlebars/hbs_renderer.rs | 33 ++++++++++---------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index b6be764f..aa7c7ee7 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -410,7 +410,7 @@ fn build_header_links(html: String, filename: &str) -> String { .into_owned() } -/// Wraps a single header tag with a link, making sure each tag gets its own +/// Wraps a single header tag with a link, making sure each tag gets its own /// unique ID by appending an auto-incremented number (if necessary). fn wrap_header_with_link(level: usize, content: &str, id_counter: &mut HashMap, filename: &str) -> String { @@ -434,6 +434,8 @@ fn wrap_header_with_link(level: usize, content: &str, id_counter: &mut HashMap String { let mut content = content.to_string(); @@ -455,19 +457,17 @@ fn id_from_content(content: &str) -> String { content = content.replace(sub, ""); } - content.chars() - .filter_map(|c| if c.is_alphanumeric() || c == '-' || c == '_' { - if c.is_ascii() { - Some(c.to_ascii_lowercase()) - } else { - Some(c) - } - } else if c.is_whitespace() && c.is_ascii() { - Some('-') - } else { - None - }) - .collect() + let mut id = String::new(); + + for c in content.chars() { + if c.is_alphanumeric() || c == '-' || c == '_' { + id.push(c.to_ascii_lowercase()); + } else if c.is_whitespace() { + id.push(c); + } + } + + id } // anchors to the same page (href="#anchor") do not work because of @@ -509,7 +509,7 @@ fn fix_code_blocks(html: String) -> String { let classes = &caps[2].replace(",", " "); let after = &caps[3]; - format!("", before = before, classes = classes, after = after) + format!(r#""#, before = before, classes = classes, after = after) }) .into_owned() } @@ -593,7 +593,8 @@ mod tests { ("

Foo^bar

", r#"

Foo^bar

"#), ("

", r#"

"#), ("

", r#"

"#), - ("

Foo

Foo

", r#"

Foo

Foo

"#), + ("

Foo

Foo

", + r#"

Foo

Foo

"#), ]; for (src, should_be) in inputs { From 4c187bcb9fe336d4481fc118ed16a08c487ff9bb Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Sat, 24 Jun 2017 15:50:51 +0800 Subject: [PATCH 13/13] Explained what HtmlHandlebars::write_custom_function() does --- src/renderer/html_handlebars/hbs_renderer.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index aa7c7ee7..d64a3fab 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -178,6 +178,8 @@ impl HtmlHandlebars { Ok(()) } + /// Helper function to write a file to the build directory, normalizing + /// the path to be relative to the book root. fn write_custom_file(&self, custom_file: &Path, book: &MDBook) -> Result<(), Box> { let mut data = Vec::new(); let mut f = File::open(custom_file)?;