Clean-up handlebars renderer, move some parts to utils module

This commit is contained in:
Mathieu David 2015-08-04 16:52:10 +02:00
parent bca6a7aa13
commit 1095e7c773
2 changed files with 22 additions and 76 deletions

View File

@ -19,6 +19,12 @@ use self::pulldown_cmark::{Parser, html};
pub struct HtmlHandlebars; pub struct HtmlHandlebars;
impl HtmlHandlebars {
pub fn new() -> Self {
HtmlHandlebars
}
}
impl Renderer for HtmlHandlebars { impl Renderer for HtmlHandlebars {
fn render(&self, book: BookItems, config: &BookConfig) -> Result<(), Box<Error>> { fn render(&self, book: BookItems, config: &BookConfig) -> Result<(), Box<Error>> {
debug!("[fn]: render"); debug!("[fn]: render");
@ -82,7 +88,7 @@ impl Renderer for HtmlHandlebars {
debug!("[*] Write to file"); debug!("[*] Write to file");
// Write to file // Write to file
let mut file = try!(create_file(config.dest(), &item.path)); let mut file = try!(utils::path::create_file(&config.dest().join(&item.path).with_extension("html")));
try!(file.write_all(&rendered.into_bytes())); try!(file.write_all(&rendered.into_bytes()));
// Create an index.html from the first element in SUMMARY.md // Create an index.html from the first element in SUMMARY.md
@ -117,79 +123,6 @@ impl Renderer for HtmlHandlebars {
} }
} }
impl HtmlHandlebars {
pub fn new() -> Self {
HtmlHandlebars
}
/*fn _load_template(&self, path: &Path) -> Result<String, Box<Error>> {
let mut file = try!(File::open(path));
let mut s = String::new();
try!(file.read_to_string(&mut s));
Ok(s)
}*/
}
fn create_file(working_directory: &Path, path: &Path) -> Result<File, Box<Error>> {
debug!("[fn]: create_file");
debug!("[*]: extract filename");
// 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(_) => {
debug!("[*]: Create {:?}", constructed_path);
try!(fs::create_dir(&constructed_path))
},
Ok(f) => {
if !f.is_dir() {
debug!("[*]: Create {:?}", constructed_path);
try!(fs::create_dir(&constructed_path))
} else {
debug!("[*]: Directory exists: {:?}", constructed_path);
continue
}
},
}
}
debug!("[*]: Create {:?}", constructed_path.join(&file_name));
let file = try!(File::create(
constructed_path.join(&file_name)
));
println!("[*] Create file: {:?}", constructed_path.join(&file_name));
Ok(file)
}
fn make_data(book: BookItems, config: &BookConfig) -> Result<BTreeMap<String,Json>, Box<Error>> { fn make_data(book: BookItems, config: &BookConfig) -> Result<BTreeMap<String,Json>, Box<Error>> {
debug!("[fn]: make_data"); debug!("[fn]: make_data");

View File

@ -1,6 +1,6 @@
use std::path::{Path, PathBuf, Component}; use std::path::{Path, PathBuf, Component};
use std::error::Error; use std::error::Error;
use std::fs::{self, metadata}; use std::fs::{self, metadata, File};
pub fn path_to_root(path: &Path) -> String { pub fn path_to_root(path: &Path) -> String {
debug!("[fn]: path_to_root"); debug!("[fn]: path_to_root");
@ -18,7 +18,6 @@ pub fn path_to_root(path: &Path) -> String {
}) })
} }
pub fn create_path(path: &Path) -> Result<(), Box<Error>> { pub fn create_path(path: &Path) -> Result<(), Box<Error>> {
debug!("[fn]: create_path"); debug!("[fn]: create_path");
@ -65,3 +64,17 @@ pub fn create_path(path: &Path) -> Result<(), Box<Error>> {
Ok(()) Ok(())
} }
pub fn create_file(path: &Path) -> Result<File, Box<Error>> {
debug!("[fn]: create_file");
// Construct path
if let Some(p) = path.parent() {
try!(create_path(p));
}
debug!("[*]: Create file: {}", path);
let f = try!(File::create(path));
Ok(f)
}