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
match_block_trailing_comma = true
struct_trailing_comma = "Always"
wrap_comments = true
wrap_comments = false
use_try_shorthand = true
report_todo = "Always"

View File

@ -33,6 +33,12 @@ impl Builder {
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> {
// if no custom config provided, try to read it from disk
let cfg = match self.config {

View File

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

View File

@ -18,19 +18,17 @@
//! ## Example
//!
//! ```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)]
//! fn main() {
//! let mut book = MDBook::new("my-book") // Path to root
//! .with_source("src") // Path from root to source directory
//! .with_destination("book") // Path from root to output directory
//! .read_config() // Parse book.toml configuration file
//! .expect("I don't handle configuration file errors, but you should!");
//! book.build().unwrap(); // Render the book
//! }
//! // then create a book which uses that configuration
//! let mut book = Builder::new("my-book").with_config(config).build().unwrap();
//!
//! // and finally, render the book as html
//! book.build().unwrap();
//! ```
//!
//! ## Implementing a new Renderer
@ -41,17 +39,14 @@
//! And then you can swap in your renderer like this:
//!
//! ```no_run
//! # extern crate mdbook;
//! #
//! # use mdbook::MDBook;
//! # use mdbook::renderer::HtmlHandlebars;
//! #
//! # #[allow(unused_variables)]
//! # fn main() {
//! # let your_renderer = HtmlHandlebars::new();
//! #
//! let book = MDBook::new("my-book").set_renderer(Box::new(your_renderer));
//! # }
//! # #![allow(unused_variables)]
//! use mdbook::renderer::HtmlHandlebars;
//! use mdbook::book::Builder;
//!
//! let your_renderer = HtmlHandlebars::new();
//! let book = Builder::new("my-book").set_renderer(Box::new(your_renderer))
//! .build()
//! .unwrap();
//! ```
//! 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.
@ -91,7 +86,6 @@ extern crate serde_json;
extern crate pretty_assertions;
extern crate tempdir;
mod parse;
mod preprocess;
pub mod book;
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<()> {
// 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 {
BookItem::Chapter(_, ref ch) |
BookItem::Affix(ref ch) => {
if ch.path != PathBuf::new() {
BookItem::Chapter(ref ch) => {
let mut content = ch.content.clone();
let path = ctx.book.get_source().join(&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)?;
}
// TODO: Port the playpen stuff to not require a file on disk
// content = helpers::playpen::render_playpen(&content, ch.path());
content = utils::render_markdown(&content, ctx.book.get_curly_quotes());
print_content.push_str(&content);
// 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("content".to_owned(), json!(content));
ctx.data.insert("chapter_title".to_owned(), json!(ch.name));
ctx.data.insert(
"path_to_root".to_owned(),
json!(utils::fs::path_to_root(&ch.path)),
);
// FIXME: This place needs a `Path` as well
// ctx.data.insert(
// "path_to_root".to_owned(),
// json!(utils::fs::path_to_root(&ch.path)),
// );
// Render the handlebars template with the data
debug!("[*]: Render template");
let rendered = ctx.handlebars.render("index", &ctx.data)?;
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
info!("[*] Creating {:?} ✓", filename.display());
@ -79,7 +73,6 @@ impl HtmlHandlebars {
if ctx.is_index {
self.render_index(ctx.book, ch, &ctx.destination)?;
}
}
},
_ => {},
}