This commit is contained in:
2022-02-26 23:56:33 +08:00
commit 48e0a80deb
19 changed files with 4442 additions and 0 deletions

87
src/parser/mainpage.rs Normal file
View File

@@ -0,0 +1,87 @@
use crate::gettext;
use html_parser::Dom;
use html_parser::Node;
use json::JsonValue;
pub struct MainPageParser {
pub value: Option<JsonValue>,
}
impl MainPageParser {
pub fn new() -> Self {
Self {
value: None,
}
}
fn iter(&mut self, node: &Node) -> bool {
match node {
Node::Element(e) => {
if e.name == "meta" {
let name = e.attributes.get("name");
if name.is_none() {
return false;
}
let name = name.unwrap();
if name.is_none() {
return false;
}
if name.as_ref().unwrap() != "global-data" {
return false;
}
if e.id.is_none() {
return false;
}
if e.id.as_ref().unwrap() != "meta-global-data" {
return false;
}
let c = e.attributes.get("content");
if c.is_none() {
return false;
}
let c = c.unwrap();
if c.is_none() {
return false;
}
let r = json::parse(c.as_ref().unwrap());
if r.is_err() {
println!("{} {}", gettext("Failed to parse JSON:"), r.unwrap_err());
return false;
}
self.value = Some(r.unwrap());
true
} else {
for c in e.children.iter() {
if self.iter(c) {
return true;
}
}
false
}
}
Node::Comment(_) => { false }
Node::Text(_) => { false }
}
}
pub fn parse(&mut self, context: &str) -> bool {
let r = Dom::parse(context);
if r.is_err() {
println!("{} {}", gettext("Failed to parse HTML:"), r.unwrap_err());
return false;
}
let dom = r.unwrap();
if dom.errors.len() > 0 {
println!("{}", gettext("Some errors occured during parsing:"));
for i in dom.errors.iter() {
println!("{}", i);
}
}
for n in dom.children.iter() {
if self.iter(n) {
return true;
}
}
false
}
}

3
src/parser/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
extern crate html_parser;
pub mod mainpage;