@@ -12,9 +12,17 @@ use renderer::{Renderer, HtmlHandlebars};
12
12
13
13
14
14
pub struct MDBook {
15
- config : BookConfig ,
15
+ root : PathBuf ,
16
+ dest : PathBuf ,
17
+ src : PathBuf ,
18
+
19
+ pub title : String ,
20
+ pub author : String ,
21
+ pub description : String ,
22
+
16
23
pub content : Vec < BookItem > ,
17
24
renderer : Box < Renderer > ,
25
+
18
26
#[ cfg( feature = "serve" ) ]
19
27
livereload : Option < String > ,
20
28
}
@@ -34,11 +42,15 @@ impl MDBook {
34
42
}
35
43
36
44
MDBook {
45
+ root : root. to_owned ( ) ,
46
+ dest : PathBuf :: from ( "book" ) ,
47
+ src : PathBuf :: from ( "src" ) ,
48
+
49
+ title : String :: new ( ) ,
50
+ author : String :: new ( ) ,
51
+ description : String :: new ( ) ,
52
+
37
53
content : vec ! [ ] ,
38
- config : BookConfig :: new ( root)
39
- . set_src ( & root. join ( "src" ) )
40
- . set_dest ( & root. join ( "book" ) )
41
- . to_owned ( ) ,
42
54
renderer : Box :: new ( HtmlHandlebars :: new ( ) ) ,
43
55
livereload : None ,
44
56
}
@@ -97,33 +109,31 @@ impl MDBook {
97
109
98
110
debug ! ( "[fn]: init" ) ;
99
111
100
- if !self . config . get_root ( ) . exists ( ) {
101
- fs:: create_dir_all ( self . config . get_root ( ) ) . unwrap ( ) ;
102
- output ! ( "{:?} created" , self . config . get_root ( ) ) ;
112
+ if !self . root . exists ( ) {
113
+ fs:: create_dir_all ( & self . root ) . unwrap ( ) ;
114
+ output ! ( "{:?} created" , & self . root ) ;
103
115
}
104
116
105
117
{
106
- let dest = self . config . get_dest ( ) ;
107
- let src = self . config . get_src ( ) ;
108
118
109
- if !dest. exists ( ) {
110
- debug ! ( "[*]: {:?} does not exist, trying to create directory" , dest) ;
111
- try!( fs:: create_dir ( & dest) ) ;
119
+ if !self . dest . exists ( ) {
120
+ debug ! ( "[*]: {:?} does not exist, trying to create directory" , self . dest) ;
121
+ try!( fs:: create_dir ( & self . dest ) ) ;
112
122
}
113
123
114
- if !src. exists ( ) {
115
- debug ! ( "[*]: {:?} does not exist, trying to create directory" , src) ;
116
- try!( fs:: create_dir ( & src) ) ;
124
+ if !self . src . exists ( ) {
125
+ debug ! ( "[*]: {:?} does not exist, trying to create directory" , self . src) ;
126
+ try!( fs:: create_dir ( & self . src ) ) ;
117
127
}
118
128
119
- let summary = src. join ( "SUMMARY.md" ) ;
129
+ let summary = self . src . join ( "SUMMARY.md" ) ;
120
130
121
131
if !summary. exists ( ) {
122
132
123
133
// Summary does not exist, create it
124
134
125
135
debug ! ( "[*]: {:?} does not exist, trying to create SUMMARY.md" , src. join( "SUMMARY.md" ) ) ;
126
- let mut f = try!( File :: create ( & src. join ( "SUMMARY.md" ) ) ) ;
136
+ let mut f = try!( File :: create ( & self . src . join ( "SUMMARY.md" ) ) ) ;
127
137
128
138
debug ! ( "[*]: Writing to SUMMARY.md" ) ;
129
139
@@ -143,7 +153,7 @@ impl MDBook {
143
153
BookItem :: Spacer => continue ,
144
154
BookItem :: Chapter ( _, ref ch) | BookItem :: Affix ( ref ch) => {
145
155
if ch. path != PathBuf :: new ( ) {
146
- let path = self . config . get_src ( ) . join ( & ch. path ) ;
156
+ let path = self . src . join ( & ch. path ) ;
147
157
148
158
if !path. exists ( ) {
149
159
debug ! ( "[*]: {:?} does not exist, trying to create file" , path) ;
@@ -170,12 +180,12 @@ impl MDBook {
170
180
171
181
// Because of `src/book/mdbook.rs#L37-L39`, `dest` will always start with `root`. If it
172
182
// is not, `strip_prefix` will return an Error.
173
- if !self . get_dest ( ) . starts_with ( self . get_root ( ) ) {
183
+ if !self . get_dest ( ) . starts_with ( & self . root ) {
174
184
return ;
175
185
}
176
186
177
187
let relative = self . get_dest ( )
178
- . strip_prefix ( self . get_root ( ) )
188
+ . strip_prefix ( & self . root )
179
189
. expect ( "Destination is not relative to root." ) ;
180
190
let relative = relative. to_str ( )
181
191
. expect ( "Path could not be yielded into a string slice." ) ;
@@ -201,7 +211,7 @@ impl MDBook {
201
211
try!( self . init ( ) ) ;
202
212
203
213
// Clean output directory
204
- try!( utils:: fs:: remove_dir_content ( & self . config . get_dest ( ) ) ) ;
214
+ try!( utils:: fs:: remove_dir_content ( & self . dest ) ) ;
205
215
206
216
try!( self . renderer . render ( & self ) ) ;
207
217
@@ -210,13 +220,13 @@ impl MDBook {
210
220
211
221
212
222
pub fn get_gitignore ( & self ) -> PathBuf {
213
- self . config . get_root ( ) . join ( ".gitignore" )
223
+ self . root . join ( ".gitignore" )
214
224
}
215
225
216
226
pub fn copy_theme ( & self ) -> Result < ( ) , Box < Error > > {
217
227
debug ! ( "[fn]: copy_theme" ) ;
218
228
219
- let theme_dir = self . config . get_src ( ) . join ( "theme" ) ;
229
+ let theme_dir = self . src . join ( "theme" ) ;
220
230
221
231
if !theme_dir. exists ( ) {
222
232
debug ! ( "[*]: {:?} does not exist, trying to create directory" , theme_dir) ;
@@ -267,8 +277,18 @@ impl MDBook {
267
277
/// of the current working directory by using a relative path instead of an absolute path.
268
278
269
279
pub fn read_config ( mut self ) -> Self {
270
- let root = self . config . get_root ( ) . to_owned ( ) ;
271
- self . config . read_config ( & root) ;
280
+
281
+ let config = BookConfig :: new ( & self . root )
282
+ . read_config ( & self . root )
283
+ . to_owned ( ) ;
284
+
285
+ self . title = config. title ;
286
+ self . description = config. description ;
287
+ self . author = config. author ;
288
+
289
+ self . dest = config. dest ;
290
+ self . src = config. src ;
291
+
272
292
self
273
293
}
274
294
@@ -331,74 +351,74 @@ impl MDBook {
331
351
}
332
352
333
353
pub fn get_root ( & self ) -> & Path {
334
- self . config . get_root ( )
354
+ & self . root
335
355
}
336
356
337
357
pub fn set_dest ( mut self , dest : & Path ) -> Self {
338
358
339
359
// Handle absolute and relative paths
340
360
match dest. is_absolute ( ) {
341
361
true => {
342
- self . config . set_dest ( dest ) ;
362
+ self . dest = dest . to_owned ( ) ;
343
363
} ,
344
364
false => {
345
- let dest = self . config . get_root ( ) . join ( dest) . to_owned ( ) ;
346
- self . config . set_dest ( & dest) ;
365
+ let dest = self . root . join ( dest) . to_owned ( ) ;
366
+ self . dest = dest ;
347
367
} ,
348
368
}
349
369
350
370
self
351
371
}
352
372
353
373
pub fn get_dest ( & self ) -> & Path {
354
- self . config . get_dest ( )
374
+ & self . dest
355
375
}
356
376
357
377
pub fn set_src ( mut self , src : & Path ) -> Self {
358
378
359
379
// Handle absolute and relative paths
360
380
match src. is_absolute ( ) {
361
381
true => {
362
- self . config . set_src ( src ) ;
382
+ self . src = src . to_owned ( ) ;
363
383
} ,
364
384
false => {
365
- let src = self . config . get_root ( ) . join ( src) . to_owned ( ) ;
366
- self . config . set_src ( & src) ;
385
+ let src = self . root . join ( src) . to_owned ( ) ;
386
+ self . src = src ;
367
387
} ,
368
388
}
369
389
370
390
self
371
391
}
372
392
373
393
pub fn get_src ( & self ) -> & Path {
374
- self . config . get_src ( )
394
+ & self . src
375
395
}
376
396
377
397
pub fn set_title ( mut self , title : & str ) -> Self {
378
- self . config . title = title. to_owned ( ) ;
398
+ self . title = title. to_owned ( ) ;
379
399
self
380
400
}
381
401
382
402
pub fn get_title ( & self ) -> & str {
383
- & self . config . title
403
+ & self . title
384
404
}
385
405
386
406
pub fn set_author ( mut self , author : & str ) -> Self {
387
- self . config . author = author. to_owned ( ) ;
407
+ self . author = author. to_owned ( ) ;
388
408
self
389
409
}
390
410
391
411
pub fn get_author ( & self ) -> & str {
392
- & self . config . author
412
+ & self . author
393
413
}
394
414
395
415
pub fn set_description ( mut self , description : & str ) -> Self {
396
- self . config . description = description. to_owned ( ) ;
416
+ self . description = description. to_owned ( ) ;
397
417
self
398
418
}
399
419
400
420
pub fn get_description ( & self ) -> & str {
401
- & self . config . description
421
+ & self . description
402
422
}
403
423
404
424
pub fn set_livereload ( & mut self , livereload : String ) -> & mut Self {
@@ -421,7 +441,7 @@ impl MDBook {
421
441
// Construct book
422
442
fn parse_summary ( & mut self ) -> Result < ( ) , Box < Error > > {
423
443
// When append becomes stable, use self.content.append() ...
424
- self . content = try!( parse:: construct_bookitems ( & self . config . get_src ( ) . join ( "SUMMARY.md" ) ) ) ;
444
+ self . content = try!( parse:: construct_bookitems ( & self . src . join ( "SUMMARY.md" ) ) ) ;
425
445
Ok ( ( ) )
426
446
}
427
447
}
0 commit comments