@@ -32,6 +32,10 @@ pub struct MDBook {
32
32
renderer : Box < Renderer > ,
33
33
34
34
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 ,
35
39
}
36
40
37
41
impl MDBook {
@@ -79,6 +83,7 @@ impl MDBook {
79
83
renderer : Box :: new ( HtmlHandlebars :: new ( ) ) ,
80
84
81
85
livereload : None ,
86
+ create_missing : true ,
82
87
}
83
88
}
84
89
@@ -175,23 +180,27 @@ impl MDBook {
175
180
debug ! ( "[*]: constructing paths for missing files" ) ;
176
181
for item in self . iter ( ) {
177
182
debug ! ( "[*]: item: {:?}" , item) ;
178
- match * item {
183
+ let ch = match * item {
179
184
BookItem :: Spacer => continue ,
180
185
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 ( ) ) ;
193
196
}
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
+ }
195
204
}
196
205
}
197
206
0 commit comments