2015-08-04 21:15:36 +08:00
|
|
|
extern crate handlebars;
|
|
|
|
extern crate rustc_serialize;
|
|
|
|
extern crate pulldown_cmark;
|
|
|
|
|
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};
|
2015-08-04 23:13:24 +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;
|
|
|
|
|
|
|
|
use self::handlebars::{Handlebars, JsonRender};
|
|
|
|
use self::rustc_serialize::json::{Json, ToJson};
|
|
|
|
use self::pulldown_cmark::{Parser, html};
|
|
|
|
|
|
|
|
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");
|
2015-08-31 21:24:42 +08:00
|
|
|
match utils::create_path(book.get_dest()) {
|
2015-08-04 21:15:36 +08:00
|
|
|
Err(_) => return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Unexcpected error when constructing destination path"))),
|
|
|
|
_ => {},
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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() {
|
|
|
|
|
|
|
|
match item {
|
|
|
|
&BookItem::Chapter(_, ref ch) | &BookItem::Affix(ref ch) => {
|
|
|
|
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));
|
|
|
|
|
|
|
|
// Render markdown using the pulldown-cmark crate
|
|
|
|
content = render_html(&content);
|
|
|
|
print_content.push_str(&content);
|
|
|
|
|
|
|
|
// Remove content from previous file and render content for this one
|
|
|
|
data.remove("path");
|
|
|
|
match ch.path.to_str() {
|
|
|
|
Some(p) => { data.insert("path".to_string(), p.to_json()); },
|
|
|
|
None => return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Remove content from previous file and render content for this one
|
|
|
|
data.remove("content");
|
|
|
|
data.insert("content".to_string(), content.to_json());
|
|
|
|
|
|
|
|
// Remove path to root from previous file and render content for this one
|
|
|
|
data.remove("path_to_root");
|
|
|
|
data.insert("path_to_root".to_string(), utils::path_to_root(&ch.path).to_json());
|
|
|
|
|
|
|
|
// 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
|
|
|
|
let mut file = try!(utils::create_file(&book.get_dest().join(&ch.path).with_extension("html")));
|
|
|
|
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");
|
|
|
|
try!(fs::copy(
|
|
|
|
book.get_dest().join(&ch.path.with_extension("html")),
|
|
|
|
book.get_dest().join("index.html")
|
|
|
|
));
|
|
|
|
|
|
|
|
output!(
|
|
|
|
"[*] Creating index.html from {:?} ✓",
|
|
|
|
book.get_dest().join(&ch.path.with_extension("html"))
|
|
|
|
);
|
|
|
|
index = false;
|
|
|
|
}
|
|
|
|
}
|
2015-08-04 21:15:36 +08:00
|
|
|
}
|
2015-09-12 02:52:55 +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");
|
|
|
|
data.insert("path".to_string(), "print.md".to_json());
|
|
|
|
|
|
|
|
// Remove content from previous file and render content for this one
|
|
|
|
data.remove("content");
|
|
|
|
data.insert("content".to_string(), print_content.to_json());
|
|
|
|
|
|
|
|
// Remove path to root from previous file and render content for this one
|
|
|
|
data.remove("path_to_root");
|
|
|
|
data.insert("path_to_root".to_string(), utils::path_to_root(Path::new("print.md")).to_json());
|
|
|
|
|
|
|
|
// Rendere the handlebars template with the data
|
|
|
|
debug!("[*]: Render template");
|
|
|
|
let rendered = try!(handlebars.render("index", &data));
|
|
|
|
let mut file = try!(utils::create_file(&book.get_dest().join("print").with_extension("html")));
|
|
|
|
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
|
2015-08-31 21:24:42 +08:00
|
|
|
let mut js_file = try!(File::create(book.get_dest().join("book.js")));
|
2015-08-04 23:58:09 +08:00
|
|
|
try!(js_file.write_all(&theme.js));
|
2015-08-04 21:15:36 +08:00
|
|
|
|
|
|
|
// Css
|
2015-08-31 21:24:42 +08:00
|
|
|
let mut css_file = try!(File::create(book.get_dest().join("book.css")));
|
2015-08-04 23:58:09 +08:00
|
|
|
try!(css_file.write_all(&theme.css));
|
2015-08-04 21:15:36 +08:00
|
|
|
|
2015-09-14 01:16:11 +08:00
|
|
|
// JQuery
|
|
|
|
let mut jquery = try!(File::create(book.get_dest().join("jquery.js")));
|
|
|
|
try!(jquery.write_all(&theme.jquery));
|
|
|
|
|
2015-08-06 00:28:59 +08:00
|
|
|
// syntax highlighting
|
2015-08-31 21:24:42 +08:00
|
|
|
let mut highlight_css = try!(File::create(book.get_dest().join("highlight.css")));
|
2015-08-06 00:28:59 +08:00
|
|
|
try!(highlight_css.write_all(&theme.highlight_css));
|
2015-08-31 21:24:42 +08:00
|
|
|
let mut highlight_js = try!(File::create(book.get_dest().join("highlight.js")));
|
2015-08-06 00:28:59 +08:00
|
|
|
try!(highlight_js.write_all(&theme.highlight_js));
|
|
|
|
|
2015-08-04 21:15:36 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-31 21:24:42 +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");
|
|
|
|
|
|
|
|
let mut data = BTreeMap::new();
|
|
|
|
data.insert("language".to_string(), "en".to_json());
|
2015-08-31 21:24:42 +08:00
|
|
|
data.insert("title".to_string(), book.get_title().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
|
|
|
|
|
|
|
match item {
|
|
|
|
&BookItem::Affix(ref ch) => {
|
|
|
|
chapter.insert("name".to_string(), ch.name.to_json());
|
|
|
|
match ch.path.to_str() {
|
|
|
|
Some(p) => { chapter.insert("path".to_string(), p.to_json()); },
|
|
|
|
None => return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
&BookItem::Chapter(ref s, ref ch) => {
|
|
|
|
chapter.insert("section".to_string(), s.to_json());
|
|
|
|
chapter.insert("name".to_string(), ch.name.to_json());
|
|
|
|
match ch.path.to_str() {
|
|
|
|
Some(p) => { chapter.insert("path".to_string(), p.to_json()); },
|
|
|
|
None => return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
&BookItem::Spacer => {
|
|
|
|
chapter.insert("spacer".to_string(), "_spacer_".to_json());
|
|
|
|
}
|
|
|
|
|
2015-08-12 04:55:51 +08:00
|
|
|
}
|
2015-08-04 21:15:36 +08:00
|
|
|
|
|
|
|
chapters.push(chapter);
|
|
|
|
}
|
|
|
|
|
|
|
|
data.insert("chapters".to_string(), chapters.to_json());
|
|
|
|
|
|
|
|
debug!("[*]: JSON constructed");
|
|
|
|
Ok(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_html(text: &str) -> String {
|
|
|
|
let mut s = String::with_capacity(text.len() * 3 / 2);
|
|
|
|
let p = Parser::new(&text);
|
|
|
|
html::push_html(&mut s, p);
|
|
|
|
s
|
|
|
|
}
|