From c6e81337fb9f6c965713e6c4e719ee7c5adea101 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Mon, 17 Apr 2017 21:53:27 -0400 Subject: [PATCH 1/4] Implement new 'no-create' build flag. Fixes https://github.com/azerupi/mdBook/issues/253. --- src/bin/mdbook.rs | 5 +++++ src/book/mod.rs | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/src/bin/mdbook.rs b/src/bin/mdbook.rs index 09d5421a..3af3ada4 100644 --- a/src/bin/mdbook.rs +++ b/src/bin/mdbook.rs @@ -63,6 +63,7 @@ fn main() { .about("Build the book from the markdown files") .arg_from_usage("-o, --open 'Open the compiled book in a web browser'") .arg_from_usage("-d, --dest-dir=[dest-dir] 'The output directory for your book{n}(Defaults to ./book when omitted)'") + .arg_from_usage("--no-create 'Will not create non-existent files linked from SUMMARY.md'") .arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'")) .subcommand(SubCommand::with_name("watch") .about("Watch the files for changes") @@ -174,6 +175,10 @@ fn build(args: &ArgMatches) -> Result<(), Box> { None => book }; + if args.is_present("no-create") { + book.create_missing = false; + } + try!(book.build()); if args.is_present("open") { diff --git a/src/book/mod.rs b/src/book/mod.rs index 210436fe..2b56160e 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -32,6 +32,10 @@ pub struct MDBook { renderer: Box, livereload: Option, + + /// Should `mdbook build` create files referenced from SUMMARY.md if they + /// don't exist + pub create_missing: bool, } impl MDBook { @@ -79,6 +83,7 @@ impl MDBook { renderer: Box::new(HtmlHandlebars::new()), livereload: None, + create_missing: true, } } @@ -183,6 +188,9 @@ impl MDBook { let path = self.src.join(&ch.path); if !path.exists() { + if !self.create_missing { + return Err(format!("'{}' referenced from SUMMARY.md does not exist.", path.to_string_lossy()).into()); + } debug!("[*]: {:?} does not exist, trying to create file", path); try!(::std::fs::create_dir_all(path.parent().unwrap())); let mut f = try!(File::create(path)); From 15dcca87d86c845d4b9b308b5e254960ad91b43d Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Mon, 17 Apr 2017 21:55:32 -0400 Subject: [PATCH 2/4] Refactor to prevent excessive indentation. --- src/book/mod.rs | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index 2b56160e..e4622be7 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -180,26 +180,25 @@ impl MDBook { debug!("[*]: constructing paths for missing files"); for item in self.iter() { debug!("[*]: item: {:?}", item); - match *item { + let ch = match *item { BookItem::Spacer => continue, BookItem::Chapter(_, ref ch) | - BookItem::Affix(ref ch) => { - if ch.path != PathBuf::new() { - let path = self.src.join(&ch.path); + BookItem::Affix(ref ch) => ch, + }; + if ch.path != PathBuf::new() { + let path = self.src.join(&ch.path); - if !path.exists() { - if !self.create_missing { - return Err(format!("'{}' referenced from SUMMARY.md does not exist.", path.to_string_lossy()).into()); - } - debug!("[*]: {:?} does not exist, trying to create file", path); - try!(::std::fs::create_dir_all(path.parent().unwrap())); - let mut f = try!(File::create(path)); - - // debug!("[*]: Writing to {:?}", path); - try!(writeln!(f, "# {}", ch.name)); - } + if !path.exists() { + if !self.create_missing { + return Err(format!("'{}' referenced from SUMMARY.md does not exist.", path.to_string_lossy()).into()); } - }, + debug!("[*]: {:?} does not exist, trying to create file", path); + try!(::std::fs::create_dir_all(path.parent().unwrap())); + let mut f = try!(File::create(path)); + + // debug!("[*]: Writing to {:?}", path); + try!(writeln!(f, "# {}", ch.name)); + } } } From 5d72d966adc2ab6958a7a1afd9896c062c4266a8 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Mon, 17 Apr 2017 21:56:01 -0400 Subject: [PATCH 3/4] Wrap long line. --- src/book/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index e4622be7..a040fe19 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -190,7 +190,9 @@ impl MDBook { if !path.exists() { if !self.create_missing { - return Err(format!("'{}' referenced from SUMMARY.md does not exist.", path.to_string_lossy()).into()); + return Err(format!( + "'{}' referenced from SUMMARY.md does not exist.", + path.to_string_lossy()).into()); } debug!("[*]: {:?} does not exist, trying to create file", path); try!(::std::fs::create_dir_all(path.parent().unwrap())); From 4525810737b678e00c23f26c218db5426df0d714 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Mon, 17 Apr 2017 21:58:34 -0400 Subject: [PATCH 4/4] Rewrite an emptiness check. --- src/book/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index a040fe19..d2c5ae4e 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -185,7 +185,7 @@ impl MDBook { BookItem::Chapter(_, ref ch) | BookItem::Affix(ref ch) => ch, }; - if ch.path != PathBuf::new() { + if ch.path.as_os_str().is_empty() { let path = self.src.join(&ch.path); if !path.exists() {