Skip to content

Commit be4654c

Browse files
Fleshed out the docs for the book module
1 parent ace0b51 commit be4654c

File tree

4 files changed

+89
-18
lines changed

4 files changed

+89
-18
lines changed

src/bin/watch.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ where
6969
};
7070

7171
// Add the source directory to the watcher
72-
if let Err(e) = watcher.watch(book.get_source(), Recursive) {
73-
println!("Error while watching {:?}:\n {:?}", book.get_source(), e);
72+
if let Err(e) = watcher.watch(book.source_dir(), Recursive) {
73+
println!("Error while watching {:?}:\n {:?}", book.source_dir(), e);
7474
::std::process::exit(0);
7575
};
7676

src/book/init.rs

+33-13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub struct BookBuilder {
1919
}
2020

2121
impl BookBuilder {
22+
/// Create a new `BookBuilder` which will generate a book in the provided
23+
/// root directory.
2224
pub fn new<P: Into<PathBuf>>(root: P) -> BookBuilder {
2325
BookBuilder {
2426
root: root.into(),
@@ -34,20 +36,32 @@ impl BookBuilder {
3436
self
3537
}
3638

39+
/// Get the config used by the `BookBuilder`.
40+
pub fn config(&self) -> &Config {
41+
&self.config
42+
}
43+
44+
/// Should the theme be copied into the generated book (so users can tweak
45+
/// it)?
3746
pub fn copy_theme(&mut self, copy: bool) -> &mut BookBuilder {
3847
self.copy_theme = copy;
3948
self
4049
}
4150

51+
/// Should we create a `.gitignore` file?
4252
pub fn create_gitignore(&mut self, create: bool) -> &mut BookBuilder {
4353
self.create_gitignore = create;
4454
self
4555
}
4656

47-
pub fn config(&self) -> &Config {
48-
&self.config
49-
}
50-
57+
/// Generate the actual book. This will:
58+
///
59+
/// - Create the directory structure.
60+
/// - Stub out some dummy chapters and the `SUMMARY.md`.
61+
/// - Create a `.gitignore` (if applicable)
62+
/// - Create a themes directory and populate it (if applicable)
63+
/// - Generate a `book.toml` file,
64+
/// - Then load the book so we can
5165
pub fn build(&self) -> Result<MDBook> {
5266
info!("Creating a new book with stub content");
5367

@@ -69,18 +83,23 @@ impl BookBuilder {
6983

7084
self.write_book_toml()?;
7185

72-
let book = MDBook::load(&self.root)
73-
.expect("The BookBuilder should always create a valid book. \
74-
If you are seeing this it is a bug and should be reported.");
86+
match MDBook::load(&self.root) {
87+
Ok(book) => Ok(book),
88+
Err(e) => {
89+
error!("{}", e);
7590

76-
Ok(book)
91+
panic!(
92+
"The BookBuilder should always create a valid book. If you are seeing this it \
93+
is a bug and should be reported."
94+
);
95+
}
96+
}
7797
}
7898

7999
fn write_book_toml(&self) -> Result<()> {
80100
debug!("[*] Writing book.toml");
81101
let book_toml = self.root.join("book.toml");
82-
let cfg = toml::to_vec(&self.config)
83-
.chain_err(|| "Unable to serialize the config")?;
102+
let cfg = toml::to_vec(&self.config).chain_err(|| "Unable to serialize the config")?;
84103

85104
File::create(book_toml)
86105
.chain_err(|| "Couldn't create book.toml")?
@@ -92,14 +111,15 @@ impl BookBuilder {
92111
fn copy_across_theme(&self) -> Result<()> {
93112
debug!("[*] Copying theme");
94113

95-
let themedir = self.config.html_config()
114+
let themedir = self.config
115+
.html_config()
96116
.and_then(|html| html.theme)
97117
.unwrap_or_else(|| self.config.book.src.join("theme"));
98118
let themedir = self.root.join(themedir);
99119

100120
if !themedir.exists() {
101-
debug!("[*]: {:?} does not exist, creating the directory",
102-
themedir); fs::create_dir(&themedir)?;
121+
debug!("[*]: {:?} does not exist, creating the directory", themedir);
122+
fs::create_dir(&themedir)?;
103123
}
104124

105125
let mut index = File::create(themedir.join("index.hbs"))?;

src/book/mod.rs

+53-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
1+
//! The internal representation of a book and infrastructure for loading it from
2+
//! disk and building it.
3+
//!
4+
//! # Examples
5+
//!
6+
//! If creating a new book from scratch, you'll want to get a `BookBuilder` via
7+
//! the `MDBook::init()` method.
8+
//!
9+
//! ```rust,no_run
10+
//! use mdbook::MDBook;
11+
//! use mdbook::config::Config;
12+
//!
13+
//! let root_dir = "/path/to/book/root";
14+
//!
15+
//! let mut cfg = Config::default();
16+
//! cfg.book.title = Some("My Book".to_string());
17+
//! cfg.book.authors.push("Michael-F-Bryan".to_string());
18+
//!
19+
//! MDBook::init(root_dir)
20+
//! .create_gitignore(true)
21+
//! .with_config(cfg)
22+
//! .build()
23+
//! .expect("Book generation failed");
24+
//! ```
25+
//!
26+
//! You can also load an existing book and build it.
27+
//!
28+
//! ```rust,no_run
29+
//! use mdbook::MDBook;
30+
//!
31+
//! let root_dir = "/path/to/book/root";
32+
//!
33+
//! let mut md = MDBook::load(root_dir)
34+
//! .expect("Unable to load the book");
35+
//! md.build().expect("Building failed");
36+
//! ```
37+
38+
#![deny(missing_docs)]
39+
140
mod summary;
241
mod book;
342
mod init;
@@ -18,13 +57,17 @@ use errors::*;
1857

1958
use config::Config;
2059

60+
/// The object used to manage and build a book.
2161
pub struct MDBook {
62+
/// The book's root directory.
2263
pub root: PathBuf,
64+
/// The configuration used to tweak now a book is built.
2365
pub config: Config,
2466

2567
book: Book,
2668
renderer: Box<Renderer>,
2769

70+
/// The URL used for live reloading when serving up the book.
2871
pub livereload: Option<String>,
2972
}
3073

@@ -123,6 +166,8 @@ impl MDBook {
123166
self.renderer.render(self)
124167
}
125168

169+
// FIXME: This doesn't belong as part of `MDBook`. It is only used by the HTML renderer
170+
#[doc(hidden)]
126171
pub fn write_file<P: AsRef<Path>>(&self, filename: P, content: &[u8]) -> Result<()> {
127172
let path = self.get_destination().join(filename);
128173

@@ -139,6 +184,7 @@ impl MDBook {
139184
self
140185
}
141186

187+
/// Run `rustdoc` tests on the book, linking against the provided libraries.
142188
pub fn test(&mut self, library_paths: Vec<&str>) -> Result<()> {
143189
let library_args: Vec<&str> = (0..library_paths.len())
144190
.map(|_| "-L")
@@ -151,7 +197,7 @@ impl MDBook {
151197
for item in self.iter() {
152198
if let BookItem::Chapter(ref ch) = *item {
153199
if !ch.path.as_os_str().is_empty() {
154-
let path = self.get_source().join(&ch.path);
200+
let path = self.source_dir().join(&ch.path);
155201
let base = path.parent()
156202
.ok_or_else(|| String::from("Invalid bookitem path!"))?;
157203
let content = utils::fs::file_to_string(&path)?;
@@ -182,14 +228,19 @@ impl MDBook {
182228
Ok(())
183229
}
184230

231+
// FIXME: This doesn't belong under `MDBook`, it should really be passed to the renderer directly.
232+
#[doc(hidden)]
185233
pub fn get_destination(&self) -> PathBuf {
186234
self.root.join(&self.config.build.build_dir)
187235
}
188236

189-
pub fn get_source(&self) -> PathBuf {
237+
/// Get the directory containing this book's source files.
238+
pub fn source_dir(&self) -> PathBuf {
190239
self.root.join(&self.config.book.src)
191240
}
192241

242+
// FIXME: This belongs as part of the `HtmlConfig`.
243+
#[doc(hidden)]
193244
pub fn theme_dir(&self) -> PathBuf {
194245
match self.config.html_config().and_then(|h| h.theme) {
195246
Some(d) => self.root.join(d),

src/renderer/html_handlebars/hbs_renderer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ impl Renderer for HtmlHandlebars {
318318
self.copy_additional_css_and_js(book)?;
319319

320320
// Copy all remaining files
321-
let src = book.get_source();
321+
let src = book.source_dir();
322322
utils::fs::copy_files_except_ext(&src, &destination, true, &["md"])?;
323323

324324
Ok(())

0 commit comments

Comments
 (0)