From 1dda60d3bd87c79f890f489a9a36d5e70530c035 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Mon, 25 Dec 2023 15:56:39 +0000 Subject: [PATCH] Get the QualNames out of my html::Receiver. --- src/coloured_string.rs | 10 ++++++++++ src/html.rs | 20 +++++++++++++++----- src/lib.rs | 3 +++ src/main.rs | 6 +++--- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/coloured_string.rs b/src/coloured_string.rs index 6a3f965..a6fae98 100644 --- a/src/coloured_string.rs +++ b/src/coloured_string.rs @@ -130,6 +130,16 @@ impl std::ops::Add for &ColouredString { } } +impl std::ops::Add> for ColouredString { + type Output = ColouredString; + fn add(self, rhs: ColouredStringSlice<'_>) -> ColouredString { + ColouredString { + text: self.text.to_string() + &rhs.text, + colour: self.colour.to_string() + &rhs.colour, + } + } +} + impl std::ops::Add for &str { type Output = ColouredString; fn add(self, rhs: ColouredString) -> ColouredString { diff --git a/src/html.rs b/src/html.rs index 3952578..b7639c6 100644 --- a/src/html.rs +++ b/src/html.rs @@ -31,11 +31,20 @@ struct Tree { } pub trait Receiver { - fn start_tag(&mut self, tag: &QualName, attrs: &HashMap); - fn end_tag(&mut self, tag: &QualName, attrs: &HashMap); + fn start_tag(&mut self, tag: &str, attrs: &HashMap); + fn end_tag(&mut self, tag: &str, attrs: &HashMap); fn text(&mut self, text: &str); } +fn qualname_to_string(qn: &QualName) -> String { + if qn.ns == ns!(html) || qn.ns == ns!() { + qn.local.to_string() + } else { + dbg!(&qn); + format!("{}:{}", qn.ns.to_string(), qn.local.to_string()) + } +} + impl Tree { fn new_node(&mut self, contents: TreeNodeContents) -> Handle { let handle = self.nodes.len(); @@ -103,14 +112,15 @@ impl Tree { TreeNodeContents::Element { name, attrs, children } => { let mut attrmap = HashMap::new(); for attr in attrs { - attrmap.insert(attr.name.clone(), + attrmap.insert(qualname_to_string(&attr.name), attr.value.to_string()); } - receiver.start_tag(&name, &attrmap); + let tagname = qualname_to_string(&name); + receiver.start_tag(&tagname, &attrmap); for child in children { self.walk_recurse(*child, receiver); } - receiver.end_tag(&name, &attrmap); + receiver.end_tag(&tagname, &attrmap); }, _ => (), }; diff --git a/src/lib.rs b/src/lib.rs index fffce55..25e9acd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,6 @@ +#[macro_use] +extern crate html5ever; + pub mod types; pub mod auth; pub mod html; diff --git a/src/main.rs b/src/main.rs index fe2606c..f2beb3f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use mastodonochrome::types::*; use mastodonochrome::OurError; use mastodonochrome::auth::AuthConfig; -use mastodonochrome::html::{render, QualName}; +use mastodonochrome::html::{render}; use mastodonochrome::html; use std::collections::HashMap; use std::io::Read; @@ -117,10 +117,10 @@ fn tui() -> std::io::Result<()> { struct TestReceiver {} impl html::Receiver for TestReceiver { - fn start_tag(&mut self, tag: &QualName, attrs: &HashMap) { + fn start_tag(&mut self, tag: &str, attrs: &HashMap) { dbg!("start", tag, attrs); } - fn end_tag(&mut self, tag: &QualName, attrs: &HashMap) { + fn end_tag(&mut self, tag: &str, attrs: &HashMap) { dbg!("end", tag, attrs); } fn text(&mut self, text: &str) { -- 2.30.2