diff --git a/src/book/mdbook.rs b/src/book/mdbook.rs index 618c211e..a8928396 100644 --- a/src/book/mdbook.rs +++ b/src/book/mdbook.rs @@ -3,7 +3,7 @@ use std::fs::{self, File}; use std::io::Write; use std::error::Error; -use {BookConfig, BookItem, theme, parse}; +use {BookConfig, BookItem, theme, parse, utils}; use book::BookItems; use renderer::{Renderer, HtmlHandlebars}; use utils::{PathExt, create_path}; @@ -142,6 +142,9 @@ impl MDBook { try!(self.parse_summary()); + // Clean output directory + try!(utils::remove_dir_content(&self.config.get_dest())); + try!(self.renderer.render( self.iter(), &self.config, diff --git a/src/utils/mod.rs b/src/utils/mod.rs index a4d64a8a..bd9727d6 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -118,3 +118,15 @@ pub fn create_file(path: &Path) -> Result> { Ok(f) } + +/// Removes all the content of a directory but not the directory itself + +pub fn remove_dir_content(dir: &Path) -> Result<(), Box> { + for item in try!(fs::read_dir(dir)) { + if let Ok(item) = item { + let item = item.path(); + if item.is_dir() { try!(fs::remove_dir_all(item)); } else { try!(fs::remove_file(item)); } + } + } + Ok(()) +}