2017-12-11 08:24:43 +08:00
|
|
|
//! The internal representation of a book and infrastructure for loading it from
|
|
|
|
//! disk and building it.
|
|
|
|
//!
|
2017-12-11 08:29:30 +08:00
|
|
|
//! For examples on using `MDBook`, consult the [top-level documentation][1].
|
2017-12-11 08:24:43 +08:00
|
|
|
//!
|
2017-12-11 08:29:30 +08:00
|
|
|
//! [1]: ../index.html
|
2017-12-11 08:24:43 +08:00
|
|
|
|
2017-11-18 20:01:50 +08:00
|
|
|
mod book;
|
2017-11-18 20:41:04 +08:00
|
|
|
mod init;
|
2018-07-24 01:45:01 +08:00
|
|
|
mod summary;
|
2016-12-23 16:15:32 +08:00
|
|
|
|
2017-12-11 09:26:11 +08:00
|
|
|
pub use self::book::{load_book, Book, BookItem, BookItems, Chapter};
|
2017-11-18 20:41:04 +08:00
|
|
|
pub use self::init::BookBuilder;
|
2018-07-24 01:45:01 +08:00
|
|
|
pub use self::summary::{parse_summary, Link, SectionNumber, Summary, SummaryItem};
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-09-30 21:54:25 +08:00
|
|
|
use std::io::Write;
|
2018-07-24 01:45:01 +08:00
|
|
|
use std::path::PathBuf;
|
2016-04-27 05:04:27 +08:00
|
|
|
use std::process::Command;
|
2019-05-07 02:20:58 +08:00
|
|
|
use std::string::ToString;
|
2018-03-27 07:47:37 +08:00
|
|
|
use tempfile::Builder as TempFileBuilder;
|
2018-01-07 22:10:48 +08:00
|
|
|
use toml::Value;
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2019-05-26 02:50:41 +08:00
|
|
|
use crate::errors::*;
|
|
|
|
use crate::preprocess::{
|
2018-12-04 07:10:09 +08:00
|
|
|
CmdPreprocessor, IndexPreprocessor, LinkPreprocessor, Preprocessor, PreprocessorContext,
|
|
|
|
};
|
2019-08-30 18:20:53 +08:00
|
|
|
use crate::renderer::{CmdRenderer, HtmlHandlebars, MarkdownRenderer, RenderContext, Renderer};
|
2019-05-26 02:50:41 +08:00
|
|
|
use crate::utils;
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2019-11-11 19:42:24 +08:00
|
|
|
use crate::config::{Config, RustEdition};
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2017-12-11 08:24:43 +08:00
|
|
|
/// The object used to manage and build a book.
|
2017-05-19 05:52:38 +08:00
|
|
|
pub struct MDBook {
|
2017-12-11 08:24:43 +08:00
|
|
|
/// The book's root directory.
|
2017-09-30 21:13:00 +08:00
|
|
|
pub root: PathBuf,
|
2017-12-11 08:24:43 +08:00
|
|
|
/// The configuration used to tweak now a book is built.
|
2017-09-30 21:36:03 +08:00
|
|
|
pub config: Config,
|
2018-01-07 22:10:48 +08:00
|
|
|
/// A representation of the book's contents in memory.
|
|
|
|
pub book: Book,
|
2019-05-07 04:50:34 +08:00
|
|
|
renderers: Vec<Box<dyn Renderer>>,
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2018-01-07 23:24:37 +08:00
|
|
|
/// List of pre-processors to be run on the book
|
2019-05-07 04:50:34 +08:00
|
|
|
preprocessors: Vec<Box<dyn Preprocessor>>,
|
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-12-13 08:55:37 +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:");
|
2019-10-29 21:04:16 +08:00
|
|
|
warn!("\thttps://rust-lang.github.io/mdBook/format/config.html");
|
2017-12-13 08:55:37 +08:00
|
|
|
}
|
|
|
|
|
2018-01-14 02:38:43 +08:00
|
|
|
let mut config = if config_location.exists() {
|
2018-01-23 01:28:37 +08:00
|
|
|
debug!("Loading config from {}", config_location.display());
|
2017-11-18 20:01:50 +08:00
|
|
|
Config::from_disk(&config_location)?
|
|
|
|
} else {
|
|
|
|
Config::default()
|
|
|
|
};
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2018-01-14 02:38:43 +08:00
|
|
|
config.update_from_env();
|
|
|
|
|
2019-05-31 00:12:33 +08:00
|
|
|
if log_enabled!(log::Level::Trace) {
|
2017-12-11 08:42:36 +08:00
|
|
|
for line in format!("Config: {:#?}", config).lines() {
|
|
|
|
trace!("{}", line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-11 07:32:35 +08:00
|
|
|
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> {
|
2018-01-07 22:10:48 +08:00
|
|
|
let root = book_root.into();
|
2017-12-11 07:32:35 +08:00
|
|
|
|
2018-01-07 22:10:48 +08:00
|
|
|
let src_dir = root.join(&config.book.src);
|
2017-12-11 07:32:35 +08:00
|
|
|
let book = book::load_book(&src_dir, &config.build)?;
|
2018-01-07 22:10:48 +08:00
|
|
|
|
|
|
|
let renderers = determine_renderers(&config);
|
2018-01-16 06:54:14 +08:00
|
|
|
let preprocessors = determine_preprocessors(&config)?;
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2018-01-08 04:05:57 +08:00
|
|
|
Ok(MDBook {
|
2018-01-07 22:10:48 +08:00
|
|
|
root,
|
|
|
|
config,
|
|
|
|
book,
|
|
|
|
renderers,
|
2018-01-08 04:05:57 +08:00
|
|
|
preprocessors,
|
|
|
|
})
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
|
2019-03-05 03:44:00 +08:00
|
|
|
/// Load a book from its root directory using a custom config and a custom summary.
|
|
|
|
pub fn load_with_config_and_summary<P: Into<PathBuf>>(
|
|
|
|
book_root: P,
|
|
|
|
config: Config,
|
2019-05-05 22:57:43 +08:00
|
|
|
summary: Summary,
|
2019-03-05 03:44:00 +08:00
|
|
|
) -> Result<MDBook> {
|
|
|
|
let root = book_root.into();
|
|
|
|
|
|
|
|
let src_dir = root.join(&config.book.src);
|
|
|
|
let book = book::load_book_from_disk(&summary, &src_dir)?;
|
|
|
|
|
|
|
|
let renderers = determine_renderers(&config);
|
2018-01-16 06:54:14 +08:00
|
|
|
let preprocessors = determine_preprocessors(&config)?;
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2018-01-08 04:05:57 +08:00
|
|
|
Ok(MDBook {
|
2018-01-07 22:10:48 +08:00
|
|
|
root,
|
|
|
|
config,
|
|
|
|
book,
|
|
|
|
renderers,
|
2018-01-08 04:05:57 +08:00
|
|
|
preprocessors,
|
|
|
|
})
|
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
|
|
|
|
/// # 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.
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2019-05-07 04:50:34 +08:00
|
|
|
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.
|
2018-01-07 22:10:48 +08:00
|
|
|
pub fn build(&self) -> Result<()> {
|
2018-01-23 01:28:37 +08:00
|
|
|
info!("Book building has started");
|
2016-04-27 05:04:27 +08:00
|
|
|
|
2018-09-10 18:55:58 +08:00
|
|
|
for renderer in &self.renderers {
|
|
|
|
self.execute_build_process(&**renderer)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Run the entire build process for a particular `Renderer`.
|
2019-05-07 04:50:34 +08:00
|
|
|
fn execute_build_process(&self, renderer: &dyn Renderer) -> Result<()> {
|
2018-01-08 03:08:31 +08:00
|
|
|
let mut preprocessed_book = self.book.clone();
|
2018-12-04 07:10:09 +08:00
|
|
|
let preprocess_ctx = PreprocessorContext::new(
|
|
|
|
self.root.clone(),
|
|
|
|
self.config.clone(),
|
|
|
|
renderer.name().to_string(),
|
|
|
|
);
|
2018-01-08 03:08:31 +08:00
|
|
|
|
|
|
|
for preprocessor in &self.preprocessors {
|
2018-09-10 18:55:58 +08:00
|
|
|
if preprocessor_should_run(&**preprocessor, renderer, &self.config) {
|
|
|
|
debug!("Running the {} preprocessor.", preprocessor.name());
|
2018-12-04 07:10:09 +08:00
|
|
|
preprocessed_book = preprocessor.run(&preprocess_ctx, preprocessed_book)?;
|
2018-09-10 18:55:58 +08:00
|
|
|
}
|
2018-01-08 03:08:31 +08:00
|
|
|
}
|
|
|
|
|
2018-09-10 18:55:58 +08:00
|
|
|
info!("Running the {} backend", renderer.name());
|
|
|
|
self.render(&preprocessed_book, renderer)?;
|
2017-06-05 01:48:41 +08:00
|
|
|
|
2018-01-07 22:10:48 +08:00
|
|
|
Ok(())
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
|
2019-05-07 04:50:34 +08:00
|
|
|
fn render(&self, preprocessed_book: &Book, renderer: &dyn Renderer) -> Result<()> {
|
2018-01-07 22:10:48 +08:00
|
|
|
let name = renderer.name();
|
|
|
|
let build_dir = self.build_dir_for(name);
|
2017-05-19 05:52:38 +08:00
|
|
|
|
2018-01-07 22:10:48 +08:00
|
|
|
let render_context = RenderContext::new(
|
|
|
|
self.root.clone(),
|
2018-01-08 03:08:31 +08:00
|
|
|
preprocessed_book.clone(),
|
2018-01-07 22:10:48 +08:00
|
|
|
self.config.clone(),
|
|
|
|
build_dir,
|
|
|
|
);
|
|
|
|
|
|
|
|
renderer
|
|
|
|
.render(&render_context)
|
|
|
|
.chain_err(|| "Rendering failed")
|
2017-01-01 14:27:38 +08:00
|
|
|
}
|
|
|
|
|
2017-11-18 22:16:35 +08:00
|
|
|
/// You can change the default renderer to another one by using this method.
|
2018-01-17 07:02:50 +08:00
|
|
|
/// The only requirement is for your renderer to implement the [`Renderer`
|
|
|
|
/// trait](../renderer/trait.Renderer.html)
|
2018-01-07 22:10:48 +08:00
|
|
|
pub fn with_renderer<R: Renderer + 'static>(&mut self, renderer: R) -> &mut Self {
|
|
|
|
self.renderers.push(Box::new(renderer));
|
2016-04-27 05:04:27 +08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-01-17 07:02:50 +08:00
|
|
|
/// Register a [`Preprocessor`](../preprocess/trait.Preprocessor.html) to be used when rendering the book.
|
2019-05-04 01:59:58 +08:00
|
|
|
pub fn with_preprocessor<P: Preprocessor + 'static>(&mut self, preprocessor: P) -> &mut Self {
|
2018-01-07 23:24:37 +08:00
|
|
|
self.preprocessors.push(Box::new(preprocessor));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-12-11 08:24:43 +08:00
|
|
|
/// Run `rustdoc` tests on the book, linking against the provided libraries.
|
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-12-10 20:13:46 +08:00
|
|
|
|
2018-07-26 01:17:17 +08:00
|
|
|
let temp_dir = TempFileBuilder::new().prefix("mdbook-").tempdir()?;
|
2017-12-10 20:13:46 +08:00
|
|
|
|
2018-09-10 18:55:58 +08:00
|
|
|
// FIXME: Is "test" the proper renderer name to use here?
|
2018-12-04 07:10:09 +08:00
|
|
|
let preprocess_context =
|
|
|
|
PreprocessorContext::new(self.root.clone(), self.config.clone(), "test".to_string());
|
2018-01-08 00:21:46 +08:00
|
|
|
|
2018-09-10 18:55:58 +08:00
|
|
|
let book = LinkPreprocessor::new().run(&preprocess_context, self.book.clone())?;
|
2018-07-26 01:17:17 +08:00
|
|
|
// Index Preprocessor is disabled so that chapter paths continue to point to the
|
|
|
|
// actual markdown files.
|
2018-01-08 00:21:46 +08:00
|
|
|
|
2018-09-10 18:55:58 +08:00
|
|
|
for item in book.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-12-11 08:24:43 +08:00
|
|
|
let path = self.source_dir().join(&ch.path);
|
2018-01-23 01:28:37 +08:00
|
|
|
info!("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)?;
|
2019-03-01 09:44:55 +08:00
|
|
|
tmpf.write_all(ch.content.as_bytes())?;
|
2017-08-07 07:36:29 +08:00
|
|
|
|
2019-11-11 19:42:24 +08:00
|
|
|
let mut cmd = Command::new("rustdoc");
|
|
|
|
|
|
|
|
cmd.arg(&path).arg("--test").args(&library_args);
|
|
|
|
|
|
|
|
if let Some(html_cfg) = self.config.html_config() {
|
|
|
|
if html_cfg.playpen.edition == RustEdition::E2018 {
|
|
|
|
cmd.args(&["--edition", "2018"]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let output = cmd.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(())
|
|
|
|
}
|
|
|
|
|
2018-01-07 22:10:48 +08:00
|
|
|
/// The logic for determining where a backend should put its build
|
|
|
|
/// artefacts.
|
|
|
|
///
|
|
|
|
/// If there is only 1 renderer, put it in the directory pointed to by the
|
|
|
|
/// `build.build_dir` key in `Config`. If there is more than one then the
|
|
|
|
/// renderer gets its own directory within the main build dir.
|
|
|
|
///
|
|
|
|
/// i.e. If there were only one renderer (in this case, the HTML renderer):
|
|
|
|
///
|
|
|
|
/// - build/
|
|
|
|
/// - index.html
|
|
|
|
/// - ...
|
|
|
|
///
|
|
|
|
/// Otherwise if there are multiple:
|
|
|
|
///
|
|
|
|
/// - build/
|
|
|
|
/// - epub/
|
|
|
|
/// - my_awesome_book.epub
|
|
|
|
/// - html/
|
|
|
|
/// - index.html
|
|
|
|
/// - ...
|
|
|
|
/// - latex/
|
|
|
|
/// - my_awesome_book.tex
|
|
|
|
///
|
|
|
|
pub fn build_dir_for(&self, backend_name: &str) -> PathBuf {
|
|
|
|
let build_dir = self.root.join(&self.config.build.build_dir);
|
|
|
|
|
|
|
|
if self.renderers.len() <= 1 {
|
|
|
|
build_dir
|
|
|
|
} else {
|
|
|
|
build_dir.join(backend_name)
|
|
|
|
}
|
2017-05-20 19:56:01 +08:00
|
|
|
}
|
|
|
|
|
2017-12-11 08:24:43 +08:00
|
|
|
/// 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)
|
2017-05-20 19:56:01 +08:00
|
|
|
}
|
|
|
|
|
2018-03-14 23:27:56 +08:00
|
|
|
/// Get the directory containing the theme resources for the book.
|
2017-09-30 21:36:03 +08:00
|
|
|
pub fn theme_dir(&self) -> PathBuf {
|
2018-03-14 23:27:56 +08:00
|
|
|
self.config
|
|
|
|
.html_config()
|
|
|
|
.unwrap_or_default()
|
|
|
|
.theme_dir(&self.root)
|
2016-04-27 05:04:27 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-07 22:10:48 +08:00
|
|
|
|
|
|
|
/// Look at the `Config` and try to figure out what renderers to use.
|
2019-05-07 04:50:34 +08:00
|
|
|
fn determine_renderers(config: &Config) -> Vec<Box<dyn Renderer>> {
|
2019-06-20 21:12:56 +08:00
|
|
|
let mut renderers = Vec::new();
|
2018-01-07 22:10:48 +08:00
|
|
|
|
2019-05-07 02:20:58 +08:00
|
|
|
if let Some(output_table) = config.get("output").and_then(Value::as_table) {
|
2019-06-20 21:12:56 +08:00
|
|
|
renderers.extend(output_table.iter().map(|(key, table)| {
|
2018-01-07 22:10:48 +08:00
|
|
|
if key == "html" {
|
2019-06-20 21:12:56 +08:00
|
|
|
Box::new(HtmlHandlebars::new()) as Box<dyn Renderer>
|
2019-08-30 18:20:53 +08:00
|
|
|
} else if key == "markdown" {
|
|
|
|
Box::new(MarkdownRenderer::new()) as Box<dyn Renderer>
|
2018-01-07 22:10:48 +08:00
|
|
|
} else {
|
2019-06-20 21:12:56 +08:00
|
|
|
interpret_custom_renderer(key, table)
|
2018-01-07 22:10:48 +08:00
|
|
|
}
|
2019-06-20 21:12:56 +08:00
|
|
|
}));
|
2018-01-07 22:10:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we couldn't find anything, add the HTML renderer as a default
|
|
|
|
if renderers.is_empty() {
|
|
|
|
renderers.push(Box::new(HtmlHandlebars::new()));
|
|
|
|
}
|
|
|
|
|
|
|
|
renderers
|
|
|
|
}
|
|
|
|
|
2019-05-07 04:50:34 +08:00
|
|
|
fn default_preprocessors() -> Vec<Box<dyn Preprocessor>> {
|
2018-05-04 19:41:28 +08:00
|
|
|
vec![
|
|
|
|
Box::new(LinkPreprocessor::new()),
|
|
|
|
Box::new(IndexPreprocessor::new()),
|
|
|
|
]
|
2018-01-17 06:25:51 +08:00
|
|
|
}
|
|
|
|
|
2019-05-07 04:50:34 +08:00
|
|
|
fn is_default_preprocessor(pre: &dyn Preprocessor) -> bool {
|
2018-09-10 18:55:58 +08:00
|
|
|
let name = pre.name();
|
|
|
|
name == LinkPreprocessor::NAME || name == IndexPreprocessor::NAME
|
|
|
|
}
|
|
|
|
|
2018-01-08 00:40:17 +08:00
|
|
|
/// Look at the `MDBook` and try to figure out what preprocessors to run.
|
2019-05-07 04:50:34 +08:00
|
|
|
fn determine_preprocessors(config: &Config) -> Result<Vec<Box<dyn Preprocessor>>> {
|
2018-09-19 23:13:25 +08:00
|
|
|
let mut preprocessors = Vec::new();
|
|
|
|
|
|
|
|
if config.build.use_default_preprocessors {
|
|
|
|
preprocessors.extend(default_preprocessors());
|
|
|
|
}
|
|
|
|
|
2019-05-07 02:20:58 +08:00
|
|
|
if let Some(preprocessor_table) = config.get("preprocessor").and_then(Value::as_table) {
|
2018-09-19 23:13:25 +08:00
|
|
|
for key in preprocessor_table.keys() {
|
|
|
|
match key.as_ref() {
|
2018-12-04 07:10:09 +08:00
|
|
|
"links" => preprocessors.push(Box::new(LinkPreprocessor::new())),
|
|
|
|
"index" => preprocessors.push(Box::new(IndexPreprocessor::new())),
|
2018-09-19 23:13:25 +08:00
|
|
|
name => preprocessors.push(interpret_custom_preprocessor(
|
|
|
|
name,
|
|
|
|
&preprocessor_table[name],
|
|
|
|
)),
|
|
|
|
}
|
2018-01-08 00:40:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 06:54:14 +08:00
|
|
|
Ok(preprocessors)
|
2018-01-08 00:21:46 +08:00
|
|
|
}
|
|
|
|
|
2018-12-04 07:10:09 +08:00
|
|
|
fn interpret_custom_preprocessor(key: &str, table: &Value) -> Box<CmdPreprocessor> {
|
2018-09-19 23:13:25 +08:00
|
|
|
let command = table
|
|
|
|
.get("command")
|
2019-05-07 02:20:58 +08:00
|
|
|
.and_then(Value::as_str)
|
|
|
|
.map(ToString::to_string)
|
2018-09-19 23:13:25 +08:00
|
|
|
.unwrap_or_else(|| format!("mdbook-{}", key));
|
|
|
|
|
|
|
|
Box::new(CmdPreprocessor::new(key.to_string(), command.to_string()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn interpret_custom_renderer(key: &str, table: &Value) -> Box<CmdRenderer> {
|
2018-01-07 22:10:48 +08:00
|
|
|
// look for the `command` field, falling back to using the key
|
|
|
|
// prepended by "mdbook-"
|
|
|
|
let table_dot_command = table
|
|
|
|
.get("command")
|
2019-05-07 02:20:58 +08:00
|
|
|
.and_then(Value::as_str)
|
|
|
|
.map(ToString::to_string);
|
2018-01-07 22:10:48 +08:00
|
|
|
|
2018-12-04 07:10:09 +08:00
|
|
|
let command = table_dot_command.unwrap_or_else(|| format!("mdbook-{}", key));
|
2018-01-07 22:10:48 +08:00
|
|
|
|
|
|
|
Box::new(CmdRenderer::new(key.to_string(), command.to_string()))
|
|
|
|
}
|
|
|
|
|
2018-09-10 18:55:58 +08:00
|
|
|
/// Check whether we should run a particular `Preprocessor` in combination
|
|
|
|
/// with the renderer, falling back to `Preprocessor::supports_renderer()`
|
|
|
|
/// method if the user doesn't say anything.
|
|
|
|
///
|
|
|
|
/// The `build.use-default-preprocessors` config option can be used to ensure
|
|
|
|
/// default preprocessors always run if they support the renderer.
|
2019-05-07 04:50:34 +08:00
|
|
|
fn preprocessor_should_run(
|
|
|
|
preprocessor: &dyn Preprocessor,
|
|
|
|
renderer: &dyn Renderer,
|
|
|
|
cfg: &Config,
|
|
|
|
) -> bool {
|
2018-09-10 18:55:58 +08:00
|
|
|
// default preprocessors should be run by default (if supported)
|
|
|
|
if cfg.build.use_default_preprocessors && is_default_preprocessor(preprocessor) {
|
|
|
|
return preprocessor.supports_renderer(renderer.name());
|
|
|
|
}
|
|
|
|
|
|
|
|
let key = format!("preprocessor.{}.renderers", preprocessor.name());
|
|
|
|
let renderer_name = renderer.name();
|
|
|
|
|
|
|
|
if let Some(Value::Array(ref explicit_renderers)) = cfg.get(&key) {
|
2018-12-04 07:10:09 +08:00
|
|
|
return explicit_renderers
|
|
|
|
.iter()
|
2019-05-07 02:20:58 +08:00
|
|
|
.filter_map(Value::as_str)
|
2018-09-10 18:55:58 +08:00
|
|
|
.any(|name| name == renderer_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
preprocessor.supports_renderer(renderer_name)
|
|
|
|
}
|
|
|
|
|
2018-01-07 22:10:48 +08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-05-07 02:20:58 +08:00
|
|
|
use std::str::FromStr;
|
2018-01-07 22:10:48 +08:00
|
|
|
use toml::value::{Table, Value};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn config_defaults_to_html_renderer_if_empty() {
|
|
|
|
let cfg = Config::default();
|
|
|
|
|
|
|
|
// make sure we haven't got anything in the `output` table
|
|
|
|
assert!(cfg.get("output").is_none());
|
|
|
|
|
|
|
|
let got = determine_renderers(&cfg);
|
|
|
|
|
|
|
|
assert_eq!(got.len(), 1);
|
|
|
|
assert_eq!(got[0].name(), "html");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_a_random_renderer_to_the_config() {
|
|
|
|
let mut cfg = Config::default();
|
|
|
|
cfg.set("output.random", Table::new()).unwrap();
|
|
|
|
|
|
|
|
let got = determine_renderers(&cfg);
|
|
|
|
|
|
|
|
assert_eq!(got.len(), 1);
|
|
|
|
assert_eq!(got[0].name(), "random");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_a_random_renderer_with_custom_command_to_the_config() {
|
|
|
|
let mut cfg = Config::default();
|
|
|
|
|
|
|
|
let mut table = Table::new();
|
|
|
|
table.insert("command".to_string(), Value::String("false".to_string()));
|
|
|
|
cfg.set("output.random", table).unwrap();
|
|
|
|
|
|
|
|
let got = determine_renderers(&cfg);
|
|
|
|
|
|
|
|
assert_eq!(got.len(), 1);
|
|
|
|
assert_eq!(got[0].name(), "random");
|
|
|
|
}
|
2018-01-16 06:54:14 +08:00
|
|
|
|
|
|
|
#[test]
|
2018-05-04 19:41:28 +08:00
|
|
|
fn config_defaults_to_link_and_index_preprocessor_if_not_set() {
|
2018-01-16 06:54:14 +08:00
|
|
|
let cfg = Config::default();
|
|
|
|
|
2018-09-10 18:55:58 +08:00
|
|
|
// make sure we haven't got anything in the `preprocessor` table
|
|
|
|
assert!(cfg.get("preprocessor").is_none());
|
2018-01-16 06:54:14 +08:00
|
|
|
|
|
|
|
let got = determine_preprocessors(&cfg);
|
|
|
|
|
|
|
|
assert!(got.is_ok());
|
2018-05-04 19:41:28 +08:00
|
|
|
assert_eq!(got.as_ref().unwrap().len(), 2);
|
2018-01-16 06:54:14 +08:00
|
|
|
assert_eq!(got.as_ref().unwrap()[0].name(), "links");
|
2018-05-04 19:41:28 +08:00
|
|
|
assert_eq!(got.as_ref().unwrap()[1].name(), "index");
|
2018-01-16 06:54:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-09-10 18:55:58 +08:00
|
|
|
fn use_default_preprocessors_works() {
|
|
|
|
let mut cfg = Config::default();
|
|
|
|
cfg.build.use_default_preprocessors = false;
|
|
|
|
|
|
|
|
let got = determine_preprocessors(&cfg).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(got.len(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-09-19 23:13:25 +08:00
|
|
|
fn can_determine_third_party_preprocessors() {
|
2019-06-01 00:01:02 +08:00
|
|
|
let cfg_str = r#"
|
2018-01-16 06:54:14 +08:00
|
|
|
[book]
|
|
|
|
title = "Some Book"
|
|
|
|
|
2018-09-10 18:55:58 +08:00
|
|
|
[preprocessor.random]
|
|
|
|
|
2018-01-16 06:54:14 +08:00
|
|
|
[build]
|
|
|
|
build-dir = "outputs"
|
|
|
|
create-missing = false
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let cfg = Config::from_str(cfg_str).unwrap();
|
|
|
|
|
2018-09-10 18:55:58 +08:00
|
|
|
// make sure the `preprocessor.random` table exists
|
|
|
|
assert!(cfg.get_preprocessor("random").is_some());
|
2018-01-16 06:54:14 +08:00
|
|
|
|
2018-09-19 23:13:25 +08:00
|
|
|
let got = determine_preprocessors(&cfg).unwrap();
|
|
|
|
|
|
|
|
assert!(got.into_iter().any(|p| p.name() == "random"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn preprocessors_can_provide_their_own_commands() {
|
|
|
|
let cfg_str = r#"
|
|
|
|
[preprocessor.random]
|
|
|
|
command = "python random.py"
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let cfg = Config::from_str(cfg_str).unwrap();
|
|
|
|
|
|
|
|
// make sure the `preprocessor.random` table exists
|
|
|
|
let random = cfg.get_preprocessor("random").unwrap();
|
2018-12-04 07:10:09 +08:00
|
|
|
let random = interpret_custom_preprocessor("random", &Value::Table(random.clone()));
|
2018-01-16 06:54:14 +08:00
|
|
|
|
2018-09-19 23:13:25 +08:00
|
|
|
assert_eq!(random.cmd(), "python random.py");
|
2018-01-16 06:54:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-09-10 18:55:58 +08:00
|
|
|
fn config_respects_preprocessor_selection() {
|
2019-06-01 00:01:02 +08:00
|
|
|
let cfg_str = r#"
|
2018-09-10 18:55:58 +08:00
|
|
|
[preprocessor.links]
|
|
|
|
renderers = ["html"]
|
2018-01-16 06:54:14 +08:00
|
|
|
"#;
|
|
|
|
|
|
|
|
let cfg = Config::from_str(cfg_str).unwrap();
|
|
|
|
|
2018-09-10 18:55:58 +08:00
|
|
|
// double-check that we can access preprocessor.links.renderers[0]
|
2018-12-04 07:10:09 +08:00
|
|
|
let html = cfg
|
|
|
|
.get_preprocessor("links")
|
2018-09-10 18:55:58 +08:00
|
|
|
.and_then(|links| links.get("renderers"))
|
2019-05-07 02:20:58 +08:00
|
|
|
.and_then(Value::as_array)
|
2018-09-10 18:55:58 +08:00
|
|
|
.and_then(|renderers| renderers.get(0))
|
2019-05-07 02:20:58 +08:00
|
|
|
.and_then(Value::as_str)
|
2018-09-10 18:55:58 +08:00
|
|
|
.unwrap();
|
|
|
|
assert_eq!(html, "html");
|
|
|
|
let html_renderer = HtmlHandlebars::default();
|
|
|
|
let pre = LinkPreprocessor::new();
|
|
|
|
|
|
|
|
let should_run = preprocessor_should_run(&pre, &html_renderer, &cfg);
|
|
|
|
assert!(should_run);
|
|
|
|
}
|
2018-01-16 06:54:14 +08:00
|
|
|
|
2018-09-10 18:55:58 +08:00
|
|
|
struct BoolPreprocessor(bool);
|
|
|
|
impl Preprocessor for BoolPreprocessor {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"bool-preprocessor"
|
|
|
|
}
|
2018-01-16 06:54:14 +08:00
|
|
|
|
2018-09-10 18:55:58 +08:00
|
|
|
fn run(&self, _ctx: &PreprocessorContext, _book: Book) -> Result<Book> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn supports_renderer(&self, _renderer: &str) -> bool {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn preprocessor_should_run_falls_back_to_supports_renderer_method() {
|
|
|
|
let cfg = Config::default();
|
|
|
|
let html = HtmlHandlebars::new();
|
|
|
|
|
|
|
|
let should_be = true;
|
|
|
|
let got = preprocessor_should_run(&BoolPreprocessor(should_be), &html, &cfg);
|
|
|
|
assert_eq!(got, should_be);
|
|
|
|
|
|
|
|
let should_be = false;
|
|
|
|
let got = preprocessor_should_run(&BoolPreprocessor(should_be), &html, &cfg);
|
|
|
|
assert_eq!(got, should_be);
|
2018-01-16 06:54:14 +08:00
|
|
|
}
|
2018-01-07 22:10:48 +08:00
|
|
|
}
|