Merge pull request #335 from Michael-F-Bryan/refactor-hbs-renderer

Refactor hbs renderer
This commit is contained in:
Mathieu David 2017-06-24 14:44:12 +02:00 committed by GitHub
commit b441066105
1 changed files with 376 additions and 220 deletions

View File

@ -1,8 +1,9 @@
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, theme}; use utils;
use theme::{self, Theme};
use regex::{Regex, Captures}; use regex::{Regex, Captures};
use std::ascii::AsciiExt; use std::ascii::AsciiExt;
@ -25,49 +26,16 @@ impl HtmlHandlebars {
pub fn new() -> Self { pub fn new() -> Self {
HtmlHandlebars 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, mut ctx: RenderItemContext, print_content: &mut String)
-> Result<(), Box<Error>> {
// FIXME: This should be made DRY-er and rely less on mutable state
match *item { match *item {
BookItem::Chapter(_, ref ch) | BookItem::Chapter(_, ref ch) |
BookItem::Affix(ref ch) => { BookItem::Affix(ref ch) => {
if ch.path != PathBuf::new() { 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); debug!("[*]: Opening file: {:?}", path);
let mut f = File::open(&path)?; let mut f = File::open(&path)?;
@ -81,47 +49,52 @@ impl Renderer for HtmlHandlebars {
content = helpers::playpen::render_playpen(&content, p); content = helpers::playpen::render_playpen(&content, p);
} }
// Render markdown using the pulldown-cmark crate content = utils::render_markdown(&content, ctx.book.get_curly_quotes());
content = utils::render_markdown(&content, book.get_curly_quotes());
print_content.push_str(&content); print_content.push_str(&content);
// Update the context with data for this file // Update the context with data for this file
let path = let path = ch.path.to_str().ok_or_else(|| {
ch.path io::Error::new(io::ErrorKind::Other, "Could not convert path to str")
.to_str() })?;
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))?;
data.insert("path".to_owned(), json!(path)); ctx.data.insert("path".to_owned(), json!(path));
data.insert("content".to_owned(), json!(content)); ctx.data.insert("content".to_owned(), json!(content));
data.insert("chapter_title".to_owned(), json!(ch.name)); ctx.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_root".to_owned(),
json!(utils::fs::path_to_root(&ch.path)),
);
// Render the handlebars template with the data // Render the handlebars template with the data
debug!("[*]: Render template"); 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"); 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 // Write to file
info!("[*] Creating {:?} ✓", filename.display()); 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 ctx.is_index {
if index { self.render_index(&ctx.book, ch, &ctx.destination)?;
}
}
},
_ => {},
}
Ok(())
}
/// Create an index.html from the first element in SUMMARY.md
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();
let _source = File::open( File::open(destination.join(&ch.path.with_extension("html")))?
book.get_destination() .read_to_string(&mut content)?;
.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 // This could cause a problem when someone displays
// code containing <base href=...> // code containing <base href=...>
@ -134,64 +107,80 @@ impl Renderer for HtmlHandlebars {
book.write_file("index.html", content.as_bytes())?; book.write_file("index.html", content.as_bytes())?;
info!("[*] Creating index.html from {:?} ✓", info!(
"[*] Creating index.html from {:?} ✓",
book.get_destination() book.get_destination()
.expect("If the HTML renderer is called, one would assume the HtmlConfig is set... (4)") .expect(
"If the HTML renderer is called, one would assume the HtmlConfig is \
set... (4)",
)
.join(&ch.path.with_extension("html")) .join(&ch.path.with_extension("html"))
); );
index = false;
} Ok(())
}
},
_ => {},
}
} }
// Print version fn post_process(&self, rendered: String) -> String {
// 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("print.md")));
// Render the handlebars template with the data
debug!("[*]: Render template");
let rendered = handlebars.render("index", &data)?;
// do several kinds of post-processing
let rendered = build_header_links(rendered, "print.html"); let rendered = build_header_links(rendered, "print.html");
let rendered = fix_anchor_links(rendered, "print.html"); let rendered = fix_anchor_links(rendered, "print.html");
let rendered = fix_code_blocks(rendered); let rendered = fix_code_blocks(rendered);
let rendered = add_playpen_pre(rendered); let rendered = add_playpen_pre(rendered);
book.write_file(Path::new("print").with_extension("html"), &rendered.into_bytes())?; rendered
info!("[*] Creating print.html ✓"); }
// Copy static files (js, css, images, ...) fn copy_static_files(&self, book: &MDBook, theme: &Theme) -> Result<(), Box<Error>> {
debug!("[*] Copy static files");
book.write_file("book.js", &theme.js)?; book.write_file("book.js", &theme.js)?;
book.write_file("book.css", &theme.css)?; book.write_file("book.css", &theme.css)?;
book.write_file("favicon.png", &theme.favicon)?; book.write_file("favicon.png", &theme.favicon)?;
book.write_file("jquery.js", &theme.jquery)?; book.write_file("jquery.js", &theme.jquery)?;
book.write_file("highlight.css", &theme.highlight_css)?; book.write_file("highlight.css", &theme.highlight_css)?;
book.write_file("tomorrow-night.css", &theme.tomorrow_night_css)?; book.write_file(
book.write_file("ayu-highlight.css", &theme.ayu_highlight_css)?; "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("highlight.js", &theme.highlight_js)?;
book.write_file("clipboard.min.js", &theme.clipboard_js)?; book.write_file("clipboard.min.js", &theme.clipboard_js)?;
book.write_file("store.js", &theme.store_js)?; book.write_file("store.js", &theme.store_js)?;
book.write_file("_FontAwesome/css/font-awesome.css", theme::FONT_AWESOME)?; book.write_file(
book.write_file("_FontAwesome/fonts/fontawesome-webfont.eot", theme::FONT_AWESOME_EOT)?; "_FontAwesome/css/font-awesome.css",
book.write_file("_FontAwesome/fonts/fontawesome-webfont.svg", theme::FONT_AWESOME_SVG)?; theme::FONT_AWESOME,
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(
book.write_file("_FontAwesome/fonts/fontawesome-webfont.woff2", theme::FONT_AWESOME_WOFF2)?; "_FontAwesome/fonts/fontawesome-webfont.eot",
book.write_file("_FontAwesome/fonts/FontAwesome.ttf", theme::FONT_AWESOME_TTF)?; 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,
)?;
for custom_file in book.get_additional_css() Ok(())
.iter() }
.chain(book.get_additional_js().iter()) {
/// 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<Error>> {
let mut data = Vec::new(); let mut data = Vec::new();
let mut f = File::open(custom_file)?; let mut f = File::open(custom_file)?;
f.read_to_end(&mut data)?; f.read_to_end(&mut data)?;
@ -204,19 +193,110 @@ impl Renderer for HtmlHandlebars {
.expect("File has a file name") .expect("File has a file name")
.to_str() .to_str()
.expect("Could not convert to str") .expect("Could not convert to str")
} },
}; };
book.write_file(name, &data)?; book.write_file(name, &data)?;
Ok(())
} }
// Copy all remaining files /// Update the context with data for this file
utils::fs::copy_files_except_ext( fn configure_print_version(&self, data: &mut serde_json::Map<String, serde_json::Value>, print_content: &str) {
book.get_source(), data.insert("path".to_owned(), json!("print.md"));
book.get_destination() data.insert("content".to_owned(), json!(print_content));
.expect("If the HTML renderer is called, one would assume the HtmlConfig is set... (5)"), true, &["md"] 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));
}
/// 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<Error>> {
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(())
}
}
impl Renderer for HtmlHandlebars {
fn render(&self, book: &MDBook) -> Result<(), Box<Error>> {
debug!("[fn]: render");
let mut handlebars = Handlebars::new();
let theme = theme::Theme::new(book.get_theme_path());
debug!("[*]: Register handlebars template");
handlebars.register_template_string(
"index",
String::from_utf8(theme.index.clone())?,
)?; )?;
debug!("[*]: Register handlebars helpers");
self.register_hbs_helpers(&mut handlebars);
let mut data = make_data(book)?;
// 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)",
);
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"),
));
}
for (i, item) in book.iter().enumerate() {
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
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_process(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)?;
self.copy_additional_css_and_js(book)?;
// Copy all remaining files
utils::fs::copy_files_except_ext(book.get_source(), &destination, true, &["md"])?;
Ok(()) Ok(())
} }
} }
@ -244,7 +324,15 @@ fn make_data(book: &MDBook) -> Result<serde_json::Map<String, serde_json::Value>
for style in book.get_additional_css() { for style in book.get_additional_css() {
match style.strip_prefix(book.get_root()) { match style.strip_prefix(book.get_root()) {
Ok(p) => css.push(p.to_str().expect("Could not convert to str")), 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)); data.insert("additional_css".to_owned(), json!(css));
@ -256,7 +344,15 @@ fn make_data(book: &MDBook) -> Result<serde_json::Map<String, serde_json::Value>
for script in book.get_additional_js() { for script in book.get_additional_js() {
match script.strip_prefix(book.get_root()) { match script.strip_prefix(book.get_root()) {
Ok(p) => js.push(p.to_str().expect("Could not convert to str")), 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)); data.insert("additional_js".to_owned(), json!(js));
@ -271,17 +367,17 @@ fn make_data(book: &MDBook) -> Result<serde_json::Map<String, serde_json::Value>
match *item { match *item {
BookItem::Affix(ref ch) => { BookItem::Affix(ref ch) => {
chapter.insert("name".to_owned(), json!(ch.name)); chapter.insert("name".to_owned(), json!(ch.name));
let path = ch.path let path = ch.path.to_str().ok_or_else(|| {
.to_str() io::Error::new(io::ErrorKind::Other, "Could not convert 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)); chapter.insert("path".to_owned(), json!(path));
}, },
BookItem::Chapter(ref s, ref ch) => { BookItem::Chapter(ref s, ref ch) => {
chapter.insert("section".to_owned(), json!(s)); chapter.insert("section".to_owned(), json!(s));
chapter.insert("name".to_owned(), json!(ch.name)); chapter.insert("name".to_owned(), json!(ch.name));
let path = ch.path let path = ch.path.to_str().ok_or_else(|| {
.to_str() io::Error::new(io::ErrorKind::Other, "Could not convert 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)); chapter.insert("path".to_owned(), json!(path));
}, },
BookItem::Spacer => { BookItem::Spacer => {
@ -299,16 +395,55 @@ fn make_data(book: &MDBook) -> Result<serde_json::Map<String, serde_json::Value>
Ok(data) 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 { fn build_header_links(html: String, filename: &str) -> String {
let regex = Regex::new(r"<h(\d)>(.*?)</h\d>").unwrap(); let regex = Regex::new(r"<h(\d)>(.*?)</h\d>").unwrap();
let mut id_counter = HashMap::new(); let mut id_counter = HashMap::new();
regex regex
.replace_all(&html, |caps: &Captures| { .replace_all(&html, |caps: &Captures| {
let level = &caps[1]; let level = caps[1].parse().expect(
let text = &caps[2]; "Regex should ensure we only ever get numbers here",
let mut id = text.to_string(); );
let repl_sub = vec!["<em>",
wrap_header_with_link(level, &caps[2], &mut id_counter, filename)
})
.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<String, usize>, filename: &str)
-> String {
let raw_id = id_from_content(content);
let id_count = id_counter.entry(raw_id.clone()).or_insert(0);
let id = match *id_count {
0 => raw_id,
other => format!("{}-{}", raw_id, other),
};
*id_count += 1;
format!(
r#"<a class="header" href="{filename}#{id}" id="{id}"><h{level}>{text}</h{level}></a>"#,
level = level,
id = id,
text = content,
filename = filename
)
}
/// Generate an id for use with anchors which is derived from a "normalised"
/// string.
fn id_from_content(content: &str) -> String {
let mut content = content.to_string();
// Skip any tags or html-encoded stuff
let repl_sub = vec![
"<em>",
"</em>", "</em>",
"<code>", "<code>",
"</code>", "</code>",
@ -318,40 +453,23 @@ fn build_header_links(html: String, filename: &str) -> String {
"&gt;", "&gt;",
"&amp;", "&amp;",
"&#39;", "&#39;",
"&quot;"]; "&quot;",
];
for sub in repl_sub { for sub in repl_sub {
id = id.replace(sub, ""); content = content.replace(sub, "");
}
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);
} }
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::<String>();
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 id
};
format!("<a class=\"header\" href=\"{filename}#{id}\" id=\"{id}\"><h{level}>{text}</h{level}></a>",
level = level,
id = id,
text = text,
filename = filename)
})
.into_owned()
} }
// anchors to the same page (href="#anchor") do not work because of // anchors to the same page (href="#anchor") do not work because of
@ -365,11 +483,13 @@ fn fix_anchor_links(html: String, filename: &str) -> String {
let anchor = &caps[2]; let anchor = &caps[2];
let after = &caps[3]; let after = &caps[3];
format!("<a{before}href=\"{filename}#{anchor}\"{after}>", format!(
"<a{before}href=\"{filename}#{anchor}\"{after}>",
before = before, before = before,
filename = filename, filename = filename,
anchor = anchor, anchor = anchor,
after = after) after = after
)
}) })
.into_owned() .into_owned()
} }
@ -391,7 +511,7 @@ fn fix_code_blocks(html: String) -> String {
let classes = &caps[2].replace(",", " "); let classes = &caps[2].replace(",", " ");
let after = &caps[3]; let after = &caps[3];
format!("<code{before}class=\"{classes}\"{after}>", before = before, classes = classes, after = after) format!(r#"<code{before}class="{classes}"{after}>"#, before = before, classes = classes, after = after)
}) })
.into_owned() .into_owned()
} }
@ -412,14 +532,16 @@ fn add_playpen_pre(html: String) -> String {
} else { } else {
// we need to inject our own main // we need to inject our own main
let (attrs, code) = partition_source(code); let (attrs, code) = partition_source(code);
format!("<pre class=\"playpen\"><code class=\"{}\"># #![allow(unused_variables)] format!(
"<pre class=\"playpen\"><code class=\"{}\"># #![allow(unused_variables)]
{}#fn main() {{ {}#fn main() {{
\ \
{} {}
#}}</code></pre>", #}}</code></pre>",
classes, classes,
attrs, attrs,
code) code
)
} }
} else { } else {
// not language-rust, so no-op // not language-rust, so no-op
@ -449,3 +571,37 @@ fn partition_source(s: &str) -> (String, String) {
(before, after) (before, after)
} }
struct RenderItemContext<'a> {
handlebars: &'a Handlebars,
book: &'a MDBook,
destination: PathBuf,
data: serde_json::Map<String, serde_json::Value>,
is_index: bool,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn original_build_header_links() {
let inputs = vec![
("blah blah <h1>Foo</h1>", r#"blah blah <a class="header" href="bar.rs#foo" id="foo"><h1>Foo</h1></a>"#),
("<h1>Foo</h1>", r#"<a class="header" href="bar.rs#foo" id="foo"><h1>Foo</h1></a>"#),
("<h3>Foo^bar</h3>", r#"<a class="header" href="bar.rs#foobar" id="foobar"><h3>Foo^bar</h3></a>"#),
("<h4></h4>", r#"<a class="header" href="bar.rs#" id=""><h4></h4></a>"#),
("<h4><em>Hï</em></h4>", r#"<a class="header" href="bar.rs#hï" id="hï"><h4><em>Hï</em></h4></a>"#),
("<h1>Foo</h1><h3>Foo</h3>",
r#"<a class="header" href="bar.rs#foo" id="foo"><h1>Foo</h1></a><a class="header" href="bar.rs#foo-1" id="foo-1"><h3>Foo</h3></a>"#),
];
for (src, should_be) in inputs {
let got = build_header_links(src.to_string(), "bar.rs");
assert_eq!(got, should_be);
}
}
}