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};
|
2015-08-04 21:15:36 +08:00
|
|
|
|
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;
|
|
|
|
use std::io::{self, Read, Write};
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
2016-03-28 00:22:17 +08:00
|
|
|
use handlebars::{Handlebars, JsonRender};
|
|
|
|
use rustc_serialize::json::{Json, ToJson};
|
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
|
2015-08-31 21:24:42 +08:00
|
|
|
let theme = theme::Theme::new(book.get_src());
|
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 {
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Remove content from previous file and render content for this one
|
|
|
|
data.remove("path");
|
|
|
|
match ch.path.to_str() {
|
2016-03-18 05:31:28 +08:00
|
|
|
Some(p) => {
|
|
|
|
data.insert("path".to_owned(), p.to_json());
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
return Err(Box::new(io::Error::new(io::ErrorKind::Other,
|
|
|
|
"Could not convert path to str")))
|
|
|
|
},
|
2015-09-12 02:52:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Remove content from previous file and render content for this one
|
|
|
|
data.remove("content");
|
2015-09-17 10:46:23 +08:00
|
|
|
data.insert("content".to_owned(), content.to_json());
|
2015-09-12 02:52:55 +08:00
|
|
|
|
|
|
|
// Remove path to root from previous file and render content for this one
|
|
|
|
data.remove("path_to_root");
|
2016-03-18 05:41:00 +08:00
|
|
|
data.insert("path_to_root".to_owned(), utils::fs::path_to_root(&ch.path).to_json());
|
2015-09-12 02:52:55 +08:00
|
|
|
|
|
|
|
// Rendere the handlebars template with the data
|
|
|
|
debug!("[*]: Render template");
|
|
|
|
let rendered = try!(handlebars.render("index", &data));
|
|
|
|
|
|
|
|
debug!("[*]: Create file {:?}", &book.get_dest().join(&ch.path).with_extension("html"));
|
|
|
|
// Write to file
|
2016-03-18 05:41:00 +08:00
|
|
|
let mut file = try!(utils::fs::create_file(&book.get_dest().join(&ch.path).with_extension("html")));
|
2015-09-12 02:52:55 +08:00
|
|
|
output!("[*] Creating {:?} ✓", &book.get_dest().join(&ch.path).with_extension("html"));
|
|
|
|
|
|
|
|
try!(file.write_all(&rendered.into_bytes()));
|
|
|
|
|
|
|
|
// 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 index_file = try!(File::create(book.get_dest().join("index.html")));
|
|
|
|
let mut content = String::new();
|
|
|
|
let _source = try!(File::open(book.get_dest().join(&ch.path.with_extension("html"))))
|
2016-03-18 05:31:28 +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()
|
|
|
|
.filter(|line| !line.contains("<base href="))
|
|
|
|
.collect::<Vec<&str>>()
|
|
|
|
.join("\n");
|
2015-09-14 23:45:30 +08:00
|
|
|
|
|
|
|
try!(index_file.write_all(content.as_bytes()));
|
2015-09-12 02:52:55 +08:00
|
|
|
|
2016-03-18 05:31:28 +08:00
|
|
|
output!("[*] 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
|
|
|
|
|
|
|
|
// Remove content from previous file and render content for this one
|
|
|
|
data.remove("path");
|
2015-09-17 10:46:23 +08:00
|
|
|
data.insert("path".to_owned(), "print.md".to_json());
|
2015-09-05 23:26:17 +08:00
|
|
|
|
|
|
|
// Remove content from previous file and render content for this one
|
|
|
|
data.remove("content");
|
2015-09-17 10:46:23 +08:00
|
|
|
data.insert("content".to_owned(), print_content.to_json());
|
2015-09-05 23:26:17 +08:00
|
|
|
|
|
|
|
// Remove path to root from previous file and render content for this one
|
|
|
|
data.remove("path_to_root");
|
2016-03-18 05:41:00 +08:00
|
|
|
data.insert("path_to_root".to_owned(), utils::fs::path_to_root(Path::new("print.md")).to_json());
|
2015-09-05 23:26:17 +08:00
|
|
|
|
|
|
|
// Rendere the handlebars template with the data
|
|
|
|
debug!("[*]: Render template");
|
|
|
|
let rendered = try!(handlebars.render("index", &data));
|
2016-03-18 05:41:00 +08:00
|
|
|
let mut file = try!(utils::fs::create_file(&book.get_dest().join("print").with_extension("html")));
|
2015-09-05 23:26:17 +08:00
|
|
|
try!(file.write_all(&rendered.into_bytes()));
|
2015-09-05 23:39:00 +08:00
|
|
|
output!("[*] 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");
|
|
|
|
// JavaScript
|
2016-03-18 05:31:28 +08:00
|
|
|
let mut js_file = if let Ok(f) = File::create(book.get_dest().join("book.js")) {
|
|
|
|
f
|
|
|
|
} else {
|
|
|
|
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create book.js")));
|
2016-01-03 20:02:04 +08:00
|
|
|
};
|
2015-08-04 23:58:09 +08:00
|
|
|
try!(js_file.write_all(&theme.js));
|
2015-08-04 21:15:36 +08:00
|
|
|
|
|
|
|
// Css
|
2016-03-18 05:31:28 +08:00
|
|
|
let mut css_file = if let Ok(f) = File::create(book.get_dest().join("book.css")) {
|
|
|
|
f
|
|
|
|
} else {
|
|
|
|
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create book.css")));
|
2016-01-03 20:02:04 +08:00
|
|
|
};
|
2015-08-04 23:58:09 +08:00
|
|
|
try!(css_file.write_all(&theme.css));
|
2015-08-04 21:15:36 +08:00
|
|
|
|
2016-02-23 00:17:07 +08:00
|
|
|
// Favicon
|
2016-03-18 05:31:28 +08:00
|
|
|
let mut favicon_file = if let Ok(f) = File::create(book.get_dest().join("favicon.png")) {
|
|
|
|
f
|
|
|
|
} else {
|
|
|
|
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create favicon.png")));
|
2016-02-23 00:17:07 +08:00
|
|
|
};
|
|
|
|
try!(favicon_file.write_all(&theme.favicon));
|
|
|
|
|
2015-09-14 02:03:34 +08:00
|
|
|
// JQuery local fallback
|
2016-03-18 05:31:28 +08:00
|
|
|
let mut jquery = if let Ok(f) = File::create(book.get_dest().join("jquery.js")) {
|
|
|
|
f
|
|
|
|
} else {
|
|
|
|
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create jquery.js")));
|
2016-01-03 20:02:04 +08:00
|
|
|
};
|
2015-09-14 01:16:11 +08:00
|
|
|
try!(jquery.write_all(&theme.jquery));
|
|
|
|
|
2016-01-03 20:02:04 +08:00
|
|
|
// syntax highlighting
|
2016-03-18 05:31:28 +08:00
|
|
|
let mut highlight_css = if let Ok(f) = File::create(book.get_dest().join("highlight.css")) {
|
|
|
|
f
|
|
|
|
} else {
|
|
|
|
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create highlight.css")));
|
2016-01-03 20:02:04 +08:00
|
|
|
};
|
|
|
|
try!(highlight_css.write_all(&theme.highlight_css));
|
|
|
|
|
2016-03-18 05:31:28 +08:00
|
|
|
let mut tomorrow_night_css = if let Ok(f) = File::create(book.get_dest().join("tomorrow-night.css")) {
|
|
|
|
f
|
|
|
|
} else {
|
|
|
|
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create tomorrow-night.css")));
|
2016-01-03 20:02:04 +08:00
|
|
|
};
|
|
|
|
try!(tomorrow_night_css.write_all(&theme.tomorrow_night_css));
|
|
|
|
|
2016-03-18 05:31:28 +08:00
|
|
|
let mut highlight_js = if let Ok(f) = File::create(book.get_dest().join("highlight.js")) {
|
|
|
|
f
|
|
|
|
} else {
|
|
|
|
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create highlight.js")));
|
2016-01-03 20:02:04 +08:00
|
|
|
};
|
|
|
|
try!(highlight_js.write_all(&theme.highlight_js));
|
|
|
|
|
2015-09-14 02:03:34 +08:00
|
|
|
// Font Awesome local fallback
|
2016-03-18 05:41:00 +08:00
|
|
|
let mut font_awesome = if let Ok(f) = utils::fs::create_file(&book.get_dest()
|
2016-03-18 05:31:28 +08:00
|
|
|
.join("_FontAwesome/css/font-awesome.css")) {
|
|
|
|
f
|
|
|
|
} else {
|
|
|
|
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create font-awesome.css")));
|
2016-01-03 20:02:04 +08:00
|
|
|
};
|
2015-09-14 02:03:34 +08:00
|
|
|
try!(font_awesome.write_all(theme::FONT_AWESOME));
|
2016-03-18 05:41:00 +08:00
|
|
|
let mut font_awesome = if let Ok(f) = utils::fs::create_file(&book.get_dest()
|
2016-03-18 05:31:28 +08:00
|
|
|
.join("_FontAwesome/fonts/fontawesome-webfon\
|
|
|
|
t.eot")) {
|
|
|
|
f
|
|
|
|
} else {
|
|
|
|
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create fontawesome-webfont.eot")));
|
2016-01-03 20:02:04 +08:00
|
|
|
};
|
2015-09-14 02:03:34 +08:00
|
|
|
try!(font_awesome.write_all(theme::FONT_AWESOME_EOT));
|
2016-03-18 05:41:00 +08:00
|
|
|
let mut font_awesome = if let Ok(f) = utils::fs::create_file(&book.get_dest()
|
2016-03-18 05:31:28 +08:00
|
|
|
.join("_FontAwesome/fonts/fontawesome-webfon\
|
|
|
|
t.svg")) {
|
|
|
|
f
|
|
|
|
} else {
|
|
|
|
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create fontawesome-webfont.svg")));
|
2016-01-03 20:02:04 +08:00
|
|
|
};
|
2015-09-14 02:03:34 +08:00
|
|
|
try!(font_awesome.write_all(theme::FONT_AWESOME_SVG));
|
2016-03-18 05:41:00 +08:00
|
|
|
let mut font_awesome = if let Ok(f) = utils::fs::create_file(&book.get_dest()
|
2016-03-18 05:31:28 +08:00
|
|
|
.join("_FontAwesome/fonts/fontawesome-webfon\
|
|
|
|
t.ttf")) {
|
|
|
|
f
|
|
|
|
} else {
|
|
|
|
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create fontawesome-webfont.ttf")));
|
2016-01-03 20:02:04 +08:00
|
|
|
};
|
2015-09-14 02:03:34 +08:00
|
|
|
try!(font_awesome.write_all(theme::FONT_AWESOME_TTF));
|
2016-03-18 05:41:00 +08:00
|
|
|
let mut font_awesome = if let Ok(f) = utils::fs::create_file(&book.get_dest()
|
2016-03-18 05:31:28 +08:00
|
|
|
.join("_FontAwesome/fonts/fontawesome-webfon\
|
|
|
|
t.woff")) {
|
|
|
|
f
|
|
|
|
} else {
|
|
|
|
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create fontawesome-webfont.woff")));
|
2016-01-03 20:02:04 +08:00
|
|
|
};
|
2015-09-14 02:03:34 +08:00
|
|
|
try!(font_awesome.write_all(theme::FONT_AWESOME_WOFF));
|
2016-03-18 05:41:00 +08:00
|
|
|
let mut font_awesome = if let Ok(f) = utils::fs::create_file(&book.get_dest()
|
2016-03-18 05:31:28 +08:00
|
|
|
.join("_FontAwesome/fonts/fontawesome-webfon\
|
|
|
|
t.woff2")) {
|
|
|
|
f
|
|
|
|
} else {
|
|
|
|
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create fontawesome-webfont.woff2")));
|
2016-01-03 20:02:04 +08:00
|
|
|
};
|
2015-09-14 02:03:34 +08:00
|
|
|
try!(font_awesome.write_all(theme::FONT_AWESOME_WOFF2));
|
2016-03-18 05:41:00 +08:00
|
|
|
let mut font_awesome = if let Ok(f) = utils::fs::create_file(&book.get_dest()
|
2016-03-18 05:31:28 +08:00
|
|
|
.join("_FontAwesome/fonts/FontAwesome.ttf")) {
|
|
|
|
f
|
|
|
|
} else {
|
|
|
|
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not create FontAwesome.ttf")));
|
2016-01-03 20:02:04 +08:00
|
|
|
};
|
2015-09-14 02:03:34 +08:00
|
|
|
try!(font_awesome.write_all(theme::FONT_AWESOME_TTF));
|
|
|
|
|
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-03-18 05:31:28 +08:00
|
|
|
fn make_data(book: &MDBook) -> Result<BTreeMap<String, Json>, Box<Error>> {
|
2015-08-04 21:15:36 +08:00
|
|
|
debug!("[fn]: make_data");
|
|
|
|
|
2016-03-18 05:31:28 +08:00
|
|
|
let mut data = BTreeMap::new();
|
2015-09-17 10:46:23 +08:00
|
|
|
data.insert("language".to_owned(), "en".to_json());
|
|
|
|
data.insert("title".to_owned(), book.get_title().to_json());
|
2016-02-25 21:32:49 +08:00
|
|
|
data.insert("description".to_owned(), book.get_description().to_json());
|
2016-02-23 00:17:07 +08:00
|
|
|
data.insert("favicon".to_owned(), "favicon.png".to_json());
|
2016-04-02 10:46:05 +08:00
|
|
|
if let Some(livereload) = book.get_livereload() {
|
|
|
|
data.insert("livereload".to_owned(), livereload.to_json());
|
|
|
|
}
|
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) => {
|
|
|
|
chapter.insert("name".to_owned(), ch.name.to_json());
|
2015-09-12 02:52:55 +08:00
|
|
|
match ch.path.to_str() {
|
2016-03-18 05:31:28 +08:00
|
|
|
Some(p) => {
|
|
|
|
chapter.insert("path".to_owned(), p.to_json());
|
|
|
|
},
|
2015-09-12 02:52:55 +08:00
|
|
|
None => return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))),
|
|
|
|
}
|
|
|
|
},
|
2015-09-17 10:46:23 +08:00
|
|
|
BookItem::Chapter(ref s, ref ch) => {
|
|
|
|
chapter.insert("section".to_owned(), s.to_json());
|
|
|
|
chapter.insert("name".to_owned(), ch.name.to_json());
|
2015-09-12 02:52:55 +08:00
|
|
|
match ch.path.to_str() {
|
2016-03-18 05:31:28 +08:00
|
|
|
Some(p) => {
|
|
|
|
chapter.insert("path".to_owned(), p.to_json());
|
|
|
|
},
|
2015-09-12 02:52:55 +08:00
|
|
|
None => return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))),
|
|
|
|
}
|
|
|
|
},
|
2015-09-17 10:46:23 +08:00
|
|
|
BookItem::Spacer => {
|
|
|
|
chapter.insert("spacer".to_owned(), "_spacer_".to_json());
|
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);
|
|
|
|
}
|
|
|
|
|
2015-09-17 10:46:23 +08:00
|
|
|
data.insert("chapters".to_owned(), chapters.to_json());
|
2015-08-04 21:15:36 +08:00
|
|
|
|
|
|
|
debug!("[*]: JSON constructed");
|
|
|
|
Ok(data)
|
|
|
|
}
|