-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcustom_emitter.rs
146 lines (127 loc) · 4.24 KB
/
custom_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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//! An example of using a custom emitter to only extract tags you care about, efficiently.
//!
//! A naive attempt at link extraction would be to tweak `examples/tokenize.rs` to just not print
//! irrelevant data.
//!
//! The approach in this example however is a lot more performant.
//!
//! With the `LinkExtractor` emitter, "Hello world!" will not be allocated individually at all (as
//! there is no `html5gum::Token::String` we have to construct), instead it will only be borrowed
//! from the input data (or I/O buffer)
//!
//! This example can be further optimized by printing directly from within the emitter impl, and
//! changing `pop_token` to always return None.
//!
//! ```text
//! printf '<h1>Hello world!</h1><a href="foo">bar</a>' | cargo run --example=custom_emitter
//! ```
//!
//! Output:
//!
//! ```text
//! link: foo
//! ```
use html5gum::{Emitter, Error, IoReader, State, Tokenizer};
#[derive(Default)]
struct LinkExtractor {
current_tag_name: Vec<u8>,
current_tag_is_closing: bool,
current_attribute_name: Vec<u8>,
current_attribute_value: Vec<u8>,
last_start_tag: Vec<u8>,
queued_token: Option<String>,
}
impl LinkExtractor {
fn flush_old_attribute(&mut self) {
if self.current_tag_name == b"a"
&& self.current_attribute_name == b"href"
&& !self.current_attribute_value.is_empty()
&& !self.current_tag_is_closing
{
self.queued_token =
Some(String::from_utf8(self.current_attribute_value.clone()).unwrap());
}
self.current_attribute_name.clear();
self.current_attribute_value.clear();
}
}
impl Emitter for LinkExtractor {
type Token = String;
fn set_last_start_tag(&mut self, last_start_tag: Option<&[u8]>) {
self.last_start_tag.clear();
self.last_start_tag
.extend(last_start_tag.unwrap_or_default());
}
fn pop_token(&mut self) -> Option<String> {
self.queued_token.take()
}
fn emit_string(&mut self, _: &[u8]) {}
fn init_start_tag(&mut self) {
self.current_tag_name.clear();
self.current_tag_is_closing = false;
}
fn init_end_tag(&mut self) {
self.current_tag_name.clear();
self.current_tag_is_closing = true;
}
fn emit_current_tag(&mut self) -> Option<State> {
self.flush_old_attribute();
self.last_start_tag.clear();
if !self.current_tag_is_closing {
self.last_start_tag.extend(&self.current_tag_name);
}
self.current_tag_name.clear();
html5gum::naive_next_state(&self.last_start_tag)
}
fn set_self_closing(&mut self) {}
fn push_tag_name(&mut self, s: &[u8]) {
self.current_tag_name.extend(s);
}
fn init_attribute(&mut self) {
self.flush_old_attribute();
}
fn push_attribute_name(&mut self, s: &[u8]) {
self.current_attribute_name.extend(s);
}
fn push_attribute_value(&mut self, s: &[u8]) {
self.current_attribute_value.extend(s);
}
fn current_is_appropriate_end_tag_token(&mut self) -> bool {
self.current_tag_is_closing
&& !self.current_tag_name.is_empty()
&& self.current_tag_name == self.last_start_tag
}
fn emit_current_comment(&mut self) {}
fn emit_current_doctype(&mut self) {}
fn emit_eof(&mut self) {}
fn emit_error(&mut self, _: Error) {}
fn init_comment(&mut self) {}
fn init_doctype(&mut self) {}
fn push_comment(&mut self, _: &[u8]) {}
fn push_doctype_name(&mut self, _: &[u8]) {}
fn push_doctype_public_identifier(&mut self, _: &[u8]) {}
fn push_doctype_system_identifier(&mut self, _: &[u8]) {}
fn set_doctype_public_identifier(&mut self, _: &[u8]) {}
fn set_doctype_system_identifier(&mut self, _: &[u8]) {}
fn set_force_quirks(&mut self) {}
}
fn main() {
for token in Tokenizer::new_with_emitter(
IoReader::new(std::io::stdin().lock()),
LinkExtractor::default(),
)
.flatten()
{
println!("link: {}", token);
}
}
#[test]
fn basic() {
let tokens: Vec<_> = Tokenizer::new_with_emitter(
"<h1>Hello world</h1><a href=foo>bar</a>",
LinkExtractor::default(),
)
.flatten()
.collect();
assert_eq!(tokens, vec!["foo".to_owned()]);
}