Merge pull request #45 from FuGangqiang/fgq
fix #8: Init -> create files in summary.md
This commit is contained in:
commit
99942cd46d
|
@ -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());
|
||||||
|
|
|
@ -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,40 +92,53 @@ impl MDBook {
|
||||||
output!("{:?} created", self.config.get_root());
|
output!("{:?} created", self.config.get_root());
|
||||||
}
|
}
|
||||||
|
|
||||||
let dest = self.config.get_dest();
|
{
|
||||||
let src = self.config.get_src();
|
let dest = self.config.get_dest();
|
||||||
|
let src = self.config.get_src();
|
||||||
|
|
||||||
if !dest.exists() {
|
if !dest.exists() {
|
||||||
debug!("[*]: {:?} does not exist, trying to create directory", dest);
|
debug!("[*]: {:?} does not exist, trying to create directory", dest);
|
||||||
try!(fs::create_dir(&dest));
|
try!(fs::create_dir(&dest));
|
||||||
|
}
|
||||||
|
|
||||||
|
if !src.exists() {
|
||||||
|
debug!("[*]: {:?} does not exist, trying to create directory", src);
|
||||||
|
try!(fs::create_dir(&src));
|
||||||
|
}
|
||||||
|
|
||||||
|
let summary = src.join("SUMMARY.md");
|
||||||
|
|
||||||
|
if !summary.exists() {
|
||||||
|
|
||||||
|
// 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")));
|
||||||
|
|
||||||
|
debug!("[*]: Writing to SUMMARY.md");
|
||||||
|
|
||||||
|
try!(writeln!(f, "# Summary"));
|
||||||
|
try!(writeln!(f, ""));
|
||||||
|
try!(writeln!(f, "- [Chapter 1](./chapter_1.md)"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !src.exists() {
|
// parse SUMMARY.md, and create the missing item related file
|
||||||
debug!("[*]: {:?} does not exist, trying to create directory", src);
|
try!(self.parse_summary());
|
||||||
try!(fs::create_dir(&src));
|
|
||||||
}
|
|
||||||
|
|
||||||
let summary = src.join("SUMMARY.md");
|
for (_, item) in self.iter() {
|
||||||
|
if item.path != PathBuf::new() {
|
||||||
|
let path = self.config.get_src().join(&item.path);
|
||||||
|
|
||||||
if !summary.exists() {
|
if !path.exists() {
|
||||||
|
debug!("[*]: {:?} does not exist, trying to create file", path);
|
||||||
// Summary does not exist, create it and populate it
|
::std::fs::create_dir_all(path.parent().unwrap());
|
||||||
|
let mut f = try!(File::create(path));
|
||||||
debug!("[*]: {:?} does not exist, trying to create SUMMARY.md", src.join("SUMMARY.md"));
|
|
||||||
let mut f = try!(File::create(&src.join("SUMMARY.md")));
|
|
||||||
|
|
||||||
debug!("[*]: Writing to SUMMARY.md");
|
|
||||||
|
|
||||||
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 {
|
|
||||||
|
|
||||||
// Summary does exist, read it and create the missing files
|
|
||||||
|
|
||||||
|
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()));
|
||||||
|
|
Loading…
Reference in New Issue