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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#![no_std]
extern crate alloc;
pub use parse::get_all_a_hrefs;
pub use parse::get_elements_by_class_name;
pub use parse::get_elements_by_tag_name;
pub use parse::get_first_element_by_class_name;
pub use parse::get_first_element_by_tag_name;
pub use traits::Getattribute;
#[allow(warnings)]
mod traits{
use alloc::string::String;
use alloc::{fmt::format, string::ToString, vec::Vec};
use libc_print::std_name::{dbg, eprintln, println};
pub trait Getattribute {
fn get_attribute(&self, attr: impl ToString) -> Option<String>;
fn inner_text(&self) -> Option<String>;
}
impl Getattribute for String {
///## get innner text from node element
/// ```rust
/// #[allow(warnings)]
///fn main(){
/// use loa::Getattribute;
/// let html = include_str!("../index.html");
/// let body = get_first_element_by_tag_name(html, "head").unwrap();
/// let text = body.inner_text().unwrap();
/// println!("{}",text);
///}
/// ```
fn inner_text(&self) -> Option<String> {
let pattern = fancy_regex::Regex::new(r#"([^>])[^<>]+(?=[<])"#).unwrap();
let html = self.to_string().replace("\n", "");
let mut text_vec = Vec::new();
for cap in pattern.captures_iter(&html) {
if let Some(text) = cap
.unwrap()
.iter()
.map(|s| s)
.collect::<Vec<_>>()
.first()
.unwrap()
{
text_vec.push(text.as_str().trim().to_string());
}
}
Some(text_vec.join("\n").trim().to_string())
}
/// ## get node attributes
/// ```rust
/// #[allow(warnings)]
/// fn main() {
/// use loa::{get_elements_by_tag_name, get_first_element_by_tag_name, Getattribute};
/// let html = include_str!("../index.html");
/// let p_list: Vec<String> = loa::get_elements_by_tag_name(html, "p");
/// let a_first: String = loa::get_first_element_by_tag_name(html, "a").unwrap();
/// let href = a_first.get_attribute("href");
/// println!("{:?}", p_list);
/// println!("{:?}", a_first);
/// println!("{:?}", href);
/// let buttons = get_elements_by_tag_name(html, "button");
/// for bu in &buttons{
/// if bu.contains("Cargo.toml"){
/// println!("{:?}",bu.get_attribute("title"));
/// }
/// }
/// }
/// ```
fn get_attribute(&self, attr: impl ToString) -> Option<String> {
use libc_print::std_name::{dbg, eprintln, println};
let attr = attr.to_string();
let out_tag = self
.split(" ")
.filter(|s| !s.is_empty())
.collect::<Vec<_>>()
.get(0)
.expect("error to get out tag")
.to_string()
.replace("<", "");
let new_self_vec = self
.split("><")
.filter(|s| !s.is_empty())
.map(|s| s.to_string())
.collect::<Vec<_>>();
let new_self = new_self_vec.get(0).unwrap_or(&self).replace("\"\"", "");
let attr_vec = new_self
.split("\"")
.filter(|s| !s.is_empty())
.map(|s| s.trim().to_string())
.collect::<Vec<_>>();
// println!("{:?}", attr_vec);
let mut attr_index: usize = 0;
for i in 0..attr_vec.len() {
let s = attr_vec.get(i).expect("get error");
if s.contains(&attr) {
attr_index += 1;
break;
}
attr_index = attr_index + 1;
}
match attr_vec.get(attr_index) {
Some(e) => Some(e.to_string()),
None => None,
}
}
}
}
#[allow(warnings)]
mod parse {
use alloc::string::String;
use alloc::vec;
use alloc::{fmt::format, string::ToString, vec::Vec};
use libc_print::std_name::{dbg, eprintln, println};
use crate::Getattribute;
/// ## parse html get Vec of nodes
/// ```rust
/// #[allow(warnings)]
/// fn main() {
/// use loa::{get_elements_by_tag_name, get_first_element_by_tag_name, Getattribute};
/// let html = include_str!("../index.html");
/// let p_list: Vec<String> = loa::get_elements_by_tag_name(html, "p");
/// let a_first: String = loa::get_first_element_by_tag_name(html, "a").unwrap();
/// let href = a_first.get_attribute("href");
/// let class = a_first.get_attribute("class");
/// println!("{:?}", p_list);
/// println!("{:?}", a_first);
/// println!("{:?}", href);
/// println!("{:?}", class);
/// }
/// ```
pub fn get_elements_by_tag_name(html: impl ToString, tag: impl ToString) -> Vec<String> {
let html = html.to_string().replace("\n", "");
let a_b = html
.split(format(format_args!("</{}>", tag.to_string())).as_str())
.filter(|s| !s.is_empty())
.filter(|s| s.contains(format(format_args!("<{}", tag.to_string())).as_str()))
.map(|s| {
let a_e = s.replace("\n", "").trim().to_string();
let a_v = a_e
.split(format(format_args!("<{}", tag.to_string())).as_str())
.map(|s| s.to_string())
.collect::<Vec<_>>();
let mut a = a_v.get(1).unwrap().to_string();
a.push_str(format(format_args!("</{}>", tag.to_string())).as_str());
let mut aa = String::from(format(format_args!("<{} ", tag.to_string())).as_str());
aa.push_str(&a);
aa
})
.collect::<Vec<_>>();
a_b
}
///## parse html and get first element by tag name
/// ```rust
/// #[allow(warnings)]
/// fn main() {
/// use loa::{get_elements_by_tag_name, get_first_element_by_tag_name, Getattribute};
/// let html = include_str!("../index.html");
/// let p_list: Vec<String> = loa::get_elements_by_tag_name(html, "p");
/// let a_first: String = loa::get_first_element_by_tag_name(html, "a").unwrap();
/// let href = a_first.get_attribute("href");
/// let class = a_first.get_attribute("class");
/// println!("{:?}", p_list);
/// println!("{:?}", a_first);
/// println!("{:?}", href);
/// println!("{:?}", class);
/// }
/// ```
pub fn get_first_element_by_tag_name(
html: impl ToString,
tag: impl ToString,
) -> Option<String> {
if let Some(node) = get_elements_by_tag_name(html, tag).get(0) {
return Some(node.to_string());
}
None
}
///## get all nods by class name
/// ```ignore
///#[allow(warnings)]
///fn main() {
/// use loa::{
/// get_elements_by_class_name, get_elements_by_tag_name, get_first_element_by_tag_name,
/// Getattribute,
/// };
/// let html = include_str!("../index.html");
/// let all_class = get_elements_by_class_name(html, "cake");
/// println!("{:#?}",all_class);
///}
///```
pub fn get_elements_by_class_name(html: impl ToString, class: impl ToString) -> Vec<String> {
let all_tags = vec![
"!DOCTYPE",
"a",
"abbr",
"acronym",
"abbr",
"address",
"applet",
"object",
"object",
"area",
"article",
"aside",
"audio",
"base",
"basefont",
"bdi",
"bdo",
"big",
"blockquote",
"br",
"button",
"canvas",
"caption",
"center",
"cite",
"code",
"col",
"colgroup",
"data",
"datalist",
"dd",
"del",
"details",
"dfn",
"dialog",
"dir",
"div",
"dl",
"dt",
"em",
"embed",
"fieldset",
"figcaption",
"figure",
"font",
"frame",
"frameset",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"i",
"iframe",
"img",
"input",
"ins",
"kbd",
"label",
"input",
"legend",
"fieldset",
"li",
"link",
"main",
"map",
"mark",
"meta",
"meter",
"nav",
"noframes",
"noscript",
"object",
"ol",
"optgroup",
"option",
"output",
"p",
"param",
"picture",
"pre",
"progress",
"q",
"rp",
"rt",
"ruby",
"s",
"samp",
"script",
"section",
"select",
"small",
"source",
"audio",
"audio",
"span",
"strike",
"del",
"s",
"hr",
"strong",
"style",
"sub",
"summary",
"details",
"sup",
"svg",
"table",
"tbody",
"td",
"template",
"textarea",
"tfoot",
"th",
"thead",
"time",
"title",
"tr",
"track",
"tt",
"u",
"ul",
"var",
"video",
"wbr",
"footer",
"form",
"head",
"header",
"html",
"body",
];
let html = html.to_string().replace("\n", "");
let mut all_nodes: Vec<_> = vec![];
for tag in &all_tags {
let mut nodes = get_elements_by_tag_name(html.to_string(), tag);
if !nodes.is_empty() {
all_nodes.append(&mut nodes);
}
}
let mut all_class_nodes = vec![];
for tag in all_nodes.iter() {
if tag.contains("class") && tag.contains(&class.to_string()) {
if tag.get_attribute("class").is_some() {
if tag
.get_attribute("class")
.unwrap()
.contains(&class.to_string())
{
all_class_nodes.push(tag.to_string().trim().to_string());
}
}
}
}
all_class_nodes
}
///## get first element by class name
/// ```ignore
/// #[allow(warnings)]
///fn main() {
/// use loa::{
/// get_elements_by_class_name, get_elements_by_tag_name,
/// get_first_element_by_tag_name,
/// get_first_element_by_class_name,
/// Getattribute,
/// };
/// let html = include_str!("../index.html");
/// let class = get_first_element_by_class_name(html, "cake");
/// println!("{:#?}",class);
///}
/// ```
pub fn get_first_element_by_class_name(
html: impl ToString,
class: impl ToString,
) -> Option<String> {
if let Some(node) = get_elements_by_class_name(html, class).get(0) {
return Some(node.to_string());
}
None
}
/// ## get all a tags href
/// ```rust
///fn main() {
/// let html = include_str!("../index.html");
/// let re = loa::get_all_a_hrefs(html);
/// println!("{:?}",re.unwrap());
///}
/// ```
pub fn get_all_a_hrefs(html: impl ToString) -> Option<Vec<String>> {
let re_href = fancy_regex::Regex::new(r#"<a .+?\s*href\s*=\s*["']?([^"'\s>]+)["']?"#).unwrap();
let html = html.to_string();
let caps = re_href.captures_iter(&html);
let mut hrefs = vec![];
for i in caps {
let cap = i.expect("capture error") ;
let a = cap.get(0).unwrap().as_str().to_string();
if a.get_attribute("href").is_some(){
hrefs.push(a.get_attribute("href").unwrap());
}
}
if hrefs.is_empty() {
return None;
} else {
return Some(hrefs);
}
}
}