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;
|
|
|
|
use book::{BookItems, BookConfig};
|
2015-08-06 04:35:26 +08:00
|
|
|
use {utils, theme};
|
2015-08-04 21:15:36 +08:00
|
|
|
|
2015-08-04 23:13:24 +08:00
|
|
|
use std::path::PathBuf;
|
|
|
|
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 {
|
|
|
|
fn render(&self, book: BookItems, config: &BookConfig) -> Result<(), Box<Error>> {
|
|
|
|
debug!("[fn]: render");
|
|
|
|
let mut handlebars = Handlebars::new();
|
|
|
|
|
2015-08-04 23:58:09 +08:00
|
|
|
// Load theme
|
|
|
|
let theme = theme::Theme::new(&config.src());
|
2015-08-04 21:15:36 +08:00
|
|
|
|
|
|
|
// Register template
|
|
|
|
debug!("[*]: Register handlebars template");
|
2015-08-04 23:58:09 +08:00
|
|
|
try!(handlebars.register_template_string("index", theme.index.to_owned()));
|
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));
|
|
|
|
|
|
|
|
let mut data = try!(make_data(book.clone(), config));
|
|
|
|
|
|
|
|
// Check if dest directory exists
|
|
|
|
debug!("[*]: Check if destination directory exists");
|
2015-08-07 05:04:19 +08:00
|
|
|
match utils::create_path(config.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;
|
|
|
|
for (_, item) in book {
|
|
|
|
|
|
|
|
if item.path != PathBuf::new() {
|
|
|
|
|
|
|
|
let path = config.src().join(&item.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);
|
|
|
|
|
|
|
|
// Remove content from previous file and render content for this one
|
|
|
|
data.remove("path");
|
|
|
|
data.insert("path".to_string(), item.path.to_str().unwrap().to_json());
|
|
|
|
|
|
|
|
// 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");
|
2015-08-07 05:04:19 +08:00
|
|
|
data.insert("path_to_root".to_string(), utils::path_to_root(&item.path).to_json());
|
2015-08-04 21:15:36 +08:00
|
|
|
|
|
|
|
// Rendere the handlebars template with the data
|
|
|
|
debug!("[*]: Render template");
|
|
|
|
let rendered = try!(handlebars.render("index", &data));
|
|
|
|
|
2015-08-04 23:13:24 +08:00
|
|
|
debug!("[*]: Create file {:?}", &config.dest().join(&item.path).with_extension("html"));
|
2015-08-04 21:15:36 +08:00
|
|
|
// Write to file
|
2015-08-07 05:04:19 +08:00
|
|
|
let mut file = try!(utils::create_file(&config.dest().join(&item.path).with_extension("html")));
|
2015-08-04 23:13:24 +08:00
|
|
|
output!("[*] Creating {:?} ✓", &config.dest().join(&item.path).with_extension("html"));
|
|
|
|
|
2015-08-04 21:15:36 +08:00
|
|
|
try!(file.write_all(&rendered.into_bytes()));
|
|
|
|
|
|
|
|
// Create an index.html from the first element in SUMMARY.md
|
|
|
|
if index {
|
2015-08-04 23:13:24 +08:00
|
|
|
debug!("[*]: index.html");
|
2015-08-04 21:15:36 +08:00
|
|
|
try!(fs::copy(
|
|
|
|
config.dest().join(&item.path.with_extension("html")),
|
|
|
|
config.dest().join("index.html")
|
|
|
|
));
|
|
|
|
|
2015-08-04 23:13:24 +08:00
|
|
|
output!(
|
2015-08-04 21:15:36 +08:00
|
|
|
"[*] Creating index.html from {:?} ✓",
|
|
|
|
config.dest().join(&item.path.with_extension("html"))
|
|
|
|
);
|
|
|
|
index = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy static files (js, css, images, ...)
|
|
|
|
|
|
|
|
debug!("[*] Copy static files");
|
|
|
|
// JavaScript
|
|
|
|
let mut js_file = try!(File::create(config.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
|
|
|
|
let mut css_file = try!(File::create(config.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-08-06 00:28:59 +08:00
|
|
|
// syntax highlighting
|
|
|
|
let mut highlight_css = try!(File::create(config.dest().join("highlight.css")));
|
|
|
|
try!(highlight_css.write_all(&theme.highlight_css));
|
|
|
|
let mut highlight_js = try!(File::create(config.dest().join("highlight.js")));
|
|
|
|
try!(highlight_js.write_all(&theme.highlight_js));
|
|
|
|
|
2015-08-04 21:15:36 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_data(book: BookItems, config: &BookConfig) -> Result<BTreeMap<String,Json>, Box<Error>> {
|
|
|
|
debug!("[fn]: make_data");
|
|
|
|
|
|
|
|
let mut data = BTreeMap::new();
|
|
|
|
data.insert("language".to_string(), "en".to_json());
|
|
|
|
data.insert("title".to_string(), config.title.to_json());
|
|
|
|
|
|
|
|
let mut chapters = vec![];
|
|
|
|
|
|
|
|
for (section, item) in book {
|
|
|
|
let mut chapter = BTreeMap::new();
|
|
|
|
chapter.insert("section".to_string(), section.to_json());
|
|
|
|
chapter.insert("name".to_string(), item.name.to_json());
|
|
|
|
chapter.insert("path".to_string(), item.path.to_str().unwrap().to_json());
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|