2018-01-21 22:35:11 +08:00
|
|
|
//! Book preprocessing.
|
|
|
|
|
2018-12-04 07:11:41 +08:00
|
|
|
pub use self::cmd::CmdPreprocessor;
|
2018-05-04 19:41:28 +08:00
|
|
|
pub use self::index::IndexPreprocessor;
|
2018-07-24 01:45:01 +08:00
|
|
|
pub use self::links::LinkPreprocessor;
|
2018-01-08 04:05:57 +08:00
|
|
|
|
2018-12-04 07:11:41 +08:00
|
|
|
mod cmd;
|
2018-05-04 19:41:28 +08:00
|
|
|
mod index;
|
2018-07-24 01:45:01 +08:00
|
|
|
mod links;
|
2018-01-07 23:24:37 +08:00
|
|
|
|
2019-05-26 02:50:41 +08:00
|
|
|
use crate::book::Book;
|
|
|
|
use crate::config::Config;
|
|
|
|
use crate::errors::*;
|
2018-01-08 00:21:46 +08:00
|
|
|
|
2018-01-08 04:05:57 +08:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2018-05-04 19:41:28 +08:00
|
|
|
/// Extra information for a `Preprocessor` to give them more context when
|
2018-01-21 22:35:11 +08:00
|
|
|
/// processing a book.
|
2018-09-16 22:49:52 +08:00
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
2018-01-08 04:05:57 +08:00
|
|
|
pub struct PreprocessorContext {
|
2018-01-21 22:35:11 +08:00
|
|
|
/// The location of the book directory on disk.
|
2018-01-17 17:44:52 +08:00
|
|
|
pub root: PathBuf,
|
2018-01-21 22:35:11 +08:00
|
|
|
/// The book configuration (`book.toml`).
|
2018-01-17 17:44:52 +08:00
|
|
|
pub config: Config,
|
2018-09-10 18:55:58 +08:00
|
|
|
/// The `Renderer` this preprocessor is being used with.
|
|
|
|
pub renderer: String,
|
2018-09-16 14:27:37 +08:00
|
|
|
/// The calling `mdbook` version.
|
|
|
|
pub mdbook_version: String,
|
2018-10-20 11:21:24 +08:00
|
|
|
#[serde(skip)]
|
2018-09-16 14:27:37 +08:00
|
|
|
__non_exhaustive: (),
|
2018-01-17 17:44:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PreprocessorContext {
|
2018-01-21 22:35:11 +08:00
|
|
|
/// Create a new `PreprocessorContext`.
|
2018-09-10 18:55:58 +08:00
|
|
|
pub(crate) fn new(root: PathBuf, config: Config, renderer: String) -> Self {
|
2018-09-16 14:27:37 +08:00
|
|
|
PreprocessorContext {
|
|
|
|
root,
|
|
|
|
config,
|
|
|
|
renderer,
|
2019-05-26 02:50:41 +08:00
|
|
|
mdbook_version: crate::MDBOOK_VERSION.to_string(),
|
2018-09-16 14:27:37 +08:00
|
|
|
__non_exhaustive: (),
|
|
|
|
}
|
2018-01-17 17:44:52 +08:00
|
|
|
}
|
2018-01-08 04:05:57 +08:00
|
|
|
}
|
2018-01-07 23:24:37 +08:00
|
|
|
|
2018-05-04 19:41:28 +08:00
|
|
|
/// An operation which is run immediately after loading a book into memory and
|
2018-01-21 22:35:11 +08:00
|
|
|
/// before it gets rendered.
|
2018-01-08 00:21:46 +08:00
|
|
|
pub trait Preprocessor {
|
2018-01-21 22:35:11 +08:00
|
|
|
/// Get the `Preprocessor`'s name.
|
2018-01-16 06:54:14 +08:00
|
|
|
fn name(&self) -> &str;
|
2018-01-21 22:35:11 +08:00
|
|
|
|
|
|
|
/// Run this `Preprocessor`, allowing it to update the book before it is
|
|
|
|
/// given to a renderer.
|
2018-09-10 18:55:58 +08:00
|
|
|
fn run(&self, ctx: &PreprocessorContext, book: Book) -> Result<Book>;
|
|
|
|
|
|
|
|
/// A hint to `MDBook` whether this preprocessor is compatible with a
|
|
|
|
/// particular renderer.
|
|
|
|
///
|
|
|
|
/// By default, always returns `true`.
|
|
|
|
fn supports_renderer(&self, _renderer: &str) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2018-05-04 19:41:28 +08:00
|
|
|
}
|