Skip to content

Commit 77fe87e

Browse files
Updated the documentation
1 parent 92f0fa0 commit 77fe87e

File tree

5 files changed

+83
-86
lines changed

5 files changed

+83
-86
lines changed

rustfmt.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn_args_density = "Compressed"
99
enum_trailing_comma = true
1010
match_block_trailing_comma = true
1111
struct_trailing_comma = "Always"
12-
wrap_comments = true
12+
wrap_comments = false
1313
use_try_shorthand = true
1414

1515
report_todo = "Always"

src/book/builder.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ impl Builder {
3333
self
3434
}
3535

36+
/// Set the renderer to be used.
37+
pub fn set_renderer(mut self, renderer: Box<Renderer>) -> Self {
38+
self.renderer = Some(renderer);
39+
self
40+
}
41+
3642
pub fn build(self) -> Result<MDBook> {
3743
// if no custom config provided, try to read it from disk
3844
let cfg = match self.config {

src/book/mod.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl MDBook {
7070
/// # use mdbook::MDBook;
7171
/// # use mdbook::loader::BookItem;
7272
/// # #[allow(unused_variables)]
73-
/// # fn run() -> ::errors::Result<()> {
73+
/// # fn run() -> ::mdbook::errors::Result<()> {
7474
/// # let book = MDBook::new("mybook")?;
7575
/// for item in book.iter() {
7676
/// match *item {
@@ -90,7 +90,6 @@ impl MDBook {
9090
/// # }
9191
/// # fn main() { run().unwrap() }
9292
/// ```
93-
9493
pub fn iter(&self) -> BookItems {
9594
self.book.iter()
9695
}
@@ -271,22 +270,27 @@ impl MDBook {
271270
}
272271

273272
pub fn test(&mut self, library_paths: Vec<&str>) -> Result<()> {
274-
// read in the chapters
275-
self.parse_summary().chain_err(|| "Couldn't parse summary")?;
276-
let library_args: Vec<&str> = (0..library_paths.len()).map(|_| "-L")
277-
.zip(library_paths.into_iter())
278-
.flat_map(|x| vec![x.0, x.1])
279-
.collect();
273+
let library_args: Vec<&str> = (0..library_paths.len())
274+
.map(|_| "-L")
275+
.zip(library_paths.into_iter())
276+
.flat_map(|x| vec![x.0, x.1])
277+
.collect();
278+
280279
for item in self.iter() {
280+
if let BookItem::Chapter(ref ch) = *item {
281+
let chapter_path = ch.path();
281282

282-
if let BookItem::Chapter(_, ref ch) = *item {
283-
if ch.path != PathBuf::new() {
283+
if chapter_path == Path::new("") {
284284

285-
let path = self.get_source().join(&ch.path);
285+
let path = self.get_source().join(&chapter_path);
286286

287287
println!("[*]: Testing file: {:?}", path);
288288

289-
let output = Command::new("rustdoc").arg(&path).arg("--test").args(&library_args).output()?;
289+
let output = Command::new("rustdoc")
290+
.arg(&path)
291+
.arg("--test")
292+
.args(&library_args)
293+
.output()?;
290294

291295
if !output.status.success() {
292296
bail!(ErrorKind::Subprocess("Rustdoc returned an error".to_string(), output));

src/lib.rs

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,17 @@
1818
//! ## Example
1919
//!
2020
//! ```no_run
21-
//! extern crate mdbook;
22-
//!
23-
//! use mdbook::MDBook;
24-
//!
25-
//! # #[allow(unused_variables)]
26-
//! fn main() {
27-
//! let mut book = MDBook::new("my-book") // Path to root
28-
//! .with_source("src") // Path from root to source directory
29-
//! .with_destination("book") // Path from root to output directory
30-
//! .read_config() // Parse book.toml configuration file
31-
//! .expect("I don't handle configuration file errors, but you should!");
32-
//! book.build().unwrap(); // Render the book
33-
//! }
21+
//! use mdbook::config::BookConfig;
22+
//! use mdbook::book::Builder;
23+
//!
24+
//! // configure our book
25+
//! let config = BookConfig::new("my-book").with_source("src");
26+
//!
27+
//! // then create a book which uses that configuration
28+
//! let mut book = Builder::new("my-book").with_config(config).build().unwrap();
29+
//!
30+
//! // and finally, render the book as html
31+
//! book.build().unwrap();
3432
//! ```
3533
//!
3634
//! ## Implementing a new Renderer
@@ -41,17 +39,14 @@
4139
//! And then you can swap in your renderer like this:
4240
//!
4341
//! ```no_run
44-
//! # extern crate mdbook;
45-
//! #
46-
//! # use mdbook::MDBook;
47-
//! # use mdbook::renderer::HtmlHandlebars;
48-
//! #
49-
//! # #[allow(unused_variables)]
50-
//! # fn main() {
51-
//! # let your_renderer = HtmlHandlebars::new();
52-
//! #
53-
//! let book = MDBook::new("my-book").set_renderer(Box::new(your_renderer));
54-
//! # }
42+
//! # #![allow(unused_variables)]
43+
//! use mdbook::renderer::HtmlHandlebars;
44+
//! use mdbook::book::Builder;
45+
//!
46+
//! let your_renderer = HtmlHandlebars::new();
47+
//! let book = Builder::new("my-book").set_renderer(Box::new(your_renderer))
48+
//! .build()
49+
//! .unwrap();
5550
//! ```
5651
//! If you make a renderer, you get the book constructed in form of `Vec<BookItems>` and you get
5752
//! the book config in a `BookConfig` struct.
@@ -91,7 +86,6 @@ extern crate serde_json;
9186
extern crate pretty_assertions;
9287
extern crate tempdir;
9388

94-
mod parse;
9589
mod preprocess;
9690
pub mod book;
9791
pub mod config;

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -30,55 +30,48 @@ impl HtmlHandlebars {
3030

3131
fn render_item(&self, item: &BookItem, mut ctx: RenderItemContext, print_content: &mut String) -> Result<()> {
3232
// FIXME: This should be made DRY-er and rely less on mutable state
33+
// deferred because we'll probably need to rewrite it anyway when
34+
// renderers are made more pluggable
3335
match *item {
34-
BookItem::Chapter(_, ref ch) |
35-
BookItem::Affix(ref ch) => {
36-
if ch.path != PathBuf::new() {
37-
38-
let path = ctx.book.get_source().join(&ch.path);
39-
40-
debug!("[*]: Opening file: {:?}", path);
41-
let mut f = File::open(&path)?;
42-
let mut content: String = String::new();
43-
44-
debug!("[*]: Reading file");
45-
f.read_to_string(&mut content)?;
46-
47-
// Parse and expand links
48-
if let Some(p) = path.parent() {
49-
content = preprocess::links::replace_all(&content, p)?;
50-
}
51-
52-
content = utils::render_markdown(&content, ctx.book.get_curly_quotes());
53-
print_content.push_str(&content);
54-
55-
// Update the context with data for this file
56-
let path = ch.path.to_str().ok_or_else(|| {
57-
io::Error::new(io::ErrorKind::Other, "Could not convert path to str")
58-
})?;
59-
60-
ctx.data.insert("path".to_owned(), json!(path));
61-
ctx.data.insert("content".to_owned(), json!(content));
62-
ctx.data.insert("chapter_title".to_owned(), json!(ch.name));
63-
ctx.data.insert(
64-
"path_to_root".to_owned(),
65-
json!(utils::fs::path_to_root(&ch.path)),
66-
);
67-
68-
// Render the handlebars template with the data
69-
debug!("[*]: Render template");
70-
let rendered = ctx.handlebars.render("index", &ctx.data)?;
71-
let rendered = self.post_process(rendered);
72-
73-
let filename = Path::new(&ch.path).with_extension("html");
74-
75-
// Write to file
76-
info!("[*] Creating {:?} ✓", filename.display());
77-
ctx.book.write_file(filename, &rendered.into_bytes())?;
78-
79-
if ctx.is_index {
80-
self.render_index(ctx.book, ch, &ctx.destination)?;
81-
}
36+
BookItem::Chapter(ref ch) => {
37+
let mut content = ch.content.clone();
38+
39+
// TODO: Port the playpen stuff to not require a file on disk
40+
// content = helpers::playpen::render_playpen(&content, ch.path());
41+
42+
content = utils::render_markdown(&content, ctx.book.get_curly_quotes());
43+
print_content.push_str(&content);
44+
45+
// Update the context with data for this file
46+
47+
let path = match ch.path().to_str() {
48+
Some(p) => p,
49+
None => bail!("Could not convert path to str"),
50+
};
51+
ctx.data.insert("path".to_owned(), json!(path));
52+
53+
ctx.data.insert("content".to_owned(), json!(content));
54+
ctx.data.insert("chapter_title".to_owned(), json!(ch.name));
55+
56+
// FIXME: This place needs a `Path` as well
57+
// ctx.data.insert(
58+
// "path_to_root".to_owned(),
59+
// json!(utils::fs::path_to_root(&ch.path)),
60+
// );
61+
62+
// Render the handlebars template with the data
63+
debug!("[*]: Render template");
64+
let rendered = ctx.handlebars.render("index", &ctx.data)?;
65+
let rendered = self.post_process(rendered);
66+
67+
let filename = Path::new(ch.path()).with_extension("html");
68+
69+
// Write to file
70+
info!("[*] Creating {:?} ✓", filename.display());
71+
ctx.book.write_file(filename, &rendered.into_bytes())?;
72+
73+
if ctx.is_index {
74+
self.render_index(ctx.book, ch, &ctx.destination)?;
8275
}
8376
},
8477
_ => {},

0 commit comments

Comments
 (0)