Skip to content

Commit a4a708b

Browse files
authored
Merge pull request #254 from frewsxcv/frewsxcv-no-create
Implement new 'no-create' build flag.
2 parents a3b925e + 4525810 commit a4a708b

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

src/bin/mdbook.rs

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ fn main() {
6363
.about("Build the book from the markdown files")
6464
.arg_from_usage("-o, --open 'Open the compiled book in a web browser'")
6565
.arg_from_usage("-d, --dest-dir=[dest-dir] 'The output directory for your book{n}(Defaults to ./book when omitted)'")
66+
.arg_from_usage("--no-create 'Will not create non-existent files linked from SUMMARY.md'")
6667
.arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'"))
6768
.subcommand(SubCommand::with_name("watch")
6869
.about("Watch the files for changes")
@@ -174,6 +175,10 @@ fn build(args: &ArgMatches) -> Result<(), Box<Error>> {
174175
None => book
175176
};
176177

178+
if args.is_present("no-create") {
179+
book.create_missing = false;
180+
}
181+
177182
try!(book.build());
178183

179184
if args.is_present("open") {

src/book/mod.rs

+23-14
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ pub struct MDBook {
3232
renderer: Box<Renderer>,
3333

3434
livereload: Option<String>,
35+
36+
/// Should `mdbook build` create files referenced from SUMMARY.md if they
37+
/// don't exist
38+
pub create_missing: bool,
3539
}
3640

3741
impl MDBook {
@@ -79,6 +83,7 @@ impl MDBook {
7983
renderer: Box::new(HtmlHandlebars::new()),
8084

8185
livereload: None,
86+
create_missing: true,
8287
}
8388
}
8489

@@ -175,23 +180,27 @@ impl MDBook {
175180
debug!("[*]: constructing paths for missing files");
176181
for item in self.iter() {
177182
debug!("[*]: item: {:?}", item);
178-
match *item {
183+
let ch = match *item {
179184
BookItem::Spacer => continue,
180185
BookItem::Chapter(_, ref ch) |
181-
BookItem::Affix(ref ch) => {
182-
if ch.path != PathBuf::new() {
183-
let path = self.src.join(&ch.path);
184-
185-
if !path.exists() {
186-
debug!("[*]: {:?} does not exist, trying to create file", path);
187-
try!(::std::fs::create_dir_all(path.parent().unwrap()));
188-
let mut f = try!(File::create(path));
189-
190-
// debug!("[*]: Writing to {:?}", path);
191-
try!(writeln!(f, "# {}", ch.name));
192-
}
186+
BookItem::Affix(ref ch) => ch,
187+
};
188+
if ch.path.as_os_str().is_empty() {
189+
let path = self.src.join(&ch.path);
190+
191+
if !path.exists() {
192+
if !self.create_missing {
193+
return Err(format!(
194+
"'{}' referenced from SUMMARY.md does not exist.",
195+
path.to_string_lossy()).into());
193196
}
194-
},
197+
debug!("[*]: {:?} does not exist, trying to create file", path);
198+
try!(::std::fs::create_dir_all(path.parent().unwrap()));
199+
let mut f = try!(File::create(path));
200+
201+
// debug!("[*]: Writing to {:?}", path);
202+
try!(writeln!(f, "# {}", ch.name));
203+
}
195204
}
196205
}
197206

0 commit comments

Comments
 (0)