#mdbook #boilerplate #proprocessor

mdbook-preprocessor-boilerplate

Boilerplate code for mdbook preprocessors

3 releases

0.1.2 Apr 20, 2023
0.1.1 Dec 2, 2021
0.1.0 Dec 2, 2021

#877 in Text processing

Download history 1812/week @ 2024-03-14 1462/week @ 2024-03-21 1396/week @ 2024-03-28 1138/week @ 2024-04-04 1154/week @ 2024-04-11 1068/week @ 2024-04-18 1543/week @ 2024-04-25 1857/week @ 2024-05-02 845/week @ 2024-05-09 1568/week @ 2024-05-16 1000/week @ 2024-05-23 1581/week @ 2024-05-30 3479/week @ 2024-06-06 2381/week @ 2024-06-13 2530/week @ 2024-06-20 1444/week @ 2024-06-27

10,052 downloads per month
Used in 3 crates

GPL-3.0 license

15KB
55 lines

mdbook-preprocessor-boilerplate

Boilerplate code for mdbook preprocessors.

Handles the CLI, checks whether the renderer is supported, checks the mdbook version, and runs your preprocessor. All you need to do is implement the mdbook::preprocess::Preprocessor trait.

This boilerplate has a few heavy dependencies (like serde_json and mdbook). If you want a small executable, you'll have to implement this functionality yourself.

Example

The following is functionally identical to the No-Op Preprocessor Example given by mdbook.

use mdbook::book::Book;
use mdbook::preprocess::{CmdPreprocessor, Preprocessor, PreprocessorContext};
use anyhow::{bail, Result};

fn main() {
    mdbook_preprocessor_boilerplate::run(
        NoOpPreprocessor,
        "An mdbook preprocessor that does nothing" // CLI description
    );
}

struct NoOpPreprocessor;

impl Preprocessor for NoOpPreprocessor {
    fn name(&self) -> &str {
        "nop-preprocessor"
    }

    fn run(&self, ctx: &PreprocessorContext, book: Book) -> Result<Book> {
        // In testing we want to tell the preprocessor to blow up by setting a
        // particular config value
        if let Some(nop_cfg) = ctx.config.get_preprocessor(self.name()) {
            if nop_cfg.contains_key("blow-up") {
                anyhow::bail!("Boom!!1!");
            }
        }

        // we *are* a no-op preprocessor after all
        Ok(book)
    }

    fn supports_renderer(&self, renderer: &str) -> bool {
        renderer != "not-supported"
    }
}

License: GPL-3.0

Dependencies

~12–23MB
~324K SLoC