From 1095e7c7735260c062f2497528f83574a23f8425 Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Tue, 4 Aug 2015 16:52:10 +0200 Subject: [PATCH] Clean-up handlebars renderer, move some parts to utils module --- src/renderer/html_handlebars/hbs_renderer.rs | 81 ++------------------ src/utils/path.rs | 17 +++- 2 files changed, 22 insertions(+), 76 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 32b2ddc6..1a1a5f70 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -19,6 +19,12 @@ use self::pulldown_cmark::{Parser, html}; pub struct HtmlHandlebars; +impl HtmlHandlebars { + pub fn new() -> Self { + HtmlHandlebars + } +} + impl Renderer for HtmlHandlebars { fn render(&self, book: BookItems, config: &BookConfig) -> Result<(), Box> { debug!("[fn]: render"); @@ -82,7 +88,7 @@ impl Renderer for HtmlHandlebars { debug!("[*] 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())); // 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> { - 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> { - - 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, Box> { debug!("[fn]: make_data"); diff --git a/src/utils/path.rs b/src/utils/path.rs index 3ca98f78..4c815929 100644 --- a/src/utils/path.rs +++ b/src/utils/path.rs @@ -1,6 +1,6 @@ use std::path::{Path, PathBuf, Component}; use std::error::Error; -use std::fs::{self, metadata}; +use std::fs::{self, metadata, File}; pub fn path_to_root(path: &Path) -> String { 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> { debug!("[fn]: create_path"); @@ -65,3 +64,17 @@ pub fn create_path(path: &Path) -> Result<(), Box> { Ok(()) } + +pub fn create_file(path: &Path) -> Result> { + 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) +}