2015-07-19 06:08:38 +08:00
|
|
|
extern crate handlebars;
|
|
|
|
extern crate rustc_serialize;
|
2015-07-29 03:01:13 +08:00
|
|
|
extern crate pulldown_cmark;
|
2015-07-19 06:08:38 +08:00
|
|
|
|
2015-07-30 21:38:21 +08:00
|
|
|
mod hbs_toc_helper;
|
2015-08-01 00:34:43 +08:00
|
|
|
mod hbs_navigation_helper;
|
2015-07-30 21:38:21 +08:00
|
|
|
use self::hbs_toc_helper::RenderToc;
|
|
|
|
|
2015-07-19 06:08:38 +08:00
|
|
|
use renderer::Renderer;
|
2015-07-29 07:06:46 +08:00
|
|
|
use book::{BookItems, BookConfig};
|
2015-07-30 21:20:55 +08:00
|
|
|
use {theme, utils};
|
2015-07-19 06:08:38 +08:00
|
|
|
|
|
|
|
use std::path::{Path, PathBuf, Component};
|
|
|
|
use std::fs::{self, File, metadata};
|
|
|
|
use std::error::Error;
|
|
|
|
use std::io::{self, Read, Write};
|
2015-07-29 03:01:13 +08:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
2015-07-30 21:38:21 +08:00
|
|
|
use self::handlebars::{Handlebars, JsonRender};
|
|
|
|
use self::rustc_serialize::json::{Json, ToJson};
|
2015-07-30 21:20:55 +08:00
|
|
|
use self::pulldown_cmark::{Parser, html};
|
2015-07-19 06:08:38 +08:00
|
|
|
|
|
|
|
pub struct HtmlHandlebars;
|
|
|
|
|
|
|
|
impl Renderer for HtmlHandlebars {
|
|
|
|
fn render(&self, book: BookItems, config: &BookConfig) -> Result<(), Box<Error>> {
|
2015-08-04 00:06:01 +08:00
|
|
|
debug!("[fn]: render");
|
2015-07-19 06:08:38 +08:00
|
|
|
let mut handlebars = Handlebars::new();
|
|
|
|
|
|
|
|
// Load template
|
|
|
|
let t = theme::get_index_hbs();
|
|
|
|
|
|
|
|
// Register template
|
2015-08-04 00:06:01 +08:00
|
|
|
debug!("[*]: Register handlebars template");
|
2015-07-19 06:08:38 +08:00
|
|
|
try!(handlebars.register_template_string("index", t.to_owned()));
|
|
|
|
|
2015-08-01 00:34:43 +08:00
|
|
|
// Register helpers
|
2015-08-04 00:06:01 +08:00
|
|
|
debug!("[*]: Register handlebars helpers");
|
2015-07-29 06:57:47 +08:00
|
|
|
handlebars.register_helper("toc", Box::new(RenderToc));
|
2015-08-01 00:34:43 +08:00
|
|
|
handlebars.register_helper("previous", Box::new(hbs_navigation_helper::previous));
|
|
|
|
handlebars.register_helper("next", Box::new(hbs_navigation_helper::next));
|
2015-07-29 06:57:47 +08:00
|
|
|
|
2015-07-29 03:01:13 +08:00
|
|
|
let mut data = try!(make_data(book.clone(), config));
|
2015-07-19 06:08:38 +08:00
|
|
|
|
2015-08-03 20:23:32 +08:00
|
|
|
// Check if dest directory exists
|
2015-08-04 00:06:01 +08:00
|
|
|
debug!("[*]: Check if destination directory exists");
|
2015-08-03 20:23:32 +08:00
|
|
|
match utils::path::create_path(config.dest()) {
|
|
|
|
Err(_) => return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Unexcpected error when constructing destination path"))),
|
|
|
|
_ => {},
|
|
|
|
};
|
|
|
|
|
2015-07-29 03:01:13 +08:00
|
|
|
// Render a file for every entry in the book
|
2015-07-30 04:26:13 +08:00
|
|
|
let mut index = true;
|
2015-07-19 06:08:38 +08:00
|
|
|
for (_, item) in book {
|
|
|
|
|
|
|
|
if item.path != PathBuf::new() {
|
|
|
|
|
2015-07-29 03:01:13 +08:00
|
|
|
let path = config.src().join(&item.path);
|
2015-07-30 19:49:49 +08:00
|
|
|
|
2015-08-04 00:06:01 +08:00
|
|
|
debug!("[*]: Opening file: {:?}", path);
|
2015-07-29 03:01:13 +08:00
|
|
|
let mut f = try!(File::open(&path));
|
|
|
|
let mut content: String = String::new();
|
|
|
|
|
2015-08-04 00:06:01 +08:00
|
|
|
debug!("[*]: Reading file");
|
2015-07-29 03:01:13 +08:00
|
|
|
try!(f.read_to_string(&mut content));
|
|
|
|
|
2015-07-30 21:38:21 +08:00
|
|
|
// Render markdown using the pulldown-cmark crate
|
2015-07-29 03:01:13 +08:00
|
|
|
content = render_html(&content);
|
|
|
|
|
2015-07-31 21:06:08 +08:00
|
|
|
// 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());
|
|
|
|
|
2015-07-29 03:01:13 +08:00
|
|
|
// Remove content from previous file and render content for this one
|
|
|
|
data.remove("content");
|
|
|
|
data.insert("content".to_string(), content.to_json());
|
|
|
|
|
2015-07-30 05:32:01 +08:00
|
|
|
// Remove path to root from previous file and render content for this one
|
|
|
|
data.remove("path_to_root");
|
2015-07-30 21:20:55 +08:00
|
|
|
data.insert("path_to_root".to_string(), utils::path::path_to_root(&item.path).to_json());
|
2015-07-30 05:32:01 +08:00
|
|
|
|
2015-07-29 03:01:13 +08:00
|
|
|
// Rendere the handlebars template with the data
|
2015-08-04 00:06:01 +08:00
|
|
|
debug!("[*]: Render template");
|
2015-07-19 06:08:38 +08:00
|
|
|
let rendered = try!(handlebars.render("index", &data));
|
|
|
|
|
2015-08-04 00:06:01 +08:00
|
|
|
debug!("[*] Write to file");
|
2015-07-29 03:01:13 +08:00
|
|
|
// Write to file
|
2015-07-19 06:08:38 +08:00
|
|
|
let mut file = try!(create_file(config.dest(), &item.path));
|
|
|
|
try!(file.write_all(&rendered.into_bytes()));
|
2015-07-30 04:26:13 +08:00
|
|
|
|
|
|
|
// Create an index.html from the first element in SUMMARY.md
|
|
|
|
if index {
|
2015-08-04 00:06:01 +08:00
|
|
|
debug!("[*] index.html");
|
2015-07-30 04:26:13 +08:00
|
|
|
try!(fs::copy(
|
2015-07-30 19:49:49 +08:00
|
|
|
config.dest().join(&item.path.with_extension("html")),
|
2015-07-30 04:26:13 +08:00
|
|
|
config.dest().join("index.html")
|
|
|
|
));
|
2015-08-04 00:06:01 +08:00
|
|
|
|
|
|
|
println!(
|
|
|
|
"[*] Creating index.html from {:?} ✓",
|
|
|
|
config.dest().join(&item.path.with_extension("html"))
|
|
|
|
);
|
2015-07-30 04:26:13 +08:00
|
|
|
index = false;
|
|
|
|
}
|
2015-07-19 06:08:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 03:01:13 +08:00
|
|
|
// Copy static files (js, css, images, ...)
|
|
|
|
|
2015-08-04 00:06:01 +08:00
|
|
|
debug!("[*] Copy static files");
|
2015-07-29 03:01:13 +08:00
|
|
|
// JavaScript
|
|
|
|
let mut js_file = try!(File::create(config.dest().join("book.js")));
|
|
|
|
try!(js_file.write_all(theme::get_js()));
|
|
|
|
|
|
|
|
// Css
|
|
|
|
let mut css_file = try!(File::create(config.dest().join("book.css")));
|
|
|
|
try!(css_file.write_all(theme::get_css()));
|
|
|
|
|
2015-07-19 06:08:38 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HtmlHandlebars {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
HtmlHandlebars
|
|
|
|
}
|
|
|
|
|
2015-08-04 00:06:01 +08:00
|
|
|
/*fn _load_template(&self, path: &Path) -> Result<String, Box<Error>> {
|
2015-07-19 06:08:38 +08:00
|
|
|
let mut file = try!(File::open(path));
|
|
|
|
let mut s = String::new();
|
|
|
|
try!(file.read_to_string(&mut s));
|
|
|
|
Ok(s)
|
2015-08-04 00:06:01 +08:00
|
|
|
}*/
|
2015-07-19 06:08:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn create_file(working_directory: &Path, path: &Path) -> Result<File, Box<Error>> {
|
|
|
|
|
2015-08-04 00:06:01 +08:00
|
|
|
debug!("[fn]: create_file");
|
2015-08-03 20:23:32 +08:00
|
|
|
|
2015-08-04 00:06:01 +08:00
|
|
|
debug!("[*]: extract filename");
|
2015-07-19 06:08:38 +08:00
|
|
|
// Extract filename
|
|
|
|
let mut file_name;
|
|
|
|
if let Some(name) = path.file_stem() {
|
|
|
|
file_name = String::from(name.to_str().unwrap());
|
|
|
|
}
|
|
|
|
else { return Err(Box::new(io::Error::new(io::ErrorKind::Other, "No filename"))) }
|
|
|
|
|
|
|
|
file_name.push_str(".html");
|
|
|
|
|
|
|
|
// Delete filename from path
|
|
|
|
let mut path = path.to_path_buf();
|
|
|
|
path.pop();
|
|
|
|
|
|
|
|
// Create directories if they do not exist
|
|
|
|
let mut constructed_path = PathBuf::from(working_directory);
|
|
|
|
|
|
|
|
for component in path.components() {
|
|
|
|
|
|
|
|
let mut dir;
|
|
|
|
match component {
|
|
|
|
Component::Normal(_) => { dir = PathBuf::from(component.as_os_str()); },
|
|
|
|
_ => continue,
|
|
|
|
}
|
|
|
|
|
|
|
|
constructed_path.push(&dir);
|
|
|
|
|
|
|
|
// Check if path exists
|
|
|
|
match metadata(&constructed_path) {
|
|
|
|
// Any way to combine the Err and first Ok branch ??
|
|
|
|
Err(_) => {
|
2015-08-04 00:06:01 +08:00
|
|
|
debug!("[*]: Create {:?}", constructed_path);
|
2015-07-19 06:08:38 +08:00
|
|
|
try!(fs::create_dir(&constructed_path))
|
|
|
|
},
|
|
|
|
Ok(f) => {
|
|
|
|
if !f.is_dir() {
|
2015-08-04 00:06:01 +08:00
|
|
|
debug!("[*]: Create {:?}", constructed_path);
|
2015-07-19 06:08:38 +08:00
|
|
|
try!(fs::create_dir(&constructed_path))
|
|
|
|
} else {
|
2015-08-04 00:06:01 +08:00
|
|
|
debug!("[*]: Directory exists: {:?}", constructed_path);
|
2015-07-19 06:08:38 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-08-04 00:06:01 +08:00
|
|
|
debug!("[*]: Create {:?}", constructed_path.join(&file_name));
|
2015-07-19 06:08:38 +08:00
|
|
|
let file = try!(File::create(
|
2015-07-30 19:49:49 +08:00
|
|
|
constructed_path.join(&file_name)
|
2015-07-19 06:08:38 +08:00
|
|
|
));
|
2015-08-04 00:06:01 +08:00
|
|
|
println!("[*] Create file: {:?} ✓", constructed_path.join(&file_name));
|
2015-07-19 06:08:38 +08:00
|
|
|
|
|
|
|
Ok(file)
|
|
|
|
}
|
2015-07-19 20:02:21 +08:00
|
|
|
|
|
|
|
|
2015-07-29 03:01:13 +08:00
|
|
|
fn make_data(book: BookItems, config: &BookConfig) -> Result<BTreeMap<String,Json>, Box<Error>> {
|
2015-08-04 00:06:01 +08:00
|
|
|
debug!("[fn]: make_data");
|
2015-07-19 20:02:21 +08:00
|
|
|
|
|
|
|
let mut data = BTreeMap::new();
|
|
|
|
data.insert("language".to_string(), "en".to_json());
|
2015-07-29 06:57:47 +08:00
|
|
|
data.insert("title".to_string(), config.title.to_json());
|
2015-07-19 20:02:21 +08:00
|
|
|
|
|
|
|
let mut chapters = vec![];
|
|
|
|
|
|
|
|
for (section, item) in book {
|
|
|
|
let mut chapter = BTreeMap::new();
|
|
|
|
chapter.insert("section".to_string(), section.to_json());
|
2015-07-29 06:57:47 +08:00
|
|
|
chapter.insert("name".to_string(), item.name.to_json());
|
|
|
|
chapter.insert("path".to_string(), item.path.to_str().unwrap().to_json());
|
2015-07-19 20:02:21 +08:00
|
|
|
|
|
|
|
chapters.push(chapter);
|
|
|
|
}
|
|
|
|
|
|
|
|
data.insert("chapters".to_string(), chapters.to_json());
|
|
|
|
|
2015-08-04 00:06:01 +08:00
|
|
|
debug!("[*]: JSON constructed");
|
2015-07-29 03:01:13 +08:00
|
|
|
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
|
2015-07-19 20:02:21 +08:00
|
|
|
}
|