event_filter/
event-filter.rs

1use std::io::Write as _;
2
3use pulldown_cmark::{html, Event, Options, Parser, Tag, TagEnd};
4
5fn main() {
6    let markdown_input: &str = "This is Peter on ![holiday in Greece](pearl_beach.jpg).";
7    println!("Parsing the following markdown string:\n{}", markdown_input);
8
9    // Set up parser. We can treat is as any other iterator. We replace Peter by John
10    // and image by its alt text.
11    let parser = Parser::new_ext(markdown_input, Options::empty())
12        .map(|event| match event {
13            Event::Text(text) => Event::Text(text.replace("Peter", "John").into()),
14            _ => event,
15        })
16        .filter(|event| match event {
17            Event::Start(Tag::Image { .. }) | Event::End(TagEnd::Image) => false,
18            _ => true,
19        });
20
21    // Write to anything implementing the `Write` trait. This could also be a file
22    // or network socket.
23    let stdout = std::io::stdout();
24    let mut handle = stdout.lock();
25    handle.write_all(b"\nHTML output:\n").unwrap();
26    html::write_html_io(&mut handle, parser).unwrap();
27}