Merge pull request #254 from frewsxcv/frewsxcv-no-create
Implement new 'no-create' build flag.
This commit is contained in:
commit
a4a708bdda
|
@ -63,6 +63,7 @@ fn main() {
|
||||||
.about("Build the book from the markdown files")
|
.about("Build the book from the markdown files")
|
||||||
.arg_from_usage("-o, --open 'Open the compiled book in a web browser'")
|
.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("-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)'"))
|
.arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'"))
|
||||||
.subcommand(SubCommand::with_name("watch")
|
.subcommand(SubCommand::with_name("watch")
|
||||||
.about("Watch the files for changes")
|
.about("Watch the files for changes")
|
||||||
|
@ -174,6 +175,10 @@ fn build(args: &ArgMatches) -> Result<(), Box<Error>> {
|
||||||
None => book
|
None => book
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if args.is_present("no-create") {
|
||||||
|
book.create_missing = false;
|
||||||
|
}
|
||||||
|
|
||||||
try!(book.build());
|
try!(book.build());
|
||||||
|
|
||||||
if args.is_present("open") {
|
if args.is_present("open") {
|
||||||
|
|
|
@ -32,6 +32,10 @@ pub struct MDBook {
|
||||||
renderer: Box<Renderer>,
|
renderer: Box<Renderer>,
|
||||||
|
|
||||||
livereload: Option<String>,
|
livereload: Option<String>,
|
||||||
|
|
||||||
|
/// Should `mdbook build` create files referenced from SUMMARY.md if they
|
||||||
|
/// don't exist
|
||||||
|
pub create_missing: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MDBook {
|
impl MDBook {
|
||||||
|
@ -79,6 +83,7 @@ impl MDBook {
|
||||||
renderer: Box::new(HtmlHandlebars::new()),
|
renderer: Box::new(HtmlHandlebars::new()),
|
||||||
|
|
||||||
livereload: None,
|
livereload: None,
|
||||||
|
create_missing: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,14 +180,20 @@ impl MDBook {
|
||||||
debug!("[*]: constructing paths for missing files");
|
debug!("[*]: constructing paths for missing files");
|
||||||
for item in self.iter() {
|
for item in self.iter() {
|
||||||
debug!("[*]: item: {:?}", item);
|
debug!("[*]: item: {:?}", item);
|
||||||
match *item {
|
let ch = match *item {
|
||||||
BookItem::Spacer => continue,
|
BookItem::Spacer => continue,
|
||||||
BookItem::Chapter(_, ref ch) |
|
BookItem::Chapter(_, ref ch) |
|
||||||
BookItem::Affix(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);
|
let path = self.src.join(&ch.path);
|
||||||
|
|
||||||
if !path.exists() {
|
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);
|
debug!("[*]: {:?} does not exist, trying to create file", path);
|
||||||
try!(::std::fs::create_dir_all(path.parent().unwrap()));
|
try!(::std::fs::create_dir_all(path.parent().unwrap()));
|
||||||
let mut f = try!(File::create(path));
|
let mut f = try!(File::create(path));
|
||||||
|
@ -191,8 +202,6 @@ impl MDBook {
|
||||||
try!(writeln!(f, "# {}", ch.name));
|
try!(writeln!(f, "# {}", ch.name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
debug!("[*]: init done");
|
debug!("[*]: init done");
|
||||||
|
|
Loading…
Reference in New Issue