@@ -39,17 +39,19 @@ fn create_missing(src_dir: &Path, summary: &Summary) -> Result<()> {
39
39
let next = items. pop ( ) . expect ( "already checked" ) ;
40
40
41
41
if let SummaryItem :: Link ( ref link) = * next {
42
- let filename = src_dir. join ( & link. location ) ;
43
- if !filename. exists ( ) {
44
- if let Some ( parent) = filename. parent ( ) {
45
- if !parent. exists ( ) {
46
- fs:: create_dir_all ( parent) ?;
42
+ if let Some ( ref location) = link. location {
43
+ let filename = src_dir. join ( location) ;
44
+ if !filename. exists ( ) {
45
+ if let Some ( parent) = filename. parent ( ) {
46
+ if !parent. exists ( ) {
47
+ fs:: create_dir_all ( parent) ?;
48
+ }
47
49
}
48
- }
49
- debug ! ( "Creating missing file {}" , filename. display( ) ) ;
50
+ debug ! ( "Creating missing file {}" , filename. display( ) ) ;
50
51
51
- let mut f = File :: create ( & filename) ?;
52
- writeln ! ( f, "# {}" , link. name) ?;
52
+ let mut f = File :: create ( & filename) ?;
53
+ writeln ! ( f, "# {}" , link. name) ?;
54
+ }
53
55
}
54
56
55
57
items. extend ( & link. nested_items ) ;
@@ -152,7 +154,7 @@ pub struct Chapter {
152
154
/// Nested items.
153
155
pub sub_items : Vec < BookItem > ,
154
156
/// The chapter's location, relative to the `SUMMARY.md` file.
155
- pub path : PathBuf ,
157
+ pub path : Option < PathBuf > ,
156
158
/// An ordered list of the names of each chapter above this one, in the hierarchy.
157
159
pub parent_names : Vec < String > ,
158
160
}
@@ -168,11 +170,31 @@ impl Chapter {
168
170
Chapter {
169
171
name : name. to_string ( ) ,
170
172
content,
171
- path : path. into ( ) ,
173
+ path : Some ( path. into ( ) ) ,
174
+ parent_names,
175
+ ..Default :: default ( )
176
+ }
177
+ }
178
+
179
+ /// Create a new draft chapter that is not attached to a source markdown file and has
180
+ /// thus no content.
181
+ pub fn new_draft ( name : & str , parent_names : Vec < String > ) -> Self {
182
+ Chapter {
183
+ name : name. to_string ( ) ,
184
+ content : String :: new ( ) ,
185
+ path : None ,
172
186
parent_names,
173
187
..Default :: default ( )
174
188
}
175
189
}
190
+
191
+ /// Check if the chapter is a draft chapter, meaning it has no path to a source markdown file
192
+ pub fn is_draft_chapter ( & self ) -> bool {
193
+ match self . path {
194
+ Some ( _) => false ,
195
+ None => true ,
196
+ }
197
+ }
176
198
}
177
199
178
200
/// Use the provided `Summary` to load a `Book` from disk.
@@ -202,7 +224,7 @@ pub(crate) fn load_book_from_disk<P: AsRef<Path>>(summary: &Summary, src_dir: P)
202
224
} )
203
225
}
204
226
205
- fn load_summary_item < P : AsRef < Path > > (
227
+ fn load_summary_item < P : AsRef < Path > + Clone > (
206
228
item : & SummaryItem ,
207
229
src_dir : P ,
208
230
parent_names : Vec < String > ,
@@ -220,28 +242,35 @@ fn load_chapter<P: AsRef<Path>>(
220
242
src_dir : P ,
221
243
parent_names : Vec < String > ,
222
244
) -> Result < Chapter > {
223
- debug ! ( "Loading {} ({})" , link. name, link. location. display( ) ) ;
224
245
let src_dir = src_dir. as_ref ( ) ;
225
246
226
- let location = if link. location . is_absolute ( ) {
227
- link. location . clone ( )
228
- } else {
229
- src_dir. join ( & link. location )
230
- } ;
247
+ let mut ch = if let Some ( ref link_location) = link. location {
248
+ debug ! ( "Loading {} ({})" , link. name, link_location. display( ) ) ;
249
+
250
+ let location = if link_location. is_absolute ( ) {
251
+ link_location. clone ( )
252
+ } else {
253
+ src_dir. join ( link_location)
254
+ } ;
231
255
232
- let mut f = File :: open ( & location)
233
- . chain_err ( || format ! ( "Chapter file not found, {}" , link . location . display( ) ) ) ?;
256
+ let mut f = File :: open ( & location)
257
+ . chain_err ( || format ! ( "Chapter file not found, {}" , link_location . display( ) ) ) ?;
234
258
235
- let mut content = String :: new ( ) ;
236
- f. read_to_string ( & mut content)
237
- . chain_err ( || format ! ( "Unable to read \" {}\" ({})" , link. name, location. display( ) ) ) ?;
259
+ let mut content = String :: new ( ) ;
260
+ f. read_to_string ( & mut content)
261
+ . chain_err ( || format ! ( "Unable to read \" {}\" ({})" , link. name, location. display( ) ) ) ?;
238
262
239
- let stripped = location
240
- . strip_prefix ( & src_dir)
241
- . expect ( "Chapters are always inside a book" ) ;
263
+ let stripped = location
264
+ . strip_prefix ( & src_dir)
265
+ . expect ( "Chapters are always inside a book" ) ;
266
+
267
+ Chapter :: new ( & link. name , content, stripped, parent_names. clone ( ) )
268
+ } else {
269
+ Chapter :: new_draft ( & link. name , parent_names. clone ( ) )
270
+ } ;
242
271
243
272
let mut sub_item_parents = parent_names. clone ( ) ;
244
- let mut ch = Chapter :: new ( & link . name , content , stripped , parent_names ) ;
273
+
245
274
ch. number = link. number . clone ( ) ;
246
275
247
276
sub_item_parents. push ( link. name . clone ( ) ) ;
@@ -376,15 +405,15 @@ And here is some \
376
405
name : String :: from ( "Nested Chapter 1" ) ,
377
406
content : String :: from ( "Hello World!" ) ,
378
407
number : Some ( SectionNumber ( vec ! [ 1 , 2 ] ) ) ,
379
- path : PathBuf :: from ( "second.md" ) ,
408
+ path : Some ( PathBuf :: from ( "second.md" ) ) ,
380
409
parent_names : vec ! [ String :: from( "Chapter 1" ) ] ,
381
410
sub_items : Vec :: new ( ) ,
382
411
} ;
383
412
let should_be = BookItem :: Chapter ( Chapter {
384
413
name : String :: from ( "Chapter 1" ) ,
385
414
content : String :: from ( DUMMY_SRC ) ,
386
415
number : None ,
387
- path : PathBuf :: from ( "chapter_1.md" ) ,
416
+ path : Some ( PathBuf :: from ( "chapter_1.md" ) ) ,
388
417
parent_names : Vec :: new ( ) ,
389
418
sub_items : vec ! [
390
419
BookItem :: Chapter ( nested. clone( ) ) ,
@@ -408,7 +437,7 @@ And here is some \
408
437
sections : vec ! [ BookItem :: Chapter ( Chapter {
409
438
name: String :: from( "Chapter 1" ) ,
410
439
content: String :: from( DUMMY_SRC ) ,
411
- path: PathBuf :: from( "chapter_1.md" ) ,
440
+ path: Some ( PathBuf :: from( "chapter_1.md" ) ) ,
412
441
..Default :: default ( )
413
442
} ) ] ,
414
443
..Default :: default ( )
@@ -448,7 +477,7 @@ And here is some \
448
477
name: String :: from( "Chapter 1" ) ,
449
478
content: String :: from( DUMMY_SRC ) ,
450
479
number: None ,
451
- path: PathBuf :: from( "Chapter_1/index.md" ) ,
480
+ path: Some ( PathBuf :: from( "Chapter_1/index.md" ) ) ,
452
481
parent_names: Vec :: new( ) ,
453
482
sub_items: vec![
454
483
BookItem :: Chapter ( Chapter :: new(
@@ -500,7 +529,7 @@ And here is some \
500
529
name: String :: from( "Chapter 1" ) ,
501
530
content: String :: from( DUMMY_SRC ) ,
502
531
number: None ,
503
- path: PathBuf :: from( "Chapter_1/index.md" ) ,
532
+ path: Some ( PathBuf :: from( "Chapter_1/index.md" ) ) ,
504
533
parent_names: Vec :: new( ) ,
505
534
sub_items: vec![
506
535
BookItem :: Chapter ( Chapter :: new(
@@ -537,7 +566,7 @@ And here is some \
537
566
let summary = Summary {
538
567
numbered_chapters : vec ! [ SummaryItem :: Link ( Link {
539
568
name: String :: from( "Empty" ) ,
540
- location: PathBuf :: from( "" ) ,
569
+ location: Some ( PathBuf :: from( "" ) ) ,
541
570
..Default :: default ( )
542
571
} ) ] ,
543
572
..Default :: default ( )
@@ -556,7 +585,7 @@ And here is some \
556
585
let summary = Summary {
557
586
numbered_chapters : vec ! [ SummaryItem :: Link ( Link {
558
587
name: String :: from( "nested" ) ,
559
- location: dir,
588
+ location: Some ( dir) ,
560
589
..Default :: default ( )
561
590
} ) ] ,
562
591
..Default :: default ( )
0 commit comments