Merge pull request #45 from FuGangqiang/fgq

fix #8: Init -> create files in summary.md
This commit is contained in:
Mathieu David 2015-08-30 08:43:24 +02:00
commit 99942cd46d
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>> { fn init(args: &ArgMatches) -> Result<(), Box<Error>> {
let book_dir = get_book_dir(args); 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 // Call the function that does the initialization
try!(book.init()); 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::fs::{self, File};
use std::io::Write; use std::io::Write;
use std::error::Error; 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 /// It uses the paths given as source and output directories and adds a `SUMMARY.md` and a
/// `chapter_1.md` to the source directory. /// `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"); debug!("[fn]: init");
@ -92,6 +92,7 @@ impl MDBook {
output!("{:?} created", self.config.get_root()); output!("{:?} created", self.config.get_root());
} }
{
let dest = self.config.get_dest(); let dest = self.config.get_dest();
let src = self.config.get_src(); let src = self.config.get_src();
@ -109,7 +110,7 @@ impl MDBook {
if !summary.exists() { 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")); debug!("[*]: {:?} does not exist, trying to create SUMMARY.md", src.join("SUMMARY.md"));
let mut f = try!(File::create(&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, "# Summary"));
try!(writeln!(f, "")); try!(writeln!(f, ""));
try!(writeln!(f, "- [Chapter 1](./chapter_1.md)")); try!(writeln!(f, "- [Chapter 1](./chapter_1.md)"));
}
}
let mut chapter_1 = try!(File::create(&src.join("chapter_1.md"))); // parse SUMMARY.md, and create the missing item related file
try!(writeln!(chapter_1, "# Chapter 1")); try!(self.parse_summary());
} else {
// 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(()); return Ok(());
@ -140,7 +153,7 @@ impl MDBook {
pub fn build(&mut self) -> Result<(), Box<Error>> { pub fn build(&mut self) -> Result<(), Box<Error>> {
debug!("[fn]: build"); debug!("[fn]: build");
try!(self.parse_summary()); self.init();
// Clean output directory // Clean output directory
try!(utils::remove_dir_content(&self.config.get_dest())); try!(utils::remove_dir_content(&self.config.get_dest()));