2015-08-06 18:38:48 +08:00
|
|
|
//! # mdBook
|
|
|
|
//!
|
2017-12-11 15:50:31 +08:00
|
|
|
//! **mdBook** is similar to GitBook but implemented in Rust.
|
2015-08-06 18:38:48 +08:00
|
|
|
//! It offers a command line interface, but can also be used as a regular crate.
|
|
|
|
//!
|
2017-12-11 15:50:31 +08:00
|
|
|
//! This is the API doc, the [user guide] is also available if you want
|
|
|
|
//! information about the command line tool, format, structure etc. It is also
|
|
|
|
//! rendered with mdBook to showcase the features and default theme.
|
2015-08-06 18:38:48 +08:00
|
|
|
//!
|
|
|
|
//! Some reasons why you would want to use the crate (over the cli):
|
|
|
|
//!
|
|
|
|
//! - Integrate mdbook in a current project
|
|
|
|
//! - Extend the capabilities of mdBook
|
|
|
|
//! - Do some processing or test before building your book
|
|
|
|
//! - Write a new Renderer
|
|
|
|
//! - ...
|
|
|
|
//!
|
2017-12-11 08:29:30 +08:00
|
|
|
//! # Examples
|
2015-08-06 18:38:48 +08:00
|
|
|
//!
|
2017-12-11 08:29:30 +08:00
|
|
|
//! If creating a new book from scratch, you'll want to get a `BookBuilder` via
|
|
|
|
//! the `MDBook::init()` method.
|
|
|
|
//!
|
|
|
|
//! ```rust,no_run
|
|
|
|
//! use mdbook::MDBook;
|
|
|
|
//! use mdbook::config::Config;
|
|
|
|
//!
|
|
|
|
//! let root_dir = "/path/to/book/root";
|
|
|
|
//!
|
|
|
|
//! // create a default config and change a couple things
|
|
|
|
//! let mut cfg = Config::default();
|
|
|
|
//! cfg.book.title = Some("My Book".to_string());
|
|
|
|
//! cfg.book.authors.push("Michael-F-Bryan".to_string());
|
2015-08-06 18:38:48 +08:00
|
|
|
//!
|
2017-12-11 08:29:30 +08:00
|
|
|
//! MDBook::init(root_dir)
|
|
|
|
//! .create_gitignore(true)
|
|
|
|
//! .with_config(cfg)
|
|
|
|
//! .build()
|
|
|
|
//! .expect("Book generation failed");
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! You can also load an existing book and build it.
|
|
|
|
//!
|
|
|
|
//! ```rust,no_run
|
2015-08-06 18:38:48 +08:00
|
|
|
//! use mdbook::MDBook;
|
2017-12-11 08:29:30 +08:00
|
|
|
//!
|
|
|
|
//! let root_dir = "/path/to/book/root";
|
|
|
|
//!
|
|
|
|
//! let mut md = MDBook::load(root_dir)
|
|
|
|
//! .expect("Unable to load the book");
|
|
|
|
//! md.build().expect("Building failed");
|
2015-08-06 18:38:48 +08:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! ## Implementing a new Renderer
|
|
|
|
//!
|
2017-12-11 15:50:31 +08:00
|
|
|
//! If you want to create a new renderer for mdBook, the only thing you have to
|
|
|
|
//! do is to implement the [Renderer](renderer/renderer/trait.Renderer.html)
|
|
|
|
//! trait.
|
2015-08-06 18:38:48 +08:00
|
|
|
//!
|
|
|
|
//! And then you can swap in your renderer like this:
|
|
|
|
//!
|
2015-08-06 21:04:27 +08:00
|
|
|
//! ```no_run
|
|
|
|
//! # extern crate mdbook;
|
|
|
|
//! #
|
|
|
|
//! # use mdbook::MDBook;
|
|
|
|
//! # use mdbook::renderer::HtmlHandlebars;
|
|
|
|
//! #
|
2017-06-06 17:58:08 +08:00
|
|
|
//! # #[allow(unused_variables)]
|
2015-08-06 21:04:27 +08:00
|
|
|
//! # fn main() {
|
|
|
|
//! # let your_renderer = HtmlHandlebars::new();
|
|
|
|
//! #
|
2017-11-18 22:16:35 +08:00
|
|
|
//! let mut book = MDBook::load("my-book").unwrap();
|
2018-01-07 22:10:48 +08:00
|
|
|
//! book.with_renderer(your_renderer);
|
2015-08-06 21:04:27 +08:00
|
|
|
//! # }
|
2015-08-06 18:38:48 +08:00
|
|
|
//! ```
|
2015-08-06 21:04:27 +08:00
|
|
|
//!
|
2017-12-11 15:50:31 +08:00
|
|
|
//! 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.
|
|
|
|
//!
|
|
|
|
//! It's your responsability to create the necessary files in the correct
|
|
|
|
//! directories.
|
2015-08-07 03:10:59 +08:00
|
|
|
//!
|
|
|
|
//! ## utils
|
|
|
|
//!
|
2017-12-11 15:50:31 +08:00
|
|
|
//! I have regrouped some useful functions in the [utils](utils/index.html)
|
|
|
|
//! module, like the following function [`utils::fs::create_file(path:
|
|
|
|
//! &Path)`](utils/fs/fn.create_file.html).
|
2015-08-07 03:10:59 +08:00
|
|
|
//!
|
2017-07-10 19:26:43 +08:00
|
|
|
//! This function creates a file and returns it. But before creating the file
|
|
|
|
//! it checks every directory in the path to see if it exists, and if it does
|
|
|
|
//! not it will be created.
|
2015-08-07 03:10:59 +08:00
|
|
|
//!
|
|
|
|
//! Make sure to take a look at it.
|
2017-12-11 15:50:31 +08:00
|
|
|
//!
|
|
|
|
//! [user guide]: https://rust-lang-nursery.github.io/mdBook/
|
2015-08-06 18:38:48 +08:00
|
|
|
|
2017-06-24 23:48:50 +08:00
|
|
|
#[macro_use]
|
|
|
|
extern crate error_chain;
|
2017-07-04 07:04:18 +08:00
|
|
|
extern crate handlebars;
|
2018-01-06 05:03:30 +08:00
|
|
|
extern crate itertools;
|
2017-07-04 07:04:18 +08:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2017-12-11 14:20:05 +08:00
|
|
|
extern crate memchr;
|
2017-07-04 07:04:18 +08:00
|
|
|
extern crate pulldown_cmark;
|
|
|
|
extern crate regex;
|
2017-10-03 19:40:23 +08:00
|
|
|
extern crate serde;
|
2017-02-16 11:34:37 +08:00
|
|
|
#[macro_use]
|
2017-05-19 01:32:08 +08:00
|
|
|
extern crate serde_derive;
|
2017-07-04 07:04:18 +08:00
|
|
|
#[macro_use]
|
2017-06-24 23:48:50 +08:00
|
|
|
extern crate serde_json;
|
2018-01-07 22:10:48 +08:00
|
|
|
extern crate shlex;
|
2017-07-10 19:26:43 +08:00
|
|
|
extern crate tempdir;
|
2017-09-30 20:11:24 +08:00
|
|
|
extern crate toml;
|
2018-01-14 02:38:43 +08:00
|
|
|
extern crate toml_query;
|
2017-07-10 19:26:43 +08:00
|
|
|
|
2017-12-11 12:17:20 +08:00
|
|
|
#[cfg(test)]
|
|
|
|
#[macro_use]
|
|
|
|
extern crate pretty_assertions;
|
|
|
|
|
2018-01-17 06:13:47 +08:00
|
|
|
pub mod preprocess;
|
2017-07-04 07:04:18 +08:00
|
|
|
pub mod book;
|
|
|
|
pub mod config;
|
2015-07-19 06:08:38 +08:00
|
|
|
pub mod renderer;
|
|
|
|
pub mod theme;
|
2015-07-30 21:20:55 +08:00
|
|
|
pub mod utils;
|
2015-07-07 08:56:19 +08:00
|
|
|
|
2015-07-19 06:08:38 +08:00
|
|
|
pub use book::MDBook;
|
|
|
|
pub use book::BookItem;
|
2015-08-06 04:35:26 +08:00
|
|
|
pub use renderer::Renderer;
|
2017-06-24 23:48:50 +08:00
|
|
|
|
|
|
|
/// The error types used through out this crate.
|
|
|
|
pub mod errors {
|
2018-01-07 00:02:23 +08:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2017-06-24 23:48:50 +08:00
|
|
|
error_chain!{
|
|
|
|
foreign_links {
|
|
|
|
Io(::std::io::Error);
|
2017-06-25 00:10:06 +08:00
|
|
|
HandlebarsRender(::handlebars::RenderError);
|
2017-10-13 03:50:33 +08:00
|
|
|
HandlebarsTemplate(Box<::handlebars::TemplateError>);
|
2017-06-25 00:10:06 +08:00
|
|
|
Utf8(::std::string::FromUtf8Error);
|
2017-06-24 23:48:50 +08:00
|
|
|
}
|
2017-06-25 00:04:57 +08:00
|
|
|
|
2018-01-14 02:38:43 +08:00
|
|
|
links {
|
|
|
|
TomlQuery(::toml_query::error::Error, ::toml_query::error::ErrorKind);
|
|
|
|
}
|
|
|
|
|
2017-06-25 00:04:57 +08:00
|
|
|
errors {
|
|
|
|
Subprocess(message: String, output: ::std::process::Output) {
|
|
|
|
description("A subprocess failed")
|
2017-06-28 10:28:50 +08:00
|
|
|
display("{}: {}", message, String::from_utf8_lossy(&output.stdout))
|
2017-06-25 00:04:57 +08:00
|
|
|
}
|
2017-12-11 12:17:20 +08:00
|
|
|
|
|
|
|
ParseError(line: usize, col: usize, message: String) {
|
|
|
|
description("A SUMMARY.md parsing error")
|
|
|
|
display("Error at line {}, column {}: {}", line, col, message)
|
|
|
|
}
|
2018-01-07 00:02:23 +08:00
|
|
|
|
|
|
|
ReservedFilenameError(filename: PathBuf) {
|
|
|
|
description("Reserved Filename")
|
|
|
|
display("{} is reserved for internal use", filename.display())
|
|
|
|
}
|
2017-06-25 00:04:57 +08:00
|
|
|
}
|
2017-06-24 23:48:50 +08:00
|
|
|
}
|
2017-10-13 03:50:33 +08:00
|
|
|
|
|
|
|
// Box to halve the size of Error
|
|
|
|
impl From<::handlebars::TemplateError> for Error {
|
|
|
|
fn from(e: ::handlebars::TemplateError) -> Error {
|
|
|
|
From::from(Box::new(e))
|
|
|
|
}
|
|
|
|
}
|
2017-06-28 10:28:50 +08:00
|
|
|
}
|