]>
Commit | Line | Data |
---|---|---|
2c00a5a8 XL |
1 | //! Book preprocessing. |
2 | ||
9fa01778 XL |
3 | pub use self::cmd::CmdPreprocessor; |
4 | pub use self::index::IndexPreprocessor; | |
2c00a5a8 XL |
5 | pub use self::links::LinkPreprocessor; |
6 | ||
9fa01778 XL |
7 | mod cmd; |
8 | mod index; | |
2c00a5a8 XL |
9 | mod links; |
10 | ||
dc9dc135 XL |
11 | use crate::book::Book; |
12 | use crate::config::Config; | |
13 | use crate::errors::*; | |
2c00a5a8 | 14 | |
94222f64 XL |
15 | use std::cell::RefCell; |
16 | use std::collections::HashMap; | |
2c00a5a8 XL |
17 | use 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 |
22 | pub 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 | ||
37 | impl 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. |
53 | pub 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 | } |