mdBook/src/book/mod.rs

238 lines
7.8 KiB
Rust
Raw Normal View History

//! The internal representation of a book and infrastructure for loading it from
//! disk and building it.
//!
//! For examples on using `MDBook`, consult the [top-level documentation][1].
//!
//! [1]: ../index.html
#![deny(missing_docs)]
mod summary;
mod book;
mod init;
2016-12-23 16:15:32 +08:00
pub use self::book::{load_book, Book, BookItem, BookItems, Chapter};
pub use self::summary::{parse_summary, Link, SectionNumber, Summary, SummaryItem};
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;
use tempdir::TempDir;
2016-04-27 05:04:27 +08:00
2017-11-18 22:07:08 +08:00
use utils;
use renderer::{HtmlHandlebars, Renderer};
use preprocess;
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
/// The object used to manage and build a book.
pub struct MDBook {
/// The book's root directory.
2017-09-30 21:13:00 +08:00
pub root: PathBuf,
/// The configuration used to tweak now a book is built.
pub config: Config,
2016-04-27 05:04:27 +08:00
book: Book,
2016-04-27 05:04:27 +08:00
renderer: Box<Renderer>,
/// The URL used for live reloading when serving up the book.
pub livereload: Option<String>,
2016-04-27 05:04:27 +08:00
}
impl MDBook {
/// 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
// the book.json file is no longer used, so we should emit a warning to
// let people know to migrate to book.toml
if book_root.join("book.json").exists() {
warn!("It appears you are still using book.json for configuration.");
warn!("This format is no longer used, so you should migrate to the");
warn!("book.toml format.");
warn!("Check the user guide for migration information:");
warn!("\thttps://rust-lang-nursery.github.io/mdBook/format/config.html");
}
let config = if config_location.exists() {
debug!("[*] Loading config from {}", config_location.display());
Config::from_disk(&config_location)?
} else {
Config::default()
};
2016-04-27 05:04:27 +08:00
if log_enabled!(::log::Level::Trace) {
for line in format!("Config: {:#?}", config).lines() {
trace!("{}", line);
}
}
MDBook::load_with_config(book_root, config)
}
/// Load a book from its root directory using a custom config.
pub fn load_with_config<P: Into<PathBuf>>(book_root: P, config: Config) -> Result<MDBook> {
let book_root = book_root.into();
let src_dir = book_root.join(&config.book.src);
let book = book::load_book(&src_dir, &config.build)?;
2016-04-27 05:04:27 +08:00
Ok(MDBook {
root: book_root,
config: config,
book: book,
2016-04-27 05:04:27 +08:00
renderer: Box::new(HtmlHandlebars::new()),
livereload: None,
})
2016-04-27 05:04:27 +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;
/// # use mdbook::book::BookItem;
/// # #[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() {
/// 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 {
self.book.iter()
2016-04-27 05:04:27 +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
/// ```
///
/// 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.
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
}
// FIXME: This doesn't belong as part of `MDBook`. It is only used by the HTML renderer
#[doc(hidden)]
pub fn write_file<P: AsRef<Path>>(&self, filename: P, content: &[u8]) -> Result<()> {
let path = self.get_destination().join(filename);
utils::fs::create_file(&path)?
.write_all(content)
.map_err(|e| e.into())
}
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
}
/// Run `rustdoc` tests on the book, linking against the provided libraries.
pub fn test(&mut self, library_paths: Vec<&str>) -> Result<()> {
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();
let temp_dir = TempDir::new("mdbook")?;
2016-04-27 05:04:27 +08:00
for item in self.iter() {
if let BookItem::Chapter(ref ch) = *item {
if !ch.path.as_os_str().is_empty() {
let path = self.source_dir().join(&ch.path);
let base = path.parent()
.ok_or_else(|| String::from("Invalid bookitem path!"))?;
let content = utils::fs::file_to_string(&path)?;
// Parse and expand links
let content = preprocess::links::replace_all(&content, base)?;
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
let path = temp_dir.path().join(&ch.path);
let mut tmpf = utils::fs::create_file(&path)?;
tmpf.write_all(content.as_bytes())?;
let output = Command::new("rustdoc")
.arg(&path)
.arg("--test")
.args(&library_args)
.output()?;
2016-04-27 05:04:27 +08:00
if !output.status.success() {
bail!(ErrorKind::Subprocess(
"Rustdoc returned an error".to_string(),
output
));
2016-04-27 05:04:27 +08:00
}
}
2016-04-27 05:04:27 +08:00
}
}
Ok(())
}
// FIXME: This doesn't belong under `MDBook`, it should really be passed to the renderer directly.
#[doc(hidden)]
pub fn get_destination(&self) -> PathBuf {
self.root.join(&self.config.build.build_dir)
}
/// Get the directory containing this book's source files.
pub fn source_dir(&self) -> PathBuf {
2017-09-30 21:13:00 +08:00
self.root.join(&self.config.book.src)
}
// FIXME: This belongs as part of the `HtmlConfig`.
#[doc(hidden)]
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
}
}