Skip to content

Commit a00ccf8

Browse files
rbucklandMichael-F-Bryan
authored andcommitted
Updated the documentation for new preprocessor format (#787)
* Updated the documentation for new preprocessor format * adjusted the descriptions for preprocessors
1 parent 18a36ec commit a00ccf8

File tree

2 files changed

+68
-14
lines changed

2 files changed

+68
-14
lines changed

book-example/src/for_developers/preprocessors.md

+18-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ A preprocessor is represented by the `Preprocessor` trait.
1818
```rust
1919
pub trait Preprocessor {
2020
fn name(&self) -> &str;
21-
fn run(&self, ctx: &PreprocessorContext, book: &mut Book) -> Result<()>;
21+
fn run(&self, ctx: &PreprocessorContext, book: Book) -> Result<Book>;
22+
fn supports_renderer(&self, _renderer: &str) -> bool {
23+
true
24+
}
2225
}
2326
```
2427

@@ -28,9 +31,13 @@ Where the `PreprocessorContext` is defined as
2831
pub struct PreprocessorContext {
2932
pub root: PathBuf,
3033
pub config: Config,
34+
/// The `Renderer` this preprocessor is being used with.
35+
pub renderer: String,
3136
}
3237
```
3338

39+
The `renderer` value allows you react accordingly, for example, PDF or HTML.
40+
3441
## A complete Example
3542

3643
The magic happens within the `run(...)` method of the
@@ -68,8 +75,12 @@ The following code block shows how to remove all emphasis from markdown, and do
6875
so safely.
6976

7077
```rust
71-
fn remove_emphasis(num_removed_items: &mut i32, chapter: &mut Chapter) -> Result<String> {
78+
fn remove_emphasis(
79+
num_removed_items: &mut usize,
80+
chapter: &mut Chapter,
81+
) -> Result<String> {
7282
let mut buf = String::with_capacity(chapter.content.len());
83+
7384
let events = Parser::new(&chapter.content).filter(|e| {
7485
let should_keep = match *e {
7586
Event::Start(Tag::Emphasis)
@@ -83,15 +94,16 @@ fn remove_emphasis(num_removed_items: &mut i32, chapter: &mut Chapter) -> Result
8394
}
8495
should_keep
8596
});
86-
cmark(events, &mut buf, None)
87-
.map(|_| buf)
88-
.map_err(|err| Error::from(format!("Markdown serialization failed: {}", err)))
97+
98+
cmark(events, &mut buf, None).map(|_| buf).map_err(|err| {
99+
Error::from(format!("Markdown serialization failed: {}", err))
100+
})
89101
}
90102
```
91103

92104
For everything else, have a look [at the complete example][example].
93105

94-
[preprocessor-docs]: https://docs.rs/mdbook/0.1.3/mdbook/preprocess/trait.Preprocessor.html
106+
[preprocessor-docs]: https://docs.rs/mdbook/latest/mdbook/preprocess/trait.Preprocessor.html
95107
[pc]: https://crates.io/crates/pulldown-cmark
96108
[pctc]: https://crates.io/crates/pulldown-cmark-to-cmark
97109
[example]: https://github.com/rust-lang-nursery/mdBook/blob/master/examples/de-emphasize.rs

book-example/src/format/config.md

+50-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ description = "The example book covers examples."
1414
build-dir = "my-example-book"
1515
create-missing = false
1616

17+
[preprocess.index]
18+
19+
[preprocess.links]
20+
1721
[output.html]
1822
additional-css = ["custom.css"]
1923

@@ -27,7 +31,6 @@ It is important to note that **any** relative path specified in the in the
2731
configuration will always be taken relative from the root of the book where the
2832
configuration file is located.
2933

30-
3134
### General metadata
3235

3336
This is general information about your book.
@@ -59,15 +62,25 @@ This controls the build process of your book.
5962
will be created when the book is built (i.e. `create-missing = true`). If this
6063
is `false` then the build process will instead exit with an error if any files
6164
do not exist.
62-
- **preprocess:** Specify which preprocessors to be applied. Default is
63-
`["links", "index"]`. To disable default preprocessors, pass an empty array
64-
`[]` in.
65+
- **use-default-preprocessors:** Disable the default preprocessors of (`links` &
66+
`index`) by setting this option to `false`.
6567

68+
If you have the same, and/or other preprocessors declared via their table
69+
of configuration, they will run instead.
70+
71+
- For clarity, with no preprocessor configuration, the default `links` and
72+
`index` will run.
73+
- Setting `use-default-preprocessors = false` will disable these
74+
default preprocessors from running.
75+
- Adding `[preprocessor.links]`, for example, will ensure, regardless of
76+
`use-default-preprocessors` that `links` it will run.
77+
78+
## Configuring Preprocessors
6679

6780
The following preprocessors are available and included by default:
6881

69-
- `links`: Expand the `{{# playpen}}` and `{{# include}}` handlebars helpers in
70-
a chapter.
82+
- `links`: Expand the `{{ #playpen }}` and `{{ #include }}` handlebars helpers in
83+
a chapter to include the contents of a file.
7184
- `index`: Convert all chapter files named `README.md` into `index.md`. That is
7285
to say, all `README.md` would be rendered to an index file `index.html` in the
7386
rendered book.
@@ -78,10 +91,39 @@ The following preprocessors are available and included by default:
7891
[build]
7992
build-dir = "build"
8093
create-missing = false
81-
preprocess = ["links", "index"]
94+
95+
[preprocess.links]
96+
97+
[preprocess.index]
8298
```
8399

100+
### Custom Preprocessor Configuration
101+
102+
Like renderers, preprocessor will need to be given its own table (e.g. `[preprocessor.mathjax]`).
103+
In the section, you may then pass extra configuration to the preprocessor by adding key-value pairs to the table.
104+
105+
For example
106+
107+
```
108+
[preprocess.links]
109+
# set the renderers this preprocessor will run for
110+
renderers = ["html"]
111+
some_extra_feature = true
112+
```
113+
114+
#### Locking a Preprocessor dependency to a renderer
115+
116+
You can explicitly specify that a preprocessor should run for a renderer by binding the two together.
117+
118+
```
119+
[preprocessor.mathjax]
120+
renderers = ["html"] # mathjax only makes sense with the HTML renderer
121+
```
122+
123+
## Configuring Renderers
124+
84125
### HTML renderer options
126+
85127
The HTML renderer has a couple of options as well. All the options for the
86128
renderer need to be specified under the TOML table `[output.html]`.
87129

@@ -214,4 +256,4 @@ book's title without needing to touch your `book.toml`.
214256
215257
The latter case may be useful in situations where `mdbook` is invoked from a
216258
script or CI, where it sometimes isn't possible to update the `book.toml` before
217-
building.
259+
building.

0 commit comments

Comments
 (0)