New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
vCard/JSON extraction #19443
vCard/JSON extraction #19443
Changes from 2 commits
eb2c7e0
015ef37
43aeb7c
f306835
0530512
f939632
3d68d2a
a4ed961
4628943
cbfc666
146dd58
9284187
dd4dc70
43650c2
19408a0
79d140d
aab6900
35468f8
8cafd64
87d1efa
c580c3f
3cd9731
d0cb12e
0ae2d08
7131823
a3e5b9f
0e5023d
37f70c6
543de1c
a781bba
04a043d
c740aab
File filter...
Jump to…
| @@ -923,7 +923,7 @@ impl Element { | ||
| &self.local_name | ||
| } | ||
|
|
||
| pub fn tag_name(&self) -> DOMString{ | ||
| pub fn tag_name(&self) -> DOMString { | ||
| self.TagName() | ||
vjhebbar
Contributor
|
||
| } | ||
|
|
||
| @@ -12,39 +12,38 @@ use dom::element::Element; | ||
| use dom::htmlelement::HTMLElement; | ||
| use dom::node::Node; | ||
| use dom::text::Text; | ||
| use serde_json; | ||
| use std::borrow::Cow; | ||
| use std::collections::HashMap; | ||
| use serde_json; | ||
|
|
||
| pub struct Microdata {} | ||
jdm
Member
|
||
| #[derive(Serialize,Clone)] | ||
| #[derive(Clone,Serialize)] | ||
| #[serde(untagged)] | ||
| enum Data { | ||
| StrValue(String), | ||
| VecValue(Vec<Data>), | ||
| DataValue(Box<Data>), | ||
| HashValue(HashMap<String,Data>) | ||
| HashValue(HashMap<String, Data>) | ||
| } | ||
|
|
||
| impl Microdata { | ||
| //[Pref="dom.microdata.testing.enabled"] | ||
| pub fn parse(doc: &Document, node : &Node) -> HashMap<String, String> { | ||
| pub fn parse(doc: &Document, node: &Node) -> HashMap<String, String> { | ||
jdm
Member
|
||
| //let dup_doc = Cow::Borrowed(doc); | ||
| let serialized_vcard = Self::parse_vcard(doc); | ||
| let serialized_json = Self::parse_json(node); | ||
| let mut serialized_data : HashMap<String, String> = HashMap::new(); | ||
| let mut serialized_data: HashMap<String, String> = HashMap::new(); | ||
| serialized_data.insert("vcard".to_string(), serialized_vcard); | ||
| serialized_data.insert("json".to_string(), serialized_json); | ||
| return serialized_data; | ||
| } | ||
|
|
||
| pub fn parse_vcard(doc: &Document) -> String { | ||
|
|
||
| let ele = doc.upcast::<Node>(); | ||
| let mut start_vcard = false; | ||
| let mut result : String = String::new(); | ||
| let mut master_map : HashMap<String, HashMap<String, String>> = HashMap::new(); | ||
| let mut master_key : String = String::new(); | ||
| let mut result: String = String::new(); | ||
| let mut master_map: HashMap<String, HashMap<String, String>> = HashMap::new(); | ||
| let mut master_key: String = String::new(); | ||
|
|
||
| result += "BEGIN:VCARD\nPROFILE:VCARD\nVERSION:4.0\nSOURCE:"; | ||
| result += doc.url().as_str(); | ||
| @@ -57,13 +56,13 @@ impl Microdata { | ||
|
|
||
| result += "\n"; | ||
|
|
||
| for element in ele.traverse_preorder().filter_map(DomRoot::downcast::<Element>){ | ||
| for element in ele.traverse_preorder().filter_map(DomRoot::downcast::<Element>) { | ||
jdm
Member
|
||
| if element.is::<HTMLElement>() { | ||
| if element.has_attribute(&local_name!("itemtype")){ | ||
| if element.has_attribute(&local_name!("itemtype")) { | ||
| let mut atoms = element.get_tokenlist_attribute(&local_name!("itemtype"), ); | ||
| if !atoms.is_empty() { | ||
| let val = atoms.remove(0); | ||
| if val.trim() == "http://microformats.org/profile/hcard"{ | ||
| if val.trim() == "http://microformats.org/profile/hcard" { | ||
| if !start_vcard { | ||
| start_vcard = true; | ||
| } else { | ||
| @@ -76,7 +75,7 @@ impl Microdata { | ||
| let mut atoms = element.get_tokenlist_attribute(&local_name!("itemprop"), ); | ||
| if !atoms.is_empty() { | ||
| let temp_key = atoms.remove(0); | ||
| if element.has_attribute(&local_name!("itemscope")){ | ||
| if element.has_attribute(&local_name!("itemscope")) { | ||
| master_key = String::from(temp_key.trim()).to_owned(); | ||
| let dup_master_key = Cow::Borrowed(&master_key); | ||
| master_map.entry(dup_master_key.to_string()).or_insert(HashMap::new()); | ||
| @@ -94,55 +93,49 @@ impl Microdata { | ||
| } | ||
|
|
||
| for (info_type, detail_map) in &master_map { | ||
|
|
||
| match info_type.as_str() { | ||
| "n" => { | ||
| let mut n_value: String = String::new(); | ||
|
|
||
| let mut n_value : String = String::new(); | ||
|
|
||
| let name_parts = ["family-name", "given-name", "additional-name", "honorific-prefix", "honorific-suffix"]; | ||
| let name_parts = ["family-name", "given-name", | ||
| "additional-name", "honorific-prefix", "honorific-suffix"]; | ||
| for part in name_parts.iter() { | ||
| if detail_map.contains_key(*part) { | ||
| n_value += format!("{};",detail_map.get(*part).unwrap()).as_str(); | ||
| n_value += format!("{};", detail_map.get(*part).unwrap()).as_str(); | ||
| } | ||
| } | ||
| n_value.pop(); | ||
|
|
||
| result += format!("{}:{}\n", info_type.as_str(), n_value).as_str(); | ||
|
|
||
| }, | ||
| "org" => { | ||
|
|
||
| let mut org_value : String = String::new(); | ||
| let mut org_value: String = String::new(); | ||
|
|
||
| let org_parts = ["organization-name", "organization-unit"]; | ||
| for part in org_parts.iter() { | ||
| if detail_map.contains_key(*part) { | ||
| org_value += format!("{};",detail_map.get(*part).unwrap()).as_str(); | ||
| org_value += format!("{};", detail_map.get(*part).unwrap()).as_str(); | ||
| } | ||
| } | ||
| org_value.pop(); | ||
|
|
||
| result += format!("{}:{}\n", info_type.as_str(), org_value).as_str(); | ||
|
|
||
| }, | ||
| "tel" => { | ||
|
|
||
| }, | ||
| "adr" => { | ||
| let mut adr_value: String = String::new(); | ||
|
|
||
| let mut adr_value : String = String::new(); | ||
|
|
||
| let adr_parts = ["street-address", "locality", "region", "postal-code", "country-name", "post-office-box", "extended-address"]; | ||
| let adr_parts = ["street-address", "locality", "region", "postal-code", | ||
| "country-name", "post-office-box", "extended-address"]; | ||
| for part in adr_parts.iter() { | ||
| if detail_map.contains_key(*part) { | ||
| adr_value += format!("{};",detail_map.get(*part).unwrap()).as_str(); | ||
| adr_value += format!("{};", detail_map.get(*part).unwrap()).as_str(); | ||
| } | ||
| } | ||
| adr_value.pop(); | ||
|
|
||
| result += format!("{}:{}\n", info_type.as_str(), adr_value).as_str(); | ||
|
|
||
| }, | ||
| _ => {}, | ||
| } | ||
| @@ -154,14 +147,14 @@ impl Microdata { | ||
| } | ||
|
|
||
| pub fn parse_json(node: &Node) -> String { | ||
| let json_data : Data = Self::traverse(node).unwrap(); | ||
| let json_data: Data = Self::traverse(node).unwrap(); | ||
| let json = serde_json::to_string(&json_data); | ||
| //println!("printing json from microdata {:?}", json); | ||
| return json.ok().unwrap(); | ||
| } | ||
|
|
||
| fn get_attr_value(element: &Element, property: &str)-> Option<String> { | ||
| println!("{:?}",property); | ||
| fn get_attr_value(element: &Element, property: &str) -> Option<String> { | ||
| println!("{:?}", property); | ||
| // let mut atoms = match property { | ||
| // "itemprop" => element.get_tokenlist_attribute(&local_name!("itemprop"), ), | ||
| // "itemtype" => element.get_tokenlist_attribute(&local_name!("itemtype"), ), | ||
| @@ -177,43 +170,44 @@ impl Microdata { | ||
| Some(String::from("itemprop")) | ||
| } | ||
|
|
||
| fn traverse( node: &Node)-> Option<Data> { | ||
| if !node.is::<Element>(){ | ||
| if let Some(ref text) = node.downcast::<Text>(){ | ||
| fn traverse(node: &Node) -> Option<Data> { | ||
| if !node.is::<Element>() { | ||
| if let Some(ref text) = node.downcast::<Text>() { | ||
| let mut content = String::new(); | ||
| content.push_str(&text.upcast::<CharacterData>().data()); | ||
| return Some(Data::StrValue(String::from(content))); | ||
| } | ||
| None | ||
| } | ||
| else { | ||
| } else { | ||
| let element = node.downcast::<Element>().unwrap(); | ||
| let mut head_str = String::from(""); | ||
| let mut parent_vec:Vec<Data> = Vec::new(); | ||
| let item_type = Self::get_attr_value(element,"itemtype").unwrap(); | ||
| // if element.has_attribute(&local_name!("itemscope")) && element.has_attribute(&local_name!("itemtype")) && !element.has_attribute(&local_name!("itemprop")) { | ||
| let mut parent_vec: Vec<Data> = Vec::new(); | ||
| let item_type = Self::get_attr_value(element, "itemtype").unwrap(); | ||
| // if element.has_attribute(&local_name!("itemscope")) && | ||
| // element.has_attribute(&local_name!("itemtype")) && | ||
| // !element.has_attribute(&local_name!("itemprop")) { | ||
| // head_str = String::from("items"); | ||
| // let mut propMap:HashMap<String,Data> = HashMap::new(); | ||
| // //Data::HashValue(propMap) | ||
| // let item_type = Self::get_attr_value(element,"item_type").unwrap(); | ||
|
|
||
| // } | ||
| // else if element.has_attribute(&local_name!("itemprop")) && element.has_attribute(&local_name!("item_type")) { | ||
| // else if element.has_attribute(&local_name!("itemprop")) && | ||
| // element.has_attribute(&local_name!("item_type")) { | ||
|
|
||
| // head_str = Self::get_attr_value(element,"itemprop").unwrap(); | ||
| // let item_type = Self::get_attr_value(element,"item_type").unwrap(); | ||
|
|
||
| // } | ||
| // else { | ||
| // } else { | ||
| // return None; | ||
| // } | ||
| let mut inner_map:HashMap<String,Data> = HashMap::new(); | ||
| for child in node.children(){ | ||
| if let Some(childData) = Self::traverse(child.upcast::<Node>()){ | ||
| let mut inner_map: HashMap<String, Data> = HashMap::new(); | ||
| for child in node.children() { | ||
| if let Some(childData) = Self::traverse(child.upcast::<Node>()) { | ||
| parent_vec.push(childData); | ||
| } | ||
| } | ||
| inner_map.insert(head_str,Data::VecValue(parent_vec)); | ||
| inner_map.insert(head_str, Data::VecValue(parent_vec)); | ||
| Some(Data::HashValue(inner_map)) | ||
| } | ||
| } | ||
You should be able to import ElementMethods and invoke TagName() directly from the caller.