Dry.
This commit is contained in:
+37
@@ -0,0 +1,37 @@
|
||||
use std::str;
|
||||
|
||||
use scraper::{ElementRef, Html};
|
||||
|
||||
pub trait SelectExt {
|
||||
fn element(&self) -> ElementRef;
|
||||
|
||||
fn easy_text(&self) -> String {
|
||||
let data = self
|
||||
.element()
|
||||
.text()
|
||||
.map(str::trim)
|
||||
.filter(|s| !s.is_empty())
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ");
|
||||
|
||||
htmlescape::decode_html(&data).unwrap_or(data)
|
||||
}
|
||||
|
||||
fn easy_inner_html(&self) -> String {
|
||||
let data = self.element().inner_html();
|
||||
|
||||
htmlescape::decode_html(&data).unwrap_or(data)
|
||||
}
|
||||
}
|
||||
|
||||
impl SelectExt for Html {
|
||||
fn element(&self) -> ElementRef {
|
||||
self.root_element()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> SelectExt for ElementRef<'a> {
|
||||
fn element(&self) -> ElementRef {
|
||||
*self
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user