Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated the documentation for new preprocessor format #787

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions book-example/src/for_developers/preprocessors.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ A preprocessor is represented by the `Preprocessor` trait.
```rust
pub trait Preprocessor {
fn name(&self) -> &str;
fn run(&self, ctx: &PreprocessorContext, book: &mut Book) -> Result<()>;
fn run(&self, ctx: &PreprocessorContext, book: Book) -> Result<Book>;
fn supports_renderer(&self, _renderer: &str) -> bool {
true
}
}
```

Expand All @@ -28,9 +31,13 @@ Where the `PreprocessorContext` is defined as
pub struct PreprocessorContext {
pub root: PathBuf,
pub config: Config,
/// The `Renderer` this preprocessor is being used with.
pub renderer: String,
}
```

The `renderer` value allows you react accordingly, for example, PDF or HTML.

## A complete Example

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

```rust
fn remove_emphasis(num_removed_items: &mut i32, chapter: &mut Chapter) -> Result<String> {
fn remove_emphasis(
num_removed_items: &mut usize,
chapter: &mut Chapter,
) -> Result<String> {
let mut buf = String::with_capacity(chapter.content.len());

let events = Parser::new(&chapter.content).filter(|e| {
let should_keep = match *e {
Event::Start(Tag::Emphasis)
Expand All @@ -83,15 +94,16 @@ fn remove_emphasis(num_removed_items: &mut i32, chapter: &mut Chapter) -> Result
}
should_keep
});
cmark(events, &mut buf, None)
.map(|_| buf)
.map_err(|err| Error::from(format!("Markdown serialization failed: {}", err)))

cmark(events, &mut buf, None).map(|_| buf).map_err(|err| {
Error::from(format!("Markdown serialization failed: {}", err))
})
}
```

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

[preprocessor-docs]: https://docs.rs/mdbook/0.1.3/mdbook/preprocess/trait.Preprocessor.html
[preprocessor-docs]: https://docs.rs/mdbook/0.2.2/mdbook/preprocess/trait.Preprocessor.html
[pc]: https://crates.io/crates/pulldown-cmark
[pctc]: https://crates.io/crates/pulldown-cmark-to-cmark
[example]: https://github.com/rust-lang-nursery/mdBook/blob/master/examples/de-emphasize.rs
40 changes: 33 additions & 7 deletions book-example/src/format/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ description = "The example book covers examples."
build-dir = "my-example-book"
create-missing = false

[preprocess]
index = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct. Each preprocessor gets its own table and you can enable/disable inserting the default preprocessors by setting build.use-default-preprocessors to true.

linkes = true

[output.html]
additional-css = ["custom.css"]

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


### General metadata

This is general information about your book.
Expand Down Expand Up @@ -59,10 +62,8 @@ This controls the build process of your book.
will be created when the book is built (i.e. `create-missing = true`). If this
is `false` then the build process will instead exit with an error if any files
do not exist.
- **preprocess:** Specify which preprocessors to be applied. Default is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to mention the use-default-preprocessors flag here. It's used to enable/disable including the default preprocessors and defaults to true.

I've also added a couple tests in 18a36ec to make sure the build.use-default-preprocessors flag works as intended.

`["links", "index"]`. To disable default preprocessors, pass an empty array
`[]` in.

### Configuring Preprocessors

The following preprocessors are available and included by default:

Expand All @@ -71,14 +72,39 @@ The following preprocessors are available and included by default:
- `index`: Convert all chapter files named `README.md` into `index.md`. That is
to say, all `README.md` would be rendered to an index file `index.html` in the
rendered book.

- `emoji`: Convert `:emoji:` text into Emojis. eg: `:smile:` to :smile:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, there's an emoji preprocessors? That's really cool!


**book.toml**
```toml
[build]
build-dir = "build"
create-missing = false
preprocess = ["links", "index"]

[preprocess]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to be changed to something like:

[preprocess.links]

[preprocess.index]

[preprocess.emoji]

Although those three preprocessors will be enabled by default, so it's kinda redundant to even mention them...

links = true
index = true
emoji = true
```

#### Locking a Preprocessor dependency to a renderer

You can explicitly specify that a preprocessor should run for a renderer by binding the two together.

```
[preprocessor.mathjax]
renderers = ["html"] # mathjax only makes sense with the HTML renderer
```

#### Nested Configuration
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section will probably need to mention that, like renderers, each preprocessor will always need to be given its own table (e.g. [preprocessor.mathjax]). From there, passing extra configuration to the preprocessor is just a case of adding key-value pairs to the table, so there isn't really a separate "style of configuration".


Where a preprocessor has more complex configuration available to it, use the
following style of configuration.

```
[preprocess.links]
# set the renderers this preprocessor will run for
renderers = ["html"]
some_extra_feature = true
```

### HTML renderer options
Expand Down Expand Up @@ -214,4 +240,4 @@ book's title without needing to touch your `book.toml`.

The latter case may be useful in situations where `mdbook` is invoked from a
script or CI, where it sometimes isn't possible to update the `book.toml` before
building.
building.