2018-01-21 22:35:11 +08:00
|
|
|
//! Book preprocessing.
|
|
|
|
|
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-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
|
|
|
|
2018-01-08 00:21:46 +08:00
|
|
|
use book::Book;
|
2018-01-17 17:44:52 +08:00
|
|
|
use config::Config;
|
2018-01-08 00:21:46 +08:00
|
|
|
use errors::*;
|
|
|
|
|
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-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,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PreprocessorContext {
|
2018-01-21 22:35:11 +08:00
|
|
|
/// Create a new `PreprocessorContext`.
|
|
|
|
pub(crate) fn new(root: PathBuf, config: Config) -> Self {
|
2018-01-17 17:44:52 +08:00
|
|
|
PreprocessorContext { root, config }
|
|
|
|
}
|
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-01-08 04:05:57 +08:00
|
|
|
fn run(&self, ctx: &PreprocessorContext, book: &mut Book) -> Result<()>;
|
2018-05-04 19:41:28 +08:00
|
|
|
}
|