Updated the documentation

This commit is contained in:
Michael Bryan 2017-08-03 00:29:28 +08:00
parent 92f0fa0aba
commit 77fe87e8ad
5 changed files with 71 additions and 74 deletions

View File

@ -9,7 +9,7 @@ fn_args_density = "Compressed"
enum_trailing_comma = true enum_trailing_comma = true
match_block_trailing_comma = true match_block_trailing_comma = true
struct_trailing_comma = "Always" struct_trailing_comma = "Always"
wrap_comments = true wrap_comments = false
use_try_shorthand = true use_try_shorthand = true
report_todo = "Always" report_todo = "Always"

View File

@ -33,6 +33,12 @@ impl Builder {
self self
} }
/// Set the renderer to be used.
pub fn set_renderer(mut self, renderer: Box<Renderer>) -> Self {
self.renderer = Some(renderer);
self
}
pub fn build(self) -> Result<MDBook> { pub fn build(self) -> Result<MDBook> {
// if no custom config provided, try to read it from disk // if no custom config provided, try to read it from disk
let cfg = match self.config { let cfg = match self.config {

View File

@ -70,7 +70,7 @@ impl MDBook {
/// # use mdbook::MDBook; /// # use mdbook::MDBook;
/// # use mdbook::loader::BookItem; /// # use mdbook::loader::BookItem;
/// # #[allow(unused_variables)] /// # #[allow(unused_variables)]
/// # fn run() -> ::errors::Result<()> { /// # fn run() -> ::mdbook::errors::Result<()> {
/// # let book = MDBook::new("mybook")?; /// # let book = MDBook::new("mybook")?;
/// for item in book.iter() { /// for item in book.iter() {
/// match *item { /// match *item {
@ -90,7 +90,6 @@ impl MDBook {
/// # } /// # }
/// # fn main() { run().unwrap() } /// # fn main() { run().unwrap() }
/// ``` /// ```
pub fn iter(&self) -> BookItems { pub fn iter(&self) -> BookItems {
self.book.iter() self.book.iter()
} }
@ -271,22 +270,27 @@ impl MDBook {
} }
pub fn test(&mut self, library_paths: Vec<&str>) -> Result<()> { pub fn test(&mut self, library_paths: Vec<&str>) -> Result<()> {
// read in the chapters let library_args: Vec<&str> = (0..library_paths.len())
self.parse_summary().chain_err(|| "Couldn't parse summary")?; .map(|_| "-L")
let library_args: Vec<&str> = (0..library_paths.len()).map(|_| "-L")
.zip(library_paths.into_iter()) .zip(library_paths.into_iter())
.flat_map(|x| vec![x.0, x.1]) .flat_map(|x| vec![x.0, x.1])
.collect(); .collect();
for item in self.iter() { for item in self.iter() {
if let BookItem::Chapter(ref ch) = *item {
let chapter_path = ch.path();
if let BookItem::Chapter(_, ref ch) = *item { if chapter_path == Path::new("") {
if ch.path != PathBuf::new() {
let path = self.get_source().join(&ch.path); let path = self.get_source().join(&chapter_path);
println!("[*]: Testing file: {:?}", path); println!("[*]: Testing file: {:?}", path);
let output = Command::new("rustdoc").arg(&path).arg("--test").args(&library_args).output()?; let output = Command::new("rustdoc")
.arg(&path)
.arg("--test")
.args(&library_args)
.output()?;
if !output.status.success() { if !output.status.success() {
bail!(ErrorKind::Subprocess("Rustdoc returned an error".to_string(), output)); bail!(ErrorKind::Subprocess("Rustdoc returned an error".to_string(), output));

View File

@ -18,19 +18,17 @@
//! ## Example //! ## Example
//! //!
//! ```no_run //! ```no_run
//! extern crate mdbook; //! use mdbook::config::BookConfig;
//! use mdbook::book::Builder;
//! //!
//! use mdbook::MDBook; //! // configure our book
//! let config = BookConfig::new("my-book").with_source("src");
//! //!
//! # #[allow(unused_variables)] //! // then create a book which uses that configuration
//! fn main() { //! let mut book = Builder::new("my-book").with_config(config).build().unwrap();
//! let mut book = MDBook::new("my-book") // Path to root //!
//! .with_source("src") // Path from root to source directory //! // and finally, render the book as html
//! .with_destination("book") // Path from root to output directory //! book.build().unwrap();
//! .read_config() // Parse book.toml configuration file
//! .expect("I don't handle configuration file errors, but you should!");
//! book.build().unwrap(); // Render the book
//! }
//! ``` //! ```
//! //!
//! ## Implementing a new Renderer //! ## Implementing a new Renderer
@ -41,17 +39,14 @@
//! And then you can swap in your renderer like this: //! And then you can swap in your renderer like this:
//! //!
//! ```no_run //! ```no_run
//! # extern crate mdbook; //! # #![allow(unused_variables)]
//! # //! use mdbook::renderer::HtmlHandlebars;
//! # use mdbook::MDBook; //! use mdbook::book::Builder;
//! # use mdbook::renderer::HtmlHandlebars; //!
//! # //! let your_renderer = HtmlHandlebars::new();
//! # #[allow(unused_variables)] //! let book = Builder::new("my-book").set_renderer(Box::new(your_renderer))
//! # fn main() { //! .build()
//! # let your_renderer = HtmlHandlebars::new(); //! .unwrap();
//! #
//! let book = MDBook::new("my-book").set_renderer(Box::new(your_renderer));
//! # }
//! ``` //! ```
//! If you make a renderer, you get the book constructed in form of `Vec<BookItems>` and you get //! If you make a renderer, you get the book constructed in form of `Vec<BookItems>` and you get
//! the book config in a `BookConfig` struct. //! the book config in a `BookConfig` struct.
@ -91,7 +86,6 @@ extern crate serde_json;
extern crate pretty_assertions; extern crate pretty_assertions;
extern crate tempdir; extern crate tempdir;
mod parse;
mod preprocess; mod preprocess;
pub mod book; pub mod book;
pub mod config; pub mod config;

View File

@ -30,47 +30,41 @@ impl HtmlHandlebars {
fn render_item(&self, item: &BookItem, mut ctx: RenderItemContext, print_content: &mut String) -> Result<()> { fn render_item(&self, item: &BookItem, mut ctx: RenderItemContext, print_content: &mut String) -> Result<()> {
// FIXME: This should be made DRY-er and rely less on mutable state // FIXME: This should be made DRY-er and rely less on mutable state
// deferred because we'll probably need to rewrite it anyway when
// renderers are made more pluggable
match *item { match *item {
BookItem::Chapter(_, ref ch) | BookItem::Chapter(ref ch) => {
BookItem::Affix(ref ch) => { let mut content = ch.content.clone();
if ch.path != PathBuf::new() {
let path = ctx.book.get_source().join(&ch.path); // TODO: Port the playpen stuff to not require a file on disk
// content = helpers::playpen::render_playpen(&content, ch.path());
debug!("[*]: Opening file: {:?}", path);
let mut f = File::open(&path)?;
let mut content: String = String::new();
debug!("[*]: Reading file");
f.read_to_string(&mut content)?;
// Parse and expand links
if let Some(p) = path.parent() {
content = preprocess::links::replace_all(&content, p)?;
}
content = utils::render_markdown(&content, ctx.book.get_curly_quotes()); content = utils::render_markdown(&content, ctx.book.get_curly_quotes());
print_content.push_str(&content); print_content.push_str(&content);
// Update the context with data for this file // Update the context with data for this file
let path = ch.path.to_str().ok_or_else(|| {
io::Error::new(io::ErrorKind::Other, "Could not convert path to str")
})?;
let path = match ch.path().to_str() {
Some(p) => p,
None => bail!("Could not convert path to str"),
};
ctx.data.insert("path".to_owned(), json!(path)); ctx.data.insert("path".to_owned(), json!(path));
ctx.data.insert("content".to_owned(), json!(content)); ctx.data.insert("content".to_owned(), json!(content));
ctx.data.insert("chapter_title".to_owned(), json!(ch.name)); ctx.data.insert("chapter_title".to_owned(), json!(ch.name));
ctx.data.insert(
"path_to_root".to_owned(), // FIXME: This place needs a `Path` as well
json!(utils::fs::path_to_root(&ch.path)), // ctx.data.insert(
); // "path_to_root".to_owned(),
// json!(utils::fs::path_to_root(&ch.path)),
// );
// Render the handlebars template with the data // Render the handlebars template with the data
debug!("[*]: Render template"); debug!("[*]: Render template");
let rendered = ctx.handlebars.render("index", &ctx.data)?; let rendered = ctx.handlebars.render("index", &ctx.data)?;
let rendered = self.post_process(rendered); let rendered = self.post_process(rendered);
let filename = Path::new(&ch.path).with_extension("html"); let filename = Path::new(ch.path()).with_extension("html");
// Write to file // Write to file
info!("[*] Creating {:?} ✓", filename.display()); info!("[*] Creating {:?} ✓", filename.display());
@ -79,7 +73,6 @@ impl HtmlHandlebars {
if ctx.is_index { if ctx.is_index {
self.render_index(ctx.book, ch, &ctx.destination)?; self.render_index(ctx.book, ch, &ctx.destination)?;
} }
}
}, },
_ => {}, _ => {},
} }