2015-07-07 02:56:19 +02:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use std::fs::{self, File, metadata};
|
2015-07-07 11:46:49 +02:00
|
|
|
use std::io::Write;
|
2015-07-07 12:36:11 +02:00
|
|
|
use std::io::{Error, ErrorKind};
|
2015-07-07 02:56:19 +02:00
|
|
|
|
2015-07-08 15:17:11 +02:00
|
|
|
use bookconfig::BookConfig;
|
|
|
|
|
|
2015-07-07 02:56:19 +02:00
|
|
|
pub struct MDBook {
|
2015-07-08 15:17:11 +02:00
|
|
|
path: PathBuf,
|
|
|
|
|
config: BookConfig,
|
2015-07-07 02:56:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MDBook {
|
2015-07-07 12:36:11 +02:00
|
|
|
|
2015-07-08 15:17:11 +02:00
|
|
|
pub fn new(path: &PathBuf) -> Self {
|
|
|
|
|
|
|
|
|
|
// Hacky way to check if the path exists... Until PathExt moves to stable
|
|
|
|
|
match metadata(path) {
|
|
|
|
|
Err(_) => panic!("Directory does not exist"),
|
|
|
|
|
Ok(f) => {
|
|
|
|
|
if !f.is_dir() {
|
|
|
|
|
panic!("Is not a directory");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-07 02:56:19 +02:00
|
|
|
MDBook {
|
2015-07-08 15:17:11 +02:00
|
|
|
path: path.to_owned(),
|
|
|
|
|
config: BookConfig::new(),
|
2015-07-07 02:56:19 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-08 15:17:11 +02:00
|
|
|
pub fn init(&self) -> Result<(), Error> {
|
2015-07-07 02:56:19 +02:00
|
|
|
|
2015-07-08 00:04:28 +02:00
|
|
|
// Logic problem: When self.dest is absolute, the directory given
|
|
|
|
|
// as parameter is never used...
|
2015-07-08 15:17:11 +02:00
|
|
|
let dest = self.path.join(&self.config.dest());
|
2015-07-07 02:56:19 +02:00
|
|
|
|
2015-07-08 15:17:11 +02:00
|
|
|
let src = self.path.join(&self.config.src());
|
2015-07-07 02:56:19 +02:00
|
|
|
|
|
|
|
|
// Hacky way to check if the directory exists... Until PathExt moves to stable
|
|
|
|
|
match metadata(&dest) {
|
|
|
|
|
Err(_) => {
|
2015-07-07 11:46:49 +02:00
|
|
|
// There is a very high chance that the error is due to the fact that
|
|
|
|
|
// the directory / file does not exist
|
|
|
|
|
fs::create_dir(&dest).unwrap();
|
|
|
|
|
},
|
2015-07-07 02:56:19 +02:00
|
|
|
Ok(_) => { /* If there is no error, the directory / file does exist */ }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hacky way to check if the directory exists... Until PathExt moves to stable
|
|
|
|
|
match metadata(&src) {
|
|
|
|
|
Err(_) => {
|
2015-07-07 11:46:49 +02:00
|
|
|
// There is a very high chance that the error is due to the fact that
|
|
|
|
|
// the directory / file does not exist
|
|
|
|
|
fs::create_dir(&src).unwrap();
|
|
|
|
|
},
|
2015-07-07 02:56:19 +02:00
|
|
|
Ok(_) => { /* If there is no error, the directory / file does exist */ }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hacky way to check if the directory exists... Until PathExt moves to stable
|
2015-07-07 11:46:49 +02:00
|
|
|
let summary = match metadata(&src.join("SUMMARY.md")) {
|
2015-07-07 02:56:19 +02:00
|
|
|
Err(_) => {
|
2015-07-07 11:46:49 +02:00
|
|
|
// There is a very high chance that the error is due to the fact that
|
|
|
|
|
// the directory / file does not exist
|
|
|
|
|
Result::Ok(File::create(&src.join("SUMMARY.md")).unwrap())
|
|
|
|
|
},
|
|
|
|
|
Ok(_) => {
|
|
|
|
|
/* If there is no error, the directory / file does exist */
|
|
|
|
|
Result::Err("SUMMARY.md does already exist")
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if let Ok(mut f) = summary {
|
2015-07-07 12:36:11 +02:00
|
|
|
try!(writeln!(f, "# Summary"));
|
|
|
|
|
try!(writeln!(f, ""));
|
|
|
|
|
try!(writeln!(f, "[Chapter 1](./chapter_1.md)"));
|
2015-07-07 11:46:49 +02:00
|
|
|
|
|
|
|
|
let mut chapter_1 = File::create(&src.join("chapter_1.md")).unwrap();
|
2015-07-07 12:36:11 +02:00
|
|
|
try!(writeln!(chapter_1, "# Chapter 1"));
|
2015-07-07 02:56:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-07 12:36:11 +02:00
|
|
|
pub fn build(&self, dir: &PathBuf) -> Result<(), Error> {
|
|
|
|
|
|
2015-07-08 15:17:11 +02:00
|
|
|
|
|
|
|
|
|
2015-07-07 12:36:11 +02:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-07 02:56:19 +02:00
|
|
|
pub fn set_dest(mut self, dest: PathBuf) -> Self {
|
2015-07-08 15:17:11 +02:00
|
|
|
self.config.set_dest(dest);
|
2015-07-07 02:56:19 +02:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_src(mut self, src: PathBuf) -> Self {
|
2015-07-08 15:17:11 +02:00
|
|
|
self.config.set_src(src);
|
2015-07-07 02:56:19 +02:00
|
|
|
self
|
|
|
|
|
}
|
2015-07-07 12:36:11 +02:00
|
|
|
|
2015-07-07 02:56:19 +02:00
|
|
|
}
|