Skip to content

Commit 2ec5648

Browse files
committed
Remove BookConfig field from MDBook
MDBook now stores the necessary information, BookConfig is not used as a field anymore. It is only used for parsing the configuration file. This allows to more easily replace the book.json config with the new tomlbased config
1 parent 6aa6546 commit 2ec5648

File tree

2 files changed

+64
-44
lines changed

2 files changed

+64
-44
lines changed

src/book/bookconfig.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ pub struct BookConfig {
99
pub author: String,
1010
pub description: String,
1111
root: PathBuf,
12-
dest: PathBuf,
13-
src: PathBuf,
12+
pub dest: PathBuf,
13+
pub src: PathBuf,
1414
pub indent_spaces: i32,
1515
multilingual: bool,
1616
}

src/book/mdbook.rs

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@ use renderer::{Renderer, HtmlHandlebars};
1212

1313

1414
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+
1623
pub content: Vec<BookItem>,
1724
renderer: Box<Renderer>,
25+
1826
#[cfg(feature = "serve")]
1927
livereload: Option<String>,
2028
}
@@ -34,11 +42,15 @@ impl MDBook {
3442
}
3543

3644
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+
3753
content: vec![],
38-
config: BookConfig::new(root)
39-
.set_src(&root.join("src"))
40-
.set_dest(&root.join("book"))
41-
.to_owned(),
4254
renderer: Box::new(HtmlHandlebars::new()),
4355
livereload: None,
4456
}
@@ -97,33 +109,31 @@ impl MDBook {
97109

98110
debug!("[fn]: init");
99111

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);
103115
}
104116

105117
{
106-
let dest = self.config.get_dest();
107-
let src = self.config.get_src();
108118

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));
112122
}
113123

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));
117127
}
118128

119-
let summary = src.join("SUMMARY.md");
129+
let summary = self.src.join("SUMMARY.md");
120130

121131
if !summary.exists() {
122132

123133
// Summary does not exist, create it
124134

125135
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")));
127137

128138
debug!("[*]: Writing to SUMMARY.md");
129139

@@ -143,7 +153,7 @@ impl MDBook {
143153
BookItem::Spacer => continue,
144154
BookItem::Chapter(_, ref ch) | BookItem::Affix(ref ch) => {
145155
if ch.path != PathBuf::new() {
146-
let path = self.config.get_src().join(&ch.path);
156+
let path = self.src.join(&ch.path);
147157

148158
if !path.exists() {
149159
debug!("[*]: {:?} does not exist, trying to create file", path);
@@ -170,12 +180,12 @@ impl MDBook {
170180

171181
// Because of `src/book/mdbook.rs#L37-L39`, `dest` will always start with `root`. If it
172182
// 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) {
174184
return;
175185
}
176186

177187
let relative = self.get_dest()
178-
.strip_prefix(self.get_root())
188+
.strip_prefix(&self.root)
179189
.expect("Destination is not relative to root.");
180190
let relative = relative.to_str()
181191
.expect("Path could not be yielded into a string slice.");
@@ -201,7 +211,7 @@ impl MDBook {
201211
try!(self.init());
202212

203213
// Clean output directory
204-
try!(utils::fs::remove_dir_content(&self.config.get_dest()));
214+
try!(utils::fs::remove_dir_content(&self.dest));
205215

206216
try!(self.renderer.render(&self));
207217

@@ -210,13 +220,13 @@ impl MDBook {
210220

211221

212222
pub fn get_gitignore(&self) -> PathBuf {
213-
self.config.get_root().join(".gitignore")
223+
self.root.join(".gitignore")
214224
}
215225

216226
pub fn copy_theme(&self) -> Result<(), Box<Error>> {
217227
debug!("[fn]: copy_theme");
218228

219-
let theme_dir = self.config.get_src().join("theme");
229+
let theme_dir = self.src.join("theme");
220230

221231
if !theme_dir.exists() {
222232
debug!("[*]: {:?} does not exist, trying to create directory", theme_dir);
@@ -267,8 +277,18 @@ impl MDBook {
267277
/// of the current working directory by using a relative path instead of an absolute path.
268278
269279
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+
272292
self
273293
}
274294

@@ -331,74 +351,74 @@ impl MDBook {
331351
}
332352

333353
pub fn get_root(&self) -> &Path {
334-
self.config.get_root()
354+
&self.root
335355
}
336356

337357
pub fn set_dest(mut self, dest: &Path) -> Self {
338358

339359
// Handle absolute and relative paths
340360
match dest.is_absolute() {
341361
true => {
342-
self.config.set_dest(dest);
362+
self.dest = dest.to_owned();
343363
},
344364
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;
347367
},
348368
}
349369

350370
self
351371
}
352372

353373
pub fn get_dest(&self) -> &Path {
354-
self.config.get_dest()
374+
&self.dest
355375
}
356376

357377
pub fn set_src(mut self, src: &Path) -> Self {
358378

359379
// Handle absolute and relative paths
360380
match src.is_absolute() {
361381
true => {
362-
self.config.set_src(src);
382+
self.src = src.to_owned();
363383
},
364384
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;
367387
},
368388
}
369389

370390
self
371391
}
372392

373393
pub fn get_src(&self) -> &Path {
374-
self.config.get_src()
394+
&self.src
375395
}
376396

377397
pub fn set_title(mut self, title: &str) -> Self {
378-
self.config.title = title.to_owned();
398+
self.title = title.to_owned();
379399
self
380400
}
381401

382402
pub fn get_title(&self) -> &str {
383-
&self.config.title
403+
&self.title
384404
}
385405

386406
pub fn set_author(mut self, author: &str) -> Self {
387-
self.config.author = author.to_owned();
407+
self.author = author.to_owned();
388408
self
389409
}
390410

391411
pub fn get_author(&self) -> &str {
392-
&self.config.author
412+
&self.author
393413
}
394414

395415
pub fn set_description(mut self, description: &str) -> Self {
396-
self.config.description = description.to_owned();
416+
self.description = description.to_owned();
397417
self
398418
}
399419

400420
pub fn get_description(&self) -> &str {
401-
&self.config.description
421+
&self.description
402422
}
403423

404424
pub fn set_livereload(&mut self, livereload: String) -> &mut Self {
@@ -421,7 +441,7 @@ impl MDBook {
421441
// Construct book
422442
fn parse_summary(&mut self) -> Result<(), Box<Error>> {
423443
// 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")));
425445
Ok(())
426446
}
427447
}

0 commit comments

Comments
 (0)