]> git.proxmox.com Git - rustc.git/blame - vendor/mdbook/src/preprocess/mod.rs
New upstream version 1.56.0~beta.4+dfsg1
[rustc.git] / vendor / mdbook / src / preprocess / mod.rs
CommitLineData
2c00a5a8
XL
1//! Book preprocessing.
2
9fa01778
XL
3pub use self::cmd::CmdPreprocessor;
4pub use self::index::IndexPreprocessor;
2c00a5a8
XL
5pub use self::links::LinkPreprocessor;
6
9fa01778
XL
7mod cmd;
8mod index;
2c00a5a8
XL
9mod links;
10
dc9dc135
XL
11use crate::book::Book;
12use crate::config::Config;
13use crate::errors::*;
2c00a5a8 14
94222f64
XL
15use std::cell::RefCell;
16use std::collections::HashMap;
2c00a5a8
XL
17use std::path::PathBuf;
18
9fa01778 19/// Extra information for a `Preprocessor` to give them more context when
2c00a5a8 20/// processing a book.
9fa01778 21#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
2c00a5a8
XL
22pub struct PreprocessorContext {
23 /// The location of the book directory on disk.
24 pub root: PathBuf,
25 /// The book configuration (`book.toml`).
26 pub config: Config,
9fa01778
XL
27 /// The `Renderer` this preprocessor is being used with.
28 pub renderer: String,
29 /// The calling `mdbook` version.
30 pub mdbook_version: String,
31 #[serde(skip)]
94222f64
XL
32 pub(crate) chapter_titles: RefCell<HashMap<PathBuf, String>>,
33 #[serde(skip)]
9fa01778 34 __non_exhaustive: (),
2c00a5a8
XL
35}
36
37impl PreprocessorContext {
38 /// Create a new `PreprocessorContext`.
9fa01778
XL
39 pub(crate) fn new(root: PathBuf, config: Config, renderer: String) -> Self {
40 PreprocessorContext {
41 root,
42 config,
43 renderer,
dc9dc135 44 mdbook_version: crate::MDBOOK_VERSION.to_string(),
94222f64 45 chapter_titles: RefCell::new(HashMap::new()),
9fa01778
XL
46 __non_exhaustive: (),
47 }
2c00a5a8
XL
48 }
49}
50
9fa01778 51/// An operation which is run immediately after loading a book into memory and
2c00a5a8
XL
52/// before it gets rendered.
53pub trait Preprocessor {
54 /// Get the `Preprocessor`'s name.
55 fn name(&self) -> &str;
56
57 /// Run this `Preprocessor`, allowing it to update the book before it is
58 /// given to a renderer.
9fa01778
XL
59 fn run(&self, ctx: &PreprocessorContext, book: Book) -> Result<Book>;
60
61 /// A hint to `MDBook` whether this preprocessor is compatible with a
62 /// particular renderer.
63 ///
64 /// By default, always returns `true`.
65 fn supports_renderer(&self, _renderer: &str) -> bool {
66 true
67 }
68}