moar sciter util functions
This commit is contained in:
parent
c6ada504e4
commit
c4a074183c
2 changed files with 28 additions and 5 deletions
|
@ -1,12 +1,12 @@
|
||||||
use renderers::{sciter_start, get_html};
|
use renderers::{sciter_start, get_html, destroy_matching};
|
||||||
use config::Config;
|
use config::Config;
|
||||||
|
|
||||||
|
|
||||||
pub fn html_cleanup(config: Config, input: String) -> Result<String, String> {
|
pub fn html_cleanup(config: Config, input: String) -> Result<String, String> {
|
||||||
let mut root = sciter_start(input);
|
let mut root = sciter_start(input);
|
||||||
root.find_first("meta[content='text/css']").unwrap().unwrap().destroy().unwrap();
|
destroy_matching(&mut root, "meta[content='text/css']");
|
||||||
root.find_first("style").unwrap().unwrap().destroy().unwrap();
|
destroy_matching(&mut root, "style");
|
||||||
root.find_first("title").unwrap().unwrap().destroy().unwrap();
|
destroy_matching(&mut root, "title");
|
||||||
let html = get_html(root);
|
let html = get_html(root);
|
||||||
return Ok(html);
|
return Ok(html);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,5 +19,28 @@ fn sciter_start(source: String) -> Element {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_html(element: Element) -> String {
|
fn get_html(element: Element) -> String {
|
||||||
return String::from_utf8(element.get_html(true)).expect("Failed to get HTML from element");
|
return String::from_utf8(element.get_html(true)).expect(&format!(
|
||||||
|
"Failed to get HTML from {}.",
|
||||||
|
element.get_tag()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_first(root: &mut Element, selector: &str) -> Element {
|
||||||
|
return root.find_first(selector).expect(&format!("Failed to get {}.", selector)).expect(
|
||||||
|
&format!(
|
||||||
|
"Couldn't find any {}.",
|
||||||
|
selector
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn destroy_matching(root: &mut Element, selector: &str) {
|
||||||
|
let mut matches = root.find_all(selector).expect(&format!("Failed to get {}.", selector));
|
||||||
|
if matches.is_none() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for mut ele in matches.unwrap() {
|
||||||
|
ele.destroy().expect("Failed to delete");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue