2015-08-06 04:35:26 +08:00
|
|
|
use renderer::html_handlebars::helpers;
|
2015-08-04 21:15:36 +08:00
|
|
|
use renderer::Renderer;
|
2015-08-31 21:24:42 +08:00
|
|
|
use book::MDBook;
|
2015-09-12 02:52:55 +08:00
|
|
|
use book::bookitem::BookItem;
|
2015-08-06 04:35:26 +08:00
|
|
|
use {utils, theme};
|
2017-02-16 14:39:13 +08:00
|
|
|
use regex::{Regex, Captures};
|
2015-08-04 21:15:36 +08:00
|
|
|
|
2017-02-16 14:39:13 +08:00
|
|
|
use std::ascii::AsciiExt;
|
2015-09-05 23:26:17 +08:00
|
|
|
use std::path::{Path, PathBuf};
|
2016-01-03 20:02:04 +08:00
|
|
|
use std::fs::{self, File};
|
2015-08-04 21:15:36 +08:00
|
|
|
use std::error::Error;
|
2017-01-01 14:27:38 +08:00
|
|
|
use std::io::{self, Read};
|
2015-08-04 21:15:36 +08:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
2016-08-01 20:06:08 +08:00
|
|
|
use handlebars::Handlebars;
|
2016-11-03 08:58:42 +08:00
|
|
|
|
|
|
|
use serde_json;
|
2015-12-30 07:46:55 +08:00
|
|
|
|
2015-08-04 21:15:36 +08:00
|
|
|
|
|
|
|
pub struct HtmlHandlebars;
|
|
|
|
|
2015-08-04 22:52:10 +08:00
|
|
|
impl HtmlHandlebars {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
HtmlHandlebars
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 21:15:36 +08:00
|
|
|
impl Renderer for HtmlHandlebars {
|
2015-08-31 21:24:42 +08:00
|
|
|
fn render(&self, book: &MDBook) -> Result<(), Box<Error>> {
|
2015-08-04 21:15:36 +08:00
|
|
|
debug!("[fn]: render");
|
|
|
|
let mut handlebars = Handlebars::new();
|
|
|
|
|
2015-08-04 23:58:09 +08:00
|
|
|
// Load theme
|
2016-12-07 22:22:32 +08:00
|
|
|
let theme = theme::Theme::new(book.get_theme_path());
|
2015-08-04 21:15:36 +08:00
|
|
|
|
|
|
|
// Register template
|
|
|
|
debug!("[*]: Register handlebars template");
|
2015-08-11 22:13:41 +08:00
|
|
|
try!(handlebars.register_template_string("index", try!(String::from_utf8(theme.index))));
|
2015-08-04 21:15:36 +08:00
|
|
|
|
|
|
|
// 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));
|
|
|
|
|
2015-08-31 21:24:42 +08:00
|
|
|
let mut data = try!(make_data(book));
|
2015-08-04 21:15:36 +08:00
|
|
|
|
2015-09-05 23:26:17 +08:00
|
|
|
// Print version
|
|
|
|
let mut print_content: String = String::new();
|
|
|
|
|
2015-08-04 21:15:36 +08:00
|
|
|
// Check if dest directory exists
|
|
|
|
debug!("[*]: Check if destination directory exists");
|
2016-01-03 20:02:04 +08:00
|
|
|
if let Err(_) = fs::create_dir_all(book.get_dest()) {
|
2016-03-18 05:31:28 +08:00
|
|
|
return Err(Box::new(io::Error::new(io::ErrorKind::Other,
|
|
|
|
"Unexpected error when constructing destination path")));
|
2015-09-17 10:46:23 +08:00
|
|
|
}
|
2015-08-04 21:15:36 +08:00
|
|
|
|
|
|
|
// Render a file for every entry in the book
|
|
|
|
let mut index = true;
|
2015-09-12 02:52:55 +08:00
|
|
|
for item in book.iter() {
|
|
|
|
|
2015-09-17 10:46:23 +08:00
|
|
|
match *item {
|
2016-08-01 20:06:08 +08:00
|
|
|
BookItem::Chapter(_, ref ch) |
|
|
|
|
BookItem::Affix(ref ch) => {
|
2015-09-12 02:52:55 +08:00
|
|
|
if ch.path != PathBuf::new() {
|
|
|
|
|
|
|
|
let path = book.get_src().join(&ch.path);
|
|
|
|
|
|
|
|
debug!("[*]: Opening file: {:?}", path);
|
|
|
|
let mut f = try!(File::open(&path));
|
|
|
|
let mut content: String = String::new();
|
|
|
|
|
|
|
|
debug!("[*]: Reading file");
|
|
|
|
try!(f.read_to_string(&mut content));
|
|
|
|
|
2015-12-31 21:04:11 +08:00
|
|
|
// Parse for playpen links
|
|
|
|
if let Some(p) = path.parent() {
|
2016-01-01 02:25:02 +08:00
|
|
|
content = helpers::playpen::render_playpen(&content, p);
|
2015-12-31 21:04:11 +08:00
|
|
|
}
|
|
|
|
|
2015-09-12 02:52:55 +08:00
|
|
|
// Render markdown using the pulldown-cmark crate
|
2015-12-30 07:46:55 +08:00
|
|
|
content = utils::render_markdown(&content);
|
2015-09-12 02:52:55 +08:00
|
|
|
print_content.push_str(&content);
|
|
|
|
|
2017-01-01 10:33:17 +08:00
|
|
|
// Update the context with data for this file
|
2017-01-01 10:41:59 +08:00
|
|
|
let path = ch.path.to_str().ok_or(io::Error::new(io::ErrorKind::Other,
|
|
|
|
"Could not convert path to str"))?;
|
2017-02-16 11:34:37 +08:00
|
|
|
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)));
|
2015-09-12 02:52:55 +08:00
|
|
|
|
2017-01-01 10:33:17 +08:00
|
|
|
// Render the handlebars template with the data
|
2015-09-12 02:52:55 +08:00
|
|
|
debug!("[*]: Render template");
|
|
|
|
let rendered = try!(handlebars.render("index", &data));
|
2017-02-16 14:39:13 +08:00
|
|
|
|
|
|
|
// create links for headers
|
|
|
|
let rendered = build_header_links(rendered);
|
2015-09-12 02:52:55 +08:00
|
|
|
|
|
|
|
// Write to file
|
2017-01-01 14:27:38 +08:00
|
|
|
let filename = Path::new(&ch.path).with_extension("html");
|
|
|
|
info!("[*] Creating {:?} ✓", filename.display());
|
|
|
|
try!(book.write_file(filename, &rendered.into_bytes()));
|
2015-09-12 02:52:55 +08:00
|
|
|
|
|
|
|
// Create an index.html from the first element in SUMMARY.md
|
|
|
|
if index {
|
|
|
|
debug!("[*]: index.html");
|
2015-09-14 23:45:30 +08:00
|
|
|
|
|
|
|
let mut content = String::new();
|
|
|
|
let _source = try!(File::open(book.get_dest().join(&ch.path.with_extension("html"))))
|
2016-08-01 20:06:08 +08:00
|
|
|
.read_to_string(&mut content);
|
2015-09-14 23:45:30 +08:00
|
|
|
|
|
|
|
// This could cause a problem when someone displays code containing <base href=...>
|
|
|
|
// on the front page, however this case should be very very rare...
|
2016-03-18 05:31:28 +08:00
|
|
|
content = content.lines()
|
2016-08-01 20:06:08 +08:00
|
|
|
.filter(|line| !line.contains("<base href="))
|
|
|
|
.collect::<Vec<&str>>()
|
|
|
|
.join("\n");
|
2015-09-14 23:45:30 +08:00
|
|
|
|
2017-01-01 14:27:38 +08:00
|
|
|
try!(book.write_file("index.html", content.as_bytes()));
|
2015-09-12 02:52:55 +08:00
|
|
|
|
2016-08-14 20:32:34 +08:00
|
|
|
info!("[*] Creating index.html from {:?} ✓",
|
|
|
|
book.get_dest().join(&ch.path.with_extension("html")));
|
2015-09-12 02:52:55 +08:00
|
|
|
index = false;
|
|
|
|
}
|
|
|
|
}
|
2016-03-18 05:31:28 +08:00
|
|
|
},
|
|
|
|
_ => {},
|
2015-08-04 21:15:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-05 23:26:17 +08:00
|
|
|
// Print version
|
|
|
|
|
2017-01-01 10:33:17 +08:00
|
|
|
// Update the context with data for this file
|
2017-02-16 11:34:37 +08:00
|
|
|
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"))));
|
2015-09-05 23:26:17 +08:00
|
|
|
|
2017-01-01 10:33:17 +08:00
|
|
|
// Render the handlebars template with the data
|
2015-09-05 23:26:17 +08:00
|
|
|
debug!("[*]: Render template");
|
|
|
|
let rendered = try!(handlebars.render("index", &data));
|
2017-01-01 14:27:38 +08:00
|
|
|
try!(book.write_file(Path::new("print").with_extension("html"), &rendered.into_bytes()));
|
2016-08-14 20:32:34 +08:00
|
|
|
info!("[*] Creating print.html ✓");
|
2015-09-05 23:26:17 +08:00
|
|
|
|
2015-08-04 21:15:36 +08:00
|
|
|
// Copy static files (js, css, images, ...)
|
|
|
|
|
|
|
|
debug!("[*] Copy static files");
|
2017-01-01 14:27:38 +08:00
|
|
|
try!(book.write_file("book.js", &theme.js));
|
|
|
|
try!(book.write_file("book.css", &theme.css));
|
|
|
|
try!(book.write_file("favicon.png", &theme.favicon));
|
|
|
|
try!(book.write_file("jquery.js", &theme.jquery));
|
|
|
|
try!(book.write_file("highlight.css", &theme.highlight_css));
|
|
|
|
try!(book.write_file("tomorrow-night.css", &theme.tomorrow_night_css));
|
|
|
|
try!(book.write_file("highlight.js", &theme.highlight_js));
|
|
|
|
try!(book.write_file("_FontAwesome/css/font-awesome.css", theme::FONT_AWESOME));
|
|
|
|
try!(book.write_file("_FontAwesome/fonts/fontawesome-webfont.eot", theme::FONT_AWESOME_EOT));
|
|
|
|
try!(book.write_file("_FontAwesome/fonts/fontawesome-webfont.svg", theme::FONT_AWESOME_SVG));
|
|
|
|
try!(book.write_file("_FontAwesome/fonts/fontawesome-webfont.ttf", theme::FONT_AWESOME_TTF));
|
|
|
|
try!(book.write_file("_FontAwesome/fonts/fontawesome-webfont.woff", theme::FONT_AWESOME_WOFF));
|
|
|
|
try!(book.write_file("_FontAwesome/fonts/fontawesome-webfont.woff2", theme::FONT_AWESOME_WOFF2));
|
|
|
|
try!(book.write_file("_FontAwesome/fonts/FontAwesome.ttf", theme::FONT_AWESOME_TTF));
|
2015-09-14 02:03:34 +08:00
|
|
|
|
2015-09-18 01:45:06 +08:00
|
|
|
// Copy all remaining files
|
2016-03-18 05:41:00 +08:00
|
|
|
try!(utils::fs::copy_files_except_ext(book.get_src(), book.get_dest(), true, &["md"]));
|
2015-09-18 01:45:06 +08:00
|
|
|
|
2015-08-04 21:15:36 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-03 08:58:42 +08:00
|
|
|
fn make_data(book: &MDBook) -> Result<serde_json::Map<String, serde_json::Value>, Box<Error>> {
|
2015-08-04 21:15:36 +08:00
|
|
|
debug!("[fn]: make_data");
|
|
|
|
|
2016-11-03 08:58:42 +08:00
|
|
|
let mut data = serde_json::Map::new();
|
2017-02-16 11:34:37 +08:00
|
|
|
data.insert("language".to_owned(), json!("en"));
|
|
|
|
data.insert("title".to_owned(), json!(book.get_title()));
|
|
|
|
data.insert("description".to_owned(), json!(book.get_description()));
|
|
|
|
data.insert("favicon".to_owned(), json!("favicon.png"));
|
2016-04-02 10:46:05 +08:00
|
|
|
if let Some(livereload) = book.get_livereload() {
|
2017-02-16 11:34:37 +08:00
|
|
|
data.insert("livereload".to_owned(), json!(livereload));
|
2016-04-02 10:46:05 +08:00
|
|
|
}
|
2015-08-04 21:15:36 +08:00
|
|
|
|
|
|
|
let mut chapters = vec![];
|
|
|
|
|
2015-09-12 02:52:55 +08:00
|
|
|
for item in book.iter() {
|
|
|
|
// Create the data to inject in the template
|
2015-08-04 21:15:36 +08:00
|
|
|
let mut chapter = BTreeMap::new();
|
2015-09-12 02:52:55 +08:00
|
|
|
|
2015-09-17 10:46:23 +08:00
|
|
|
match *item {
|
|
|
|
BookItem::Affix(ref ch) => {
|
2017-02-16 11:34:37 +08:00
|
|
|
chapter.insert("name".to_owned(), json!(ch.name));
|
2017-01-01 10:41:59 +08:00
|
|
|
let path = ch.path.to_str().ok_or(io::Error::new(io::ErrorKind::Other,
|
|
|
|
"Could not convert path to str"))?;
|
2017-02-16 11:34:37 +08:00
|
|
|
chapter.insert("path".to_owned(), json!(path));
|
2015-09-12 02:52:55 +08:00
|
|
|
},
|
2015-09-17 10:46:23 +08:00
|
|
|
BookItem::Chapter(ref s, ref ch) => {
|
2017-02-16 11:34:37 +08:00
|
|
|
chapter.insert("section".to_owned(), json!(s));
|
|
|
|
chapter.insert("name".to_owned(), json!(ch.name));
|
2017-01-01 10:41:59 +08:00
|
|
|
let path = ch.path.to_str().ok_or(io::Error::new(io::ErrorKind::Other,
|
|
|
|
"Could not convert path to str"))?;
|
2017-02-16 11:34:37 +08:00
|
|
|
chapter.insert("path".to_owned(), json!(path));
|
2015-09-12 02:52:55 +08:00
|
|
|
},
|
2015-09-17 10:46:23 +08:00
|
|
|
BookItem::Spacer => {
|
2017-02-16 11:34:37 +08:00
|
|
|
chapter.insert("spacer".to_owned(), json!("_spacer_"));
|
2016-03-18 05:31:28 +08:00
|
|
|
},
|
2015-09-12 02:52:55 +08:00
|
|
|
|
2015-08-12 04:55:51 +08:00
|
|
|
}
|
2015-08-04 21:15:36 +08:00
|
|
|
|
|
|
|
chapters.push(chapter);
|
|
|
|
}
|
|
|
|
|
2017-02-16 11:34:37 +08:00
|
|
|
data.insert("chapters".to_owned(), json!(chapters));
|
2015-08-04 21:15:36 +08:00
|
|
|
|
|
|
|
debug!("[*]: JSON constructed");
|
|
|
|
Ok(data)
|
|
|
|
}
|
2017-02-16 14:39:13 +08:00
|
|
|
|
|
|
|
fn build_header_links(mut html: String) -> String {
|
|
|
|
for header in &["h1", "h2", "h3", "h4", "h5"] {
|
|
|
|
let regex = Regex::new(&format!("<{h}>(.*?)</{h}>", h=header)).unwrap();
|
|
|
|
|
|
|
|
html = regex.replace_all(&html, |caps: &Captures| {
|
|
|
|
let text = &caps[1];
|
|
|
|
let mut id = text.to_string();
|
|
|
|
let repl_sub = vec!["<em>", "</em>", "<code>", "</code>",
|
|
|
|
"<strong>", "</strong>",
|
|
|
|
"<", ">", "&", "'", """];
|
|
|
|
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::<String>();
|
|
|
|
|
|
|
|
format!("<a class=\"header\" href=\"#{id}\" name=\"{id}\"><{h}>{text}</{h}></a>", h=header, id=id, text=text)
|
2017-02-17 06:11:16 +08:00
|
|
|
}).into_owned();
|
2017-02-16 14:39:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
html
|
|
|
|
}
|