-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2464 from ehuss/remove-emphasis
Add a real example of remove-emphasis
- Loading branch information
Showing
10 changed files
with
163 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
book |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[book] | ||
title = "remove-emphasis" | ||
|
||
[preprocessor.remove-emphasis] | ||
command = "cargo run --manifest-path=mdbook-remove-emphasis/Cargo.toml --locked" |
10 changes: 10 additions & 0 deletions
10
examples/remove-emphasis/mdbook-remove-emphasis/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "mdbook-remove-emphasis" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
mdbook = { version = "0.4.40", path = "../../.." } | ||
pulldown-cmark = { version = "0.12.2", default-features = false } | ||
pulldown-cmark-to-cmark = "18.0.0" | ||
serde_json = "1.0.132" |
82 changes: 82 additions & 0 deletions
82
examples/remove-emphasis/mdbook-remove-emphasis/src/main.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//! This is a demonstration of an mdBook preprocessor which parses markdown | ||
//! and removes any instances of emphasis. | ||
use mdbook::book::{Book, Chapter}; | ||
use mdbook::errors::Error; | ||
use mdbook::preprocess::{CmdPreprocessor, Preprocessor, PreprocessorContext}; | ||
use mdbook::BookItem; | ||
use pulldown_cmark::{Event, Parser, Tag, TagEnd}; | ||
use std::io; | ||
|
||
fn main() { | ||
let mut args = std::env::args().skip(1); | ||
match args.next().as_deref() { | ||
Some("supports") => { | ||
// Supports all renderers. | ||
return; | ||
} | ||
Some(arg) => { | ||
eprintln!("unknown argument: {arg}"); | ||
std::process::exit(1); | ||
} | ||
None => {} | ||
} | ||
|
||
if let Err(e) = handle_preprocessing() { | ||
eprintln!("{}", e); | ||
std::process::exit(1); | ||
} | ||
} | ||
|
||
struct RemoveEmphasis; | ||
|
||
impl Preprocessor for RemoveEmphasis { | ||
fn name(&self) -> &str { | ||
"remove-emphasis" | ||
} | ||
|
||
fn run(&self, _ctx: &PreprocessorContext, mut book: Book) -> Result<Book, Error> { | ||
let mut total = 0; | ||
book.for_each_mut(|item| { | ||
let BookItem::Chapter(ch) = item else { | ||
return; | ||
}; | ||
if ch.is_draft_chapter() { | ||
return; | ||
} | ||
match remove_emphasis(&mut total, ch) { | ||
Ok(s) => ch.content = s, | ||
Err(e) => eprintln!("failed to process chapter: {e:?}"), | ||
} | ||
}); | ||
eprintln!("removed {total} emphasis"); | ||
Ok(book) | ||
} | ||
} | ||
|
||
// ANCHOR: remove_emphasis | ||
fn remove_emphasis(num_removed_items: &mut usize, chapter: &mut Chapter) -> Result<String, Error> { | ||
let mut buf = String::with_capacity(chapter.content.len()); | ||
|
||
let events = Parser::new(&chapter.content).filter(|e| match e { | ||
Event::Start(Tag::Emphasis) | Event::Start(Tag::Strong) => { | ||
*num_removed_items += 1; | ||
false | ||
} | ||
Event::End(TagEnd::Emphasis) | Event::End(TagEnd::Strong) => false, | ||
_ => true, | ||
}); | ||
|
||
Ok(pulldown_cmark_to_cmark::cmark(events, &mut buf).map(|_| buf)?) | ||
} | ||
// ANCHOR_END: remove_emphasis | ||
|
||
pub fn handle_preprocessing() -> Result<(), Error> { | ||
let pre = RemoveEmphasis; | ||
let (ctx, book) = CmdPreprocessor::parse_input(io::stdin())?; | ||
|
||
let processed_book = pre.run(&ctx, book)?; | ||
serde_json::to_writer(io::stdout(), &processed_book)?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Summary | ||
|
||
- [Chapter 1](./chapter_1.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Chapter 1 | ||
|
||
This has *light emphasis* and **bold emphasis**. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use mdbook::MDBook; | ||
|
||
#[test] | ||
fn remove_emphasis_works() { | ||
// Tests that the remove-emphasis example works as expected. | ||
|
||
// Workaround for https://github.com/rust-lang/mdBook/issues/1424 | ||
std::env::set_current_dir("examples/remove-emphasis").unwrap(); | ||
let book = MDBook::load(".").unwrap(); | ||
book.build().unwrap(); | ||
let ch1 = std::fs::read_to_string("book/chapter_1.html").unwrap(); | ||
assert!(ch1.contains("This has light emphasis and bold emphasis.")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters