>) -> HandleResult {
+// let attrs = attrs.borrow();
+// let rels = attrs
+// .iter()
+// .find(|attr| attr.name.local.to_string() == "rel");
+// let hrefs = attrs
+// .iter()
+// .find(|attr| attr.name.local.to_string() == "href");
+// match (rels, hrefs) {
+// (Some(rel), Some(href)) => {
+// if !rel.value.to_string().contains("tag") {
+// let new_node = Node::link(&href.value);
+// node.add_child(new_node);
+// HandleResult::NewNode(new_node)
+// } else {
+// HandleResult::Keep
+// }
+// }
+// _ => HandleResult::Stop,
+// }
+// }
+
fn walk(input: &Handle, parent: &mut Node) {
- println!(">>> {:?}", input.data);
- let process_children = match input.data {
+ // println!(">>> {:?}", input.data);
+ let element = match input.data {
NodeData::Text { ref contents } => handle_text(parent, contents),
NodeData::Element {
ref name,
@@ -145,67 +190,68 @@ fn walk(input: &Handle, parent: &mut Node) {
..
} => {
let tag = name.local.to_string();
- println!("Tag: {:?}", tag);
match tag.as_ref() {
- "html" | "head" | "body" => true, // just keep going
- "p" => handle_line_break(parent),
- "span" => handle_span(parent, attrs),
- "a" => {
- println!("\tAnchor");
- if let NodeData::Element { ref attrs, .. } = input.data {
- let attrs = attrs.borrow();
- let rels = attrs
- .iter()
- .find(|attr| attr.name.local.to_string() == "rel");
- let hrefs = attrs
- .iter()
- .find(|attr| attr.name.local.to_string() == "href");
- match (rels, hrefs) {
- (Some(rel), Some(href)) => {
- if !rel.value.to_string().contains("tag") {
- result.push_str("[[");
- result.push_str(&href.value.to_string());
- result.push_str("][");
- go_children(input, result);
- result.push_str("]]");
- } else {
- go_children(input, result);
- }
- }
- _ => {}
- }
- }
- }
- _ => false,
+ "html" | "head" | "body" => HandleResult::Follow,
+ "p" => handle_line_break(),
+ "span" => handle_span(attrs),
+ // "a" => handle_anchor(parent, attrs),
+ _ => HandleResult::Stop,
}
}
- _ => true, // if we can't deal with it, just keep going
+ _ => HandleResult::Follow, // if we can't deal with it, just keep going
};
- if process_children {
- for child in input.children.borrow().iter() {
- walk(child.borrow(), parent);
+ match element {
+ HandleResult::Stop => {}
+ HandleResult::Follow => {
+ for child in input.children.borrow().iter() {
+ walk(child.borrow(), parent);
+ }
+ }
+ // HandleResult::AddAndStay(new_node) => {
+ // parent.add_child(new_node);
+ // for child in input.children.borrow().iter() {
+ // walk(child.borrow(), parent);
+ // }
+ // }
+ HandleResult::AddAndAdopt(mut new_node) => {
+ for child in input.children.borrow().iter() {
+ walk(child.borrow(), &mut new_node);
+ }
+ parent.add_child(new_node);
}
}
}
-fn main() {
- let source = String::from(
- r#"Today I finally moved with my contact and calendar management into the terminal with #vdirsyncer #khal and #khard.
Thank you @hund for your great post: https://hund.tty1.se/2020/08/12/how-to-sync-and-manage-your-caldav-and-carddav-via-the-terminal.html
#carddav #caldav #terminal
"#,
- );
- println!("Source: {}", &source);
- println!("---------------------------------");
-
+fn build_nodes(text: &str) {
let dom = parse_document(RcDom::default(), Default::default())
.from_utf8()
- .read_from(&mut source.as_bytes())
+ .read_from(&mut text.as_bytes())
.unwrap();
let mut tree = Node::root();
- walk(&dom.document, &mut result);
- println!("---------------------------------");
- let options = Options::new(70)
- .initial_indent(" ")
- .subsequent_indent(" ")
- .splitter(NoHyphenation);
- println!("{}", fill(&result.trim(), &options));
+ walk(&dom.document, &mut tree);
+ println!("Tree: {:?}", tree);
+}
+
+fn main() {
+ let example_1 = String::from(r#"A simple text component
"#);
+ build_nodes(&example_1);
+
+ let example_2 = String::from(
+ r#"THis is not visiblebut this is
"#,
+ );
+ build_nodes(&example_2);
+
+ // let example_1 = String::from(
+ // r#"Today I finally moved with my contact and calendar management into the terminal with #vdirsyncer #khal and #khard.
Thank you @hund for your great post: https://hund.tty1.se/2020/08/12/how-to-sync-and-manage-your-caldav-and-carddav-via-the-terminal.html
#carddav #caldav #terminal
"#,
+ // );
+ // println!("Source: {}", &example_1);
+ // println!("---------------------------------");
+
+ // let dom = parse_document(RcDom::default(), Default::default())
+ // .from_utf8()
+ // .read_from(&mut example_1.as_bytes())
+ // .unwrap();
+ // let mut tree = Node::root();
+ // walk(&dom.document, &mut tree);
}