mdbook/src/book/mdbook.rs

139 lines
3.8 KiB
Rust
Raw Normal View History

2015-07-07 02:56:19 +02:00
use std::path::PathBuf;
use std::fs::{self, File, metadata};
use std::io::{Write, Result};
2015-07-07 02:56:19 +02:00
2015-07-16 19:26:16 +02:00
use book::bookconfig::BookConfig;
use book::bookitem::BookItem;
use parse;
2015-07-07 02:56:19 +02:00
pub struct MDBook {
2015-07-16 19:26:16 +02:00
title: String,
author: String,
config: BookConfig,
2015-07-16 19:26:16 +02:00
pub content: Vec<BookItem>
2015-07-07 02:56:19 +02:00
}
impl MDBook {
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-16 19:26:16 +02:00
title: String::from(""),
author: String::from(""),
content: vec![],
2015-07-16 18:20:36 +02:00
config: BookConfig::new()
.set_src(path.join("src"))
.set_dest(path.join("book")),
2015-07-07 02:56:19 +02:00
}
}
pub fn init(&self) -> Result<()> {
2015-07-07 02:56:19 +02:00
2015-07-16 18:20:36 +02:00
let dest = self.config.dest();
let src = 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(_) => {
// 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(_) => {
// 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
let summary = match metadata(&src.join("SUMMARY.md")) {
2015-07-07 02:56:19 +02:00
Err(_) => {
// There is a very high chance that the error is due to the fact that
// the directory / file does not exist
Ok(File::create(&src.join("SUMMARY.md")).unwrap())
},
Ok(_) => {
/* If there is no error, the directory / file does exist */
Err("SUMMARY.md does already exist")
}
};
if let Ok(mut f) = summary {
try!(writeln!(f, "# Summary"));
try!(writeln!(f, ""));
try!(writeln!(f, "[Chapter 1](./chapter_1.md)"));
let mut chapter_1 = File::create(&src.join("chapter_1.md")).unwrap();
try!(writeln!(chapter_1, "# Chapter 1"));
2015-07-07 02:56:19 +02:00
}
return Ok(());
}
pub fn build(&mut self) -> Result<()> {
try!(self.parse_summary());
Ok(())
}
2015-07-16 19:26:16 +02:00
// Builder functions
2015-07-07 02:56:19 +02:00
pub fn set_dest(mut self, dest: PathBuf) -> Self {
2015-07-16 18:20:36 +02:00
self.config = 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-16 18:20:36 +02:00
self.config = self.config.set_src(src);
2015-07-07 02:56:19 +02:00
self
}
2015-07-16 19:26:16 +02:00
pub fn set_title(mut self, title: String) -> Self {
self.title = title;
self
}
pub fn set_author(mut self, author: String) -> Self {
self.author = author;
self
}
// Construct book
fn parse_summary(&mut self) -> Result<()> {
// When append becomes stale, use self.content.append() ...
let book_items = try!(parse::construct_bookitems(&self.config.src().join("SUMMARY.md")));
for item in book_items {
self.content.push(item)
}
// Debug
for item in &self.content {
println!("name: \"{}\" path: {:?}", item.name, item.path);
}
Ok(())
}
2015-07-07 02:56:19 +02:00
}