| 1 |
# Preprocessors |
| 2 |
|
| 3 |
A *preprocessor* is simply a bit of code which gets run immediately after the |
| 4 |
book is loaded and before it gets rendered, allowing you to update and mutate |
| 5 |
the book. Possible use cases are: |
| 6 |
|
| 7 |
- Creating custom helpers like `\{{#include /path/to/file.md}}` |
| 8 |
- Substituting in latex-style expressions (`$$ \frac{1}{3} $$`) with their |
| 9 |
mathjax equivalents |
| 10 |
|
| 11 |
See [Configuring Preprocessors](../format/configuration/preprocessors.md) for more information about using preprocessors. |
| 12 |
|
| 13 |
## Hooking into MDBook |
| 14 |
|
| 15 |
MDBook uses a fairly simple mechanism for discovering third party plugins. |
| 16 |
A new table is added to `book.toml` (e.g. `[preprocessor.foo]` for the `foo` |
| 17 |
preprocessor) and then `mdbook` will try to invoke the `mdbook-foo` program as |
| 18 |
part of the build process. |
| 19 |
|
| 20 |
Once the preprocessor has been defined and the build process starts, mdBook executes the command defined in the `preprocessor.foo.command` key twice. |
| 21 |
The first time it runs the preprocessor to determine if it supports the given renderer. |
| 22 |
mdBook passes two arguments to the process: the first argument is the string `supports` and the second argument is the renderer name. |
| 23 |
The preprocessor should exit with a status code 0 if it supports the given renderer, or return a non-zero exit code if it does not. |
| 24 |
|
| 25 |
If the preprocessor supports the renderer, then mdbook runs it a second time, passing JSON data into stdin. |
| 26 |
The JSON consists of an array of `[context, book]` where `context` is the serialized object [`PreprocessorContext`] and `book` is a [`Book`] object containing the content of the book. |
| 27 |
|
| 28 |
The preprocessor should return the JSON format of the [`Book`] object to stdout, with any modifications it wishes to perform. |
| 29 |
|
| 30 |
The easiest way to get started is by creating your own implementation of the |
| 31 |
`Preprocessor` trait (e.g. in `lib.rs`) and then creating a shell binary which |
| 32 |
translates inputs to the correct `Preprocessor` method. For convenience, there |
| 33 |
is [an example no-op preprocessor] in the `examples/` directory which can easily |
| 34 |
be adapted for other preprocessors. |
| 35 |
|
| 36 |
<details> |
| 37 |
<summary>Example no-op preprocessor</summary> |
| 38 |
|
| 39 |
```rust |
| 40 |
// nop-preprocessors.rs |
| 41 |
|
| 42 |
{{#include ../../../examples/nop-preprocessor.rs}} |
| 43 |
``` |
| 44 |
</details> |
| 45 |
|
| 46 |
## Hints for implementing a preprocessor |
| 47 |
|
| 48 |
By pulling in `mdbook-preprocessor` as a library, preprocessors can have access to the |
| 49 |
existing infrastructure for dealing with books. |
| 50 |
|
| 51 |
For example, a custom preprocessor could use the |
| 52 |
[`parse_input()`] function to deserialize the JSON written to |
| 53 |
`stdin`. Then each chapter of the `Book` can be mutated in-place via |
| 54 |
[`Book::for_each_mut()`], and then written to `stdout` with the `serde_json` |
| 55 |
crate. |
| 56 |
|
| 57 |
Chapters can be accessed either directly (by recursively iterating over |
| 58 |
chapters) or via the `Book::for_each_mut()` convenience method. |
| 59 |
|
| 60 |
The `chapter.content` is just a string which happens to be markdown. While it's |
| 61 |
entirely possible to use regular expressions or do a manual find & replace, |
| 62 |
you'll probably want to process the input into something more computer-friendly. |
| 63 |
The [`mdbook-markdown`] crate exposes the [`pulldown-cmark`][pc] crate used by mdBook to parse Markdown. The [`pulldown-cmark-to-cmark`][pctc] crate can be used to translate events back into markdown text. |
| 64 |
|
| 65 |
The following code block shows how to remove all emphasis from markdown, |
| 66 |
without accidentally breaking the document. |
| 67 |
|
| 68 |
```rust |
| 69 |
{{#rustdoc_include ../../../examples/remove-emphasis/mdbook-remove-emphasis/src/main.rs:remove_emphasis}} |
| 70 |
``` |
| 71 |
|
| 72 |
Take a look at the [full example source][emphasis-example] for more details. |
| 73 |
|
| 74 |
## Implementing a preprocessor with a different language |
| 75 |
|
| 76 |
The fact that mdBook utilizes stdin and stdout to communicate with the preprocessors makes it easy to implement them in a language other than Rust. |
| 77 |
The following code shows how to implement a simple preprocessor in Python, which will modify the content of the first chapter. |
| 78 |
The example below follows the configuration shown above with `preprocessor.foo.command` actually pointing to a Python script. |
| 79 |
|
| 80 |
```python |
| 81 |
import json |
| 82 |
import sys |
| 83 |
|
| 84 |
|
| 85 |
if __name__ == '__main__': |
| 86 |
if len(sys.argv) > 1: # we check if we received any argument |
| 87 |
if sys.argv[1] == "supports": |
| 88 |
# then we are good to return an exit status code of 0, since the other argument will just be the renderer's name |
| 89 |
sys.exit(0) |
| 90 |
|
| 91 |
# load both the context and the book representations from stdin |
| 92 |
context, book = json.load(sys.stdin) |
| 93 |
# and now, we can just modify the content of the first chapter |
| 94 |
book['items'][0]['Chapter']['content'] = '# Hello' |
| 95 |
# we are done with the book's modification, we can just print it to stdout, |
| 96 |
print(json.dumps(book)) |
| 97 |
``` |
| 98 |
|
| 99 |
|
| 100 |
[emphasis-example]: https://github.com/rust-lang/mdBook/tree/master/examples/remove-emphasis/ |
| 101 |
[pc]: https://crates.io/crates/pulldown-cmark |
| 102 |
[pctc]: https://crates.io/crates/pulldown-cmark-to-cmark |
| 103 |
[an example no-op preprocessor]: https://github.com/rust-lang/mdBook/blob/master/examples/nop-preprocessor.rs |
| 104 |
[`parse_input()`]: https://docs.rs/mdbook-preprocessor/latest/mdbook_preprocessor/fn.parse_input.html |
| 105 |
[`Book::for_each_mut()`]: https://docs.rs/mdbook-preprocessor/latest/mdbook_preprocessor/book/struct.Book.html#method.for_each_mut |
| 106 |
[`PreprocessorContext`]: https://docs.rs/mdbook-preprocessor/latest/mdbook_preprocessor/struct.PreprocessorContext.html |
| 107 |
[`Book`]: https://docs.rs/mdbook-preprocessor/latest/mdbook_preprocessor/book/struct.Book.html |
| 108 |
[`mdbook-markdown`]: https://docs.rs/mdbook-markdown/latest/mdbook_markdown/ |