2017-11-18 20:01:50 +08:00
|
|
|
mod summary;
|
|
|
|
mod book;
|
2017-11-18 20:41:04 +08:00
|
|
|
mod init;
|
2016-12-23 16:15:32 +08:00
|
|
|
|
2017-11-18 20:01:50 +08:00
|
|
|
pub use self::book::{Book, BookItem, BookItems, Chapter};
|
2017-11-18 20:41:04 +08:00
|
|
|
pub use self::init::BookBuilder;
|
2016-04-27 05:04:27 +08:00
|
|
|
|
|
|
|
use std::path::{Path, PathBuf};
|
2017-09-30 21:54:25 +08:00
|
|
|
use std::io::Write;
|
2016-04-27 05:04:27 +08:00
|
|
|
use std::process::Command;
|
2017-08-07 07:36:29 +08:00
|
|
|
use tempdir::TempDir;
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-11-18 22:07:08 +08:00
|
|
|
use utils;
|
2017-10-03 19:40:23 +08:00
|
|
|
use renderer::{HtmlHandlebars, Renderer};
|
2017-08-07 07:36:29 +08:00
|
|
|
use preprocess;
|
2017-06-25 00:04:57 +08:00
|
|
|
use errors::*;
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-09-30 21:13:00 +08:00
|
|
|
use config::Config;
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
pub struct MDBook {
|
2017-09-30 21:13:00 +08:00
|
|
|
pub root: PathBuf,
|
2017-09-30 21:36:03 +08:00
|
|
|
pub config: Config,
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-11-18 20:01:50 +08:00
|
|
|
book: Book,
|
2016-04-27 05:04:27 +08:00
|
|
|
renderer: Box<Renderer>,
|
|
|
|
|
2017-09-30 21:36:03 +08:00
|
|
|
pub livereload: Option<String>,
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MDBook {
|
2017-11-18 20:01:50 +08:00
|
|
|
/// Load a book from its root directory on disk.
|
|
|
|
pub fn load<P: Into<PathBuf>>(book_root: P) -> Result<MDBook> {
|
|
|
|
let book_root = book_root.into();
|
|
|
|
let config_location = book_root.join("book.toml");
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-11-18 20:01:50 +08:00
|
|
|
let config = if config_location.exists() {
|
|
|
|
Config::from_disk(&config_location)?
|
|
|
|
} else {
|
|
|
|
Config::default()
|
|
|
|
};
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-11-18 20:01:50 +08:00
|
|
|
let src_dir = book_root.join(&config.book.src);
|
|
|
|
let book = book::load_book(&src_dir)?;
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-11-18 20:41:04 +08:00
|
|
|
Ok(MDBook {
|
2017-11-18 20:01:50 +08:00
|
|
|
root: book_root,
|
|
|
|
config: config,
|
|
|
|
book: book,
|
2016-04-27 05:04:27 +08:00
|
|
|
renderer: Box::new(HtmlHandlebars::new()),
|
|
|
|
livereload: None,
|
2017-11-18 20:41:04 +08:00
|
|
|
})
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
|
2017-05-19 19:04:37 +08:00
|
|
|
/// Returns a flat depth-first iterator over the elements of the book,
|
|
|
|
/// it returns an [BookItem enum](bookitem.html):
|
2016-04-27 05:04:27 +08:00
|
|
|
/// `(section: String, bookitem: &BookItem)`
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # extern crate mdbook;
|
|
|
|
/// # use mdbook::MDBook;
|
2017-11-18 20:01:50 +08:00
|
|
|
/// # use mdbook::book::BookItem;
|
2017-06-06 17:58:08 +08:00
|
|
|
/// # #[allow(unused_variables)]
|
2016-04-27 05:04:27 +08:00
|
|
|
/// # fn main() {
|
2017-11-18 22:16:35 +08:00
|
|
|
/// # let book = MDBook::load("mybook").unwrap();
|
2016-04-27 05:04:27 +08:00
|
|
|
/// for item in book.iter() {
|
2017-11-18 20:01:50 +08:00
|
|
|
/// match *item {
|
|
|
|
/// BookItem::Chapter(ref chapter) => {},
|
2017-11-18 22:16:35 +08:00
|
|
|
/// BookItem::Separator => {},
|
2016-04-27 05:04:27 +08:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // would print something like this:
|
|
|
|
/// // 1. Chapter 1
|
|
|
|
/// // 1.1 Sub Chapter
|
|
|
|
/// // 1.2 Sub Chapter
|
|
|
|
/// // 2. Chapter 2
|
|
|
|
/// //
|
|
|
|
/// // etc.
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
pub fn iter(&self) -> BookItems {
|
2017-11-18 20:01:50 +08:00
|
|
|
self.book.iter()
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
|
2017-11-18 20:41:04 +08:00
|
|
|
/// `init()` gives you a `BookBuilder` which you can use to setup a new book
|
|
|
|
/// and its accompanying directory structure.
|
|
|
|
///
|
|
|
|
/// The `BookBuilder` creates some boilerplate files and directories to get
|
|
|
|
/// you started with your book.
|
2016-04-27 05:04:27 +08:00
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// book-test/
|
|
|
|
/// ├── book
|
|
|
|
/// └── src
|
|
|
|
/// ├── chapter_1.md
|
|
|
|
/// └── SUMMARY.md
|
|
|
|
/// ```
|
|
|
|
///
|
2017-11-18 20:41:04 +08:00
|
|
|
/// It uses the path provided as the root directory for your book, then adds
|
|
|
|
/// in a `src/` directory containing a `SUMMARY.md` and `chapter_1.md` file
|
|
|
|
/// to get you started.
|
|
|
|
pub fn init<P: Into<PathBuf>>(book_root: P) -> BookBuilder {
|
|
|
|
BookBuilder::new(book_root)
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
|
2017-11-18 22:07:08 +08:00
|
|
|
/// Tells the renderer to build our book and put it in the build directory.
|
2017-06-25 00:04:57 +08:00
|
|
|
pub fn build(&mut self) -> Result<()> {
|
2016-04-27 05:04:27 +08:00
|
|
|
debug!("[fn]: build");
|
|
|
|
|
2017-11-18 22:07:08 +08:00
|
|
|
let dest = self.get_destination();
|
|
|
|
if dest.exists() {
|
|
|
|
utils::fs::remove_dir_content(&dest).chain_err(|| "Unable to clear output directory")?;
|
|
|
|
}
|
2017-06-05 01:48:41 +08:00
|
|
|
|
2017-06-27 15:08:58 +08:00
|
|
|
self.renderer.render(self)
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
|
2017-06-25 00:04:57 +08:00
|
|
|
pub fn write_file<P: AsRef<Path>>(&self, filename: P, content: &[u8]) -> Result<()> {
|
2017-10-03 19:40:23 +08:00
|
|
|
let path = self.get_destination().join(filename);
|
2017-05-19 05:52:38 +08:00
|
|
|
|
2017-11-18 22:59:45 +08:00
|
|
|
utils::fs::create_file(&path)?
|
|
|
|
.write_all(content)
|
|
|
|
.map_err(|e| e.into())
|
2017-01-01 14:27:38 +08:00
|
|
|
}
|
|
|
|
|
2017-05-19 19:04:37 +08:00
|
|
|
/// Parses the `book.json` file (if it exists) to extract
|
|
|
|
/// the configuration parameters.
|
2016-04-27 05:04:27 +08:00
|
|
|
/// The `book.json` file should be in the root directory of the book.
|
|
|
|
/// The root directory is the one specified when creating a new `MDBook`
|
|
|
|
|
2017-06-25 00:04:57 +08:00
|
|
|
pub fn read_config(mut self) -> Result<Self> {
|
2017-09-30 21:13:00 +08:00
|
|
|
let config_path = self.root.join("book.toml");
|
2017-12-09 17:36:23 +08:00
|
|
|
|
|
|
|
if config_path.exists() {
|
|
|
|
debug!("[*] Loading the config from {}", config_path.display());
|
|
|
|
self.config = Config::from_disk(&config_path)?;
|
|
|
|
} else {
|
|
|
|
self.config = Config::default();
|
|
|
|
}
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-05-19 05:52:38 +08:00
|
|
|
Ok(self)
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
|
2017-11-18 22:16:35 +08:00
|
|
|
/// You can change the default renderer to another one by using this method.
|
|
|
|
/// The only requirement is for your renderer to implement the [Renderer
|
|
|
|
/// trait](../../renderer/renderer/trait.Renderer.html)
|
|
|
|
pub fn set_renderer<R: Renderer + 'static>(mut self, renderer: R) -> Self {
|
|
|
|
self.renderer = Box::new(renderer);
|
2016-04-27 05:04:27 +08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:06:15 +08:00
|
|
|
pub fn test(&mut self, library_paths: Vec<&str>) -> Result<()> {
|
2017-11-18 22:59:45 +08:00
|
|
|
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();
|
2017-08-07 07:36:29 +08:00
|
|
|
let temp_dir = TempDir::new("mdbook")?;
|
2016-04-27 05:04:27 +08:00
|
|
|
for item in self.iter() {
|
2017-11-18 20:01:50 +08:00
|
|
|
if let BookItem::Chapter(ref ch) = *item {
|
2017-08-07 07:36:29 +08:00
|
|
|
if !ch.path.as_os_str().is_empty() {
|
2017-05-19 05:52:38 +08:00
|
|
|
let path = self.get_source().join(&ch.path);
|
2017-11-18 22:59:45 +08:00
|
|
|
let base = path.parent()
|
|
|
|
.ok_or_else(|| String::from("Invalid bookitem path!"))?;
|
2017-08-07 07:36:29 +08:00
|
|
|
let content = utils::fs::file_to_string(&path)?;
|
|
|
|
// Parse and expand links
|
|
|
|
let content = preprocess::links::replace_all(&content, base)?;
|
2017-02-16 11:01:26 +08:00
|
|
|
println!("[*]: Testing file: {:?}", path);
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-09-30 21:13:00 +08:00
|
|
|
// write preprocessed file to tempdir
|
2017-08-07 07:36:29 +08:00
|
|
|
let path = temp_dir.path().join(&ch.path);
|
|
|
|
let mut tmpf = utils::fs::create_file(&path)?;
|
|
|
|
tmpf.write_all(content.as_bytes())?;
|
|
|
|
|
2017-11-18 22:59:45 +08:00
|
|
|
let output = Command::new("rustdoc")
|
|
|
|
.arg(&path)
|
|
|
|
.arg("--test")
|
|
|
|
.args(&library_args)
|
|
|
|
.output()?;
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-02-16 11:01:26 +08:00
|
|
|
if !output.status.success() {
|
2017-11-18 20:41:04 +08:00
|
|
|
bail!(ErrorKind::Subprocess(
|
|
|
|
"Rustdoc returned an error".to_string(),
|
|
|
|
output
|
|
|
|
));
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
2017-02-16 11:01:26 +08:00
|
|
|
}
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-09-30 21:36:03 +08:00
|
|
|
pub fn get_destination(&self) -> PathBuf {
|
2017-11-30 23:26:30 +08:00
|
|
|
self.root.join(&self.config.build.build_dir)
|
2017-05-20 19:56:01 +08:00
|
|
|
}
|
|
|
|
|
2017-09-30 21:36:03 +08:00
|
|
|
pub fn get_source(&self) -> PathBuf {
|
2017-09-30 21:13:00 +08:00
|
|
|
self.root.join(&self.config.book.src)
|
2017-05-20 19:56:01 +08:00
|
|
|
}
|
|
|
|
|
2017-09-30 21:36:03 +08:00
|
|
|
pub fn theme_dir(&self) -> PathBuf {
|
|
|
|
match self.config.html_config().and_then(|h| h.theme) {
|
|
|
|
Some(d) => self.root.join(d),
|
|
|
|
None => self.root.join("theme"),
|
|
|
|
}
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
}
|