2018-09-25 19:41:38 +08:00
|
|
|
//! An example preprocessor for removing all forms of emphasis from a markdown
|
|
|
|
//! book.
|
2018-02-24 18:14:52 +08:00
|
|
|
use mdbook::book::{Book, BookItem, Chapter};
|
2018-07-24 01:45:01 +08:00
|
|
|
use mdbook::errors::{Error, Result};
|
2018-02-24 18:14:52 +08:00
|
|
|
use mdbook::preprocess::{Preprocessor, PreprocessorContext};
|
|
|
|
use pulldown_cmark::{Event, Parser, Tag};
|
|
|
|
use pulldown_cmark_to_cmark::fmt::cmark;
|
|
|
|
|
2018-09-10 18:55:58 +08:00
|
|
|
const NAME: &str = "md-links-to-html-links";
|
|
|
|
|
|
|
|
fn main() {
|
2018-09-25 19:41:38 +08:00
|
|
|
panic!("This example is intended to be part of a library");
|
2018-09-10 18:55:58 +08:00
|
|
|
}
|
|
|
|
|
2018-12-04 07:57:15 +08:00
|
|
|
#[allow(dead_code)]
|
2018-02-24 18:14:52 +08:00
|
|
|
struct Deemphasize;
|
|
|
|
|
|
|
|
impl Preprocessor for Deemphasize {
|
|
|
|
fn name(&self) -> &str {
|
2018-09-10 18:55:58 +08:00
|
|
|
NAME
|
2018-02-24 18:14:52 +08:00
|
|
|
}
|
|
|
|
|
2018-09-10 18:55:58 +08:00
|
|
|
fn run(&self, _ctx: &PreprocessorContext, mut book: Book) -> Result<Book> {
|
2018-02-24 18:14:52 +08:00
|
|
|
eprintln!("Running '{}' preprocessor", self.name());
|
|
|
|
let mut num_removed_items = 0;
|
2018-09-10 18:55:58 +08:00
|
|
|
|
|
|
|
process(&mut book.sections, &mut num_removed_items)?;
|
|
|
|
|
2018-02-24 18:14:52 +08:00
|
|
|
eprintln!(
|
|
|
|
"{}: removed {} events from markdown stream.",
|
|
|
|
self.name(),
|
|
|
|
num_removed_items
|
|
|
|
);
|
2018-09-10 18:55:58 +08:00
|
|
|
|
|
|
|
Ok(book)
|
2018-02-24 18:14:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 18:55:58 +08:00
|
|
|
fn process<'a, I>(items: I, num_removed_items: &mut usize) -> Result<()>
|
|
|
|
where
|
|
|
|
I: IntoIterator<Item = &'a mut BookItem> + 'a,
|
|
|
|
{
|
|
|
|
for item in items {
|
|
|
|
if let BookItem::Chapter(ref mut chapter) = *item {
|
|
|
|
eprintln!("{}: processing chapter '{}'", NAME, chapter.name);
|
2018-02-24 18:14:52 +08:00
|
|
|
|
2018-09-10 18:55:58 +08:00
|
|
|
let md = remove_emphasis(num_removed_items, chapter)?;
|
|
|
|
chapter.content = md;
|
|
|
|
}
|
2018-02-24 18:14:52 +08:00
|
|
|
}
|
2018-09-10 18:55:58 +08:00
|
|
|
|
|
|
|
Ok(())
|
2018-02-24 18:14:52 +08:00
|
|
|
}
|
|
|
|
|
2018-12-04 07:11:41 +08:00
|
|
|
fn remove_emphasis(num_removed_items: &mut usize, chapter: &mut Chapter) -> Result<String> {
|
2018-09-10 18:55:58 +08:00
|
|
|
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)
|
|
|
|
| Event::Start(Tag::Strong)
|
|
|
|
| Event::End(Tag::Emphasis)
|
|
|
|
| Event::End(Tag::Strong) => false,
|
|
|
|
_ => true,
|
|
|
|
};
|
|
|
|
if !should_keep {
|
|
|
|
*num_removed_items += 1;
|
|
|
|
}
|
|
|
|
should_keep
|
|
|
|
});
|
|
|
|
|
2018-12-04 07:11:41 +08:00
|
|
|
cmark(events, &mut buf, None)
|
|
|
|
.map(|_| buf)
|
|
|
|
.map_err(|err| Error::from(format!("Markdown serialization failed: {}", err)))
|
2018-02-24 18:14:52 +08:00
|
|
|
}
|