-
Notifications
You must be signed in to change notification settings - Fork 11
/
callback_emitter.rs
53 lines (48 loc) · 1.45 KB
/
callback_emitter.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! A slightly simpler, but less performant version of the link extractor that can be found in
//! `examples/custom_emitter.rs`.
//!
//! ```text
//! printf '<h1>Hello world!</h1><a href="foo">bar</a>' | cargo run --example=custom_emitter
//! ```
//!
//! Output:
//!
//! ```text
//! link: foo
//! ```
use html5gum::emitters::callback::{CallbackEmitter, CallbackEvent};
use html5gum::{Emitter, IoReader, Tokenizer};
fn get_emitter() -> impl Emitter<Token = String> {
let mut is_anchor_tag = false;
let mut is_href_attr = false;
CallbackEmitter::new(move |event: CallbackEvent<'_>| match event {
CallbackEvent::OpenStartTag { name } => {
is_anchor_tag = name == b"a";
is_href_attr = false;
None
}
CallbackEvent::AttributeName { name } => {
is_href_attr = name == b"href";
None
}
CallbackEvent::AttributeValue { value } if is_anchor_tag && is_href_attr => {
Some(String::from_utf8_lossy(value).into_owned())
}
_ => None,
})
}
fn main() {
for token in
Tokenizer::new_with_emitter(IoReader::new(std::io::stdin().lock()), get_emitter()).flatten()
{
println!("link: {}", token);
}
}
#[test]
fn basic() {
let tokens: Vec<_> =
Tokenizer::new_with_emitter("<h1>Hello world</h1><a href=foo>bar</a>", get_emitter())
.flatten()
.collect();
assert_eq!(tokens, vec!["foo".to_owned()]);
}