Skip to content

Commit f4cf32e

Browse files
authored
Merge pull request #2464 from ehuss/remove-emphasis
Add a real example of remove-emphasis
2 parents 47384c1 + 9e3d533 commit f4cf32e

File tree

10 files changed

+163
-30
lines changed

10 files changed

+163
-30
lines changed

Cargo.lock

+34-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[workspace]
2+
members = [".", "examples/remove-emphasis/mdbook-remove-emphasis"]
3+
14
[package]
25
name = "mdbook"
36
version = "0.4.40"
@@ -73,3 +76,9 @@ name = "mdbook"
7376
[[example]]
7477
name = "nop-preprocessor"
7578
test = true
79+
80+
[[example]]
81+
name = "remove-emphasis"
82+
path = "examples/remove-emphasis/test.rs"
83+
crate-type = ["lib"]
84+
test = true

examples/remove-emphasis/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
book

examples/remove-emphasis/book.toml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[book]
2+
title = "remove-emphasis"
3+
4+
[preprocessor.remove-emphasis]
5+
command = "cargo run --manifest-path=mdbook-remove-emphasis/Cargo.toml --locked"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "mdbook-remove-emphasis"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
mdbook = { version = "0.4.40", path = "../../.." }
8+
pulldown-cmark = { version = "0.12.2", default-features = false }
9+
pulldown-cmark-to-cmark = "18.0.0"
10+
serde_json = "1.0.132"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//! This is a demonstration of an mdBook preprocessor which parses markdown
2+
//! and removes any instances of emphasis.
3+
4+
use mdbook::book::{Book, Chapter};
5+
use mdbook::errors::Error;
6+
use mdbook::preprocess::{CmdPreprocessor, Preprocessor, PreprocessorContext};
7+
use mdbook::BookItem;
8+
use pulldown_cmark::{Event, Parser, Tag, TagEnd};
9+
use std::io;
10+
11+
fn main() {
12+
let mut args = std::env::args().skip(1);
13+
match args.next().as_deref() {
14+
Some("supports") => {
15+
// Supports all renderers.
16+
return;
17+
}
18+
Some(arg) => {
19+
eprintln!("unknown argument: {arg}");
20+
std::process::exit(1);
21+
}
22+
None => {}
23+
}
24+
25+
if let Err(e) = handle_preprocessing() {
26+
eprintln!("{}", e);
27+
std::process::exit(1);
28+
}
29+
}
30+
31+
struct RemoveEmphasis;
32+
33+
impl Preprocessor for RemoveEmphasis {
34+
fn name(&self) -> &str {
35+
"remove-emphasis"
36+
}
37+
38+
fn run(&self, _ctx: &PreprocessorContext, mut book: Book) -> Result<Book, Error> {
39+
let mut total = 0;
40+
book.for_each_mut(|item| {
41+
let BookItem::Chapter(ch) = item else {
42+
return;
43+
};
44+
if ch.is_draft_chapter() {
45+
return;
46+
}
47+
match remove_emphasis(&mut total, ch) {
48+
Ok(s) => ch.content = s,
49+
Err(e) => eprintln!("failed to process chapter: {e:?}"),
50+
}
51+
});
52+
eprintln!("removed {total} emphasis");
53+
Ok(book)
54+
}
55+
}
56+
57+
// ANCHOR: remove_emphasis
58+
fn remove_emphasis(num_removed_items: &mut usize, chapter: &mut Chapter) -> Result<String, Error> {
59+
let mut buf = String::with_capacity(chapter.content.len());
60+
61+
let events = Parser::new(&chapter.content).filter(|e| match e {
62+
Event::Start(Tag::Emphasis) | Event::Start(Tag::Strong) => {
63+
*num_removed_items += 1;
64+
false
65+
}
66+
Event::End(TagEnd::Emphasis) | Event::End(TagEnd::Strong) => false,
67+
_ => true,
68+
});
69+
70+
Ok(pulldown_cmark_to_cmark::cmark(events, &mut buf).map(|_| buf)?)
71+
}
72+
// ANCHOR_END: remove_emphasis
73+
74+
pub fn handle_preprocessing() -> Result<(), Error> {
75+
let pre = RemoveEmphasis;
76+
let (ctx, book) = CmdPreprocessor::parse_input(io::stdin())?;
77+
78+
let processed_book = pre.run(&ctx, book)?;
79+
serde_json::to_writer(io::stdout(), &processed_book)?;
80+
81+
Ok(())
82+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Summary
2+
3+
- [Chapter 1](./chapter_1.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Chapter 1
2+
3+
This has *light emphasis* and **bold emphasis**.

examples/remove-emphasis/test.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use mdbook::MDBook;
2+
3+
#[test]
4+
fn remove_emphasis_works() {
5+
// Tests that the remove-emphasis example works as expected.
6+
7+
// Workaround for https://github.com/rust-lang/mdBook/issues/1424
8+
std::env::set_current_dir("examples/remove-emphasis").unwrap();
9+
let book = MDBook::load(".").unwrap();
10+
book.build().unwrap();
11+
let ch1 = std::fs::read_to_string("book/chapter_1.html").unwrap();
12+
assert!(ch1.contains("This has light emphasis and bold emphasis."));
13+
}

guide/src/for_developers/preprocessors.md

+3-27
Original file line numberDiff line numberDiff line change
@@ -68,33 +68,10 @@ The following code block shows how to remove all emphasis from markdown,
6868
without accidentally breaking the document.
6969

7070
```rust
71-
fn remove_emphasis(
72-
num_removed_items: &mut usize,
73-
chapter: &mut Chapter,
74-
) -> Result<String> {
75-
let mut buf = String::with_capacity(chapter.content.len());
76-
77-
let events = Parser::new(&chapter.content).filter(|e| {
78-
let should_keep = match *e {
79-
Event::Start(Tag::Emphasis)
80-
| Event::Start(Tag::Strong)
81-
| Event::End(Tag::Emphasis)
82-
| Event::End(Tag::Strong) => false,
83-
_ => true,
84-
};
85-
if !should_keep {
86-
*num_removed_items += 1;
87-
}
88-
should_keep
89-
});
90-
91-
cmark(events, &mut buf, None).map(|_| buf).map_err(|err| {
92-
Error::from(format!("Markdown serialization failed: {}", err))
93-
})
94-
}
71+
{{#rustdoc_include ../../../examples/remove-emphasis/mdbook-remove-emphasis/src/main.rs:remove_emphasis}}
9572
```
9673

97-
For everything else, have a look [at the complete example][example].
74+
Take a look at the [full example source][emphasis-example] for more details.
9875

9976
## Implementing a preprocessor with a different language
10077

@@ -122,11 +99,10 @@ if __name__ == '__main__':
12299
```
123100

124101

125-
102+
[emphasis-example]: https://github.com/rust-lang/mdBook/tree/master/examples/remove-emphasis/
126103
[preprocessor-docs]: https://docs.rs/mdbook/latest/mdbook/preprocess/trait.Preprocessor.html
127104
[pc]: https://crates.io/crates/pulldown-cmark
128105
[pctc]: https://crates.io/crates/pulldown-cmark-to-cmark
129-
[example]: https://github.com/rust-lang/mdBook/blob/master/examples/nop-preprocessor.rs
130106
[an example no-op preprocessor]: https://github.com/rust-lang/mdBook/blob/master/examples/nop-preprocessor.rs
131107
[`CmdPreprocessor::parse_input()`]: https://docs.rs/mdbook/latest/mdbook/preprocess/trait.Preprocessor.html#method.parse_input
132108
[`Book::for_each_mut()`]: https://docs.rs/mdbook/latest/mdbook/book/struct.Book.html#method.for_each_mut

0 commit comments

Comments
 (0)