fix #8: Init -> create files in summary.md

This commit is contained in:
FuGangqiang 2015-08-30 00:51:23 +08:00
parent a5aa357f57
commit 77b9882825
2 changed files with 45 additions and 32 deletions

View File

@ -61,7 +61,7 @@ fn confirm() -> bool {
fn init(args: &ArgMatches) -> Result<(), Box<Error>> {
let book_dir = get_book_dir(args);
let book = MDBook::new(&book_dir);
let mut book = MDBook::new(&book_dir);
// Call the function that does the initialization
try!(book.init());

View File

@ -1,4 +1,4 @@
use std::path::Path;
use std::path::{Path, PathBuf};
use std::fs::{self, File};
use std::io::Write;
use std::error::Error;
@ -83,7 +83,7 @@ impl MDBook {
/// It uses the paths given as source and output directories and adds a `SUMMARY.md` and a
/// `chapter_1.md` to the source directory.
pub fn init(&self) -> Result<(), Box<Error>> {
pub fn init(&mut self) -> Result<(), Box<Error>> {
debug!("[fn]: init");
@ -92,6 +92,7 @@ impl MDBook {
output!("{:?} created", self.config.get_root());
}
{
let dest = self.config.get_dest();
let src = self.config.get_src();
@ -109,7 +110,7 @@ impl MDBook {
if !summary.exists() {
// Summary does not exist, create it and populate it
// Summary does not exist, create it
debug!("[*]: {:?} does not exist, trying to create SUMMARY.md", src.join("SUMMARY.md"));
let mut f = try!(File::create(&src.join("SUMMARY.md")));
@ -119,13 +120,25 @@ impl MDBook {
try!(writeln!(f, "# Summary"));
try!(writeln!(f, ""));
try!(writeln!(f, "- [Chapter 1](./chapter_1.md)"));
}
}
let mut chapter_1 = try!(File::create(&src.join("chapter_1.md")));
try!(writeln!(chapter_1, "# Chapter 1"));
} else {
// parse SUMMARY.md, and create the missing item related file
try!(self.parse_summary());
// Summary does exist, read it and create the missing files
for (_, item) in self.iter() {
if item.path != PathBuf::new() {
let path = self.config.get_src().join(&item.path);
if !path.exists() {
debug!("[*]: {:?} does not exist, trying to create file", path);
::std::fs::create_dir_all(path.parent().unwrap());
let mut f = try!(File::create(path));
debug!("[*]: Writing to {:?}", path);
try!(writeln!(f, "# {}", item.name));
}
}
}
return Ok(());
@ -140,7 +153,7 @@ impl MDBook {
pub fn build(&mut self) -> Result<(), Box<Error>> {
debug!("[fn]: build");
try!(self.parse_summary());
self.init();
try!(self.renderer.render(
self.iter(),