chiark / gitweb /
Get the QualNames out of my html::Receiver.
authorSimon Tatham <anakin@pobox.com>
Mon, 25 Dec 2023 15:56:39 +0000 (15:56 +0000)
committerSimon Tatham <anakin@pobox.com>
Mon, 25 Dec 2023 15:56:39 +0000 (15:56 +0000)
src/coloured_string.rs
src/html.rs
src/lib.rs
src/main.rs

index 6a3f96590f1fcbdf1553e76bbd8776c570f04ad4..a6fae9828179e9c52605a205f106f9eb251b1570 100644 (file)
@@ -130,6 +130,16 @@ impl std::ops::Add<ColouredString> for &ColouredString {
     }
 }
 
+impl std::ops::Add<ColouredStringSlice<'_>> 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<ColouredString> for &str {
     type Output = ColouredString;
     fn add(self, rhs: ColouredString) -> ColouredString {
index 39525780df2f9a271fba73a89c9a5fe758804981..b7639c6b3a4893a98da9e6455162106e2c00522c 100644 (file)
@@ -31,11 +31,20 @@ struct Tree {
 }
 
 pub trait Receiver {
-    fn start_tag(&mut self, tag: &QualName, attrs: &HashMap<QualName, String>);
-    fn end_tag(&mut self, tag: &QualName, attrs: &HashMap<QualName, String>);
+    fn start_tag(&mut self, tag: &str, attrs: &HashMap<String, String>);
+    fn end_tag(&mut self, tag: &str, attrs: &HashMap<String, String>);
     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);
             },
             _ => (),
         };
index fffce55b05e915d4e468cf47eecdd488c1a57fac..25e9acd0e2b7f4af2d907b6263471126ed7da90f 100644 (file)
@@ -1,3 +1,6 @@
+#[macro_use]
+extern crate html5ever;
+
 pub mod types;
 pub mod auth;
 pub mod html;
index fe2606c8517fb32b7de44d9b675a23121a44eec5..f2beb3f5c9800ef05a324e095ee29c07b2a17fe9 100644 (file)
@@ -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<QualName, String>) {
+    fn start_tag(&mut self, tag: &str, attrs: &HashMap<String, String>) {
         dbg!("start", tag, attrs);
     }
-    fn end_tag(&mut self, tag: &QualName, attrs: &HashMap<QualName, String>) {
+    fn end_tag(&mut self, tag: &str, attrs: &HashMap<String, String>) {
         dbg!("end", tag, attrs);
     }
     fn text(&mut self, text: &str) {