chiark / gitweb /
Diagnose unsupported markup tags
authorSimon Tatham <anakin@pobox.com>
Mon, 25 Dec 2023 20:06:24 +0000 (20:06 +0000)
committerSimon Tatham <anakin@pobox.com>
Mon, 25 Dec 2023 20:08:32 +0000 (20:08 +0000)
src/text.rs

index 2bde3b758993521cd92913fadbe74c7bfe1c5b1b..b61efa9ac2c2169c2e5662bafe9331652aba7e12 100644 (file)
@@ -1,6 +1,6 @@
 use chrono::{DateTime,Utc,Local};
 use core::cmp::max;
-use std::collections::{HashMap, HashSet};
+use std::collections::{HashMap, BTreeSet};
 
 use super::html;
 use super::coloured_string::{ColouredString, ColouredStringSlice};
@@ -507,7 +507,7 @@ fn test_fileheader() {
 struct HTMLFormatter {
     paras: Vec<Paragraph>,
     colourstack: Vec<char>,
-    bad_tags: HashSet<String>,
+    bad_tags: BTreeSet<String>, // so we report more than 1 in consistent order
     indent: usize,
     pre_tag: usize,
 }
@@ -517,7 +517,7 @@ impl HTMLFormatter {
         HTMLFormatter {
             paras: vec! { Paragraph::new() },
             colourstack: vec! { ' ' },
-            bad_tags: HashSet::new(),
+            bad_tags: BTreeSet::new(),
             indent: 0,
             pre_tag: 0,
         }
@@ -526,7 +526,7 @@ impl HTMLFormatter {
     fn finish(mut self) -> Vec<Paragraph> {
         let first_nonempty = self.paras.iter()
             .position(|p| !p.is_empty()).unwrap_or(self.paras.len());
-        self.paras.splice(..first_nonempty, vec![]);
+        self.paras.splice(..first_nonempty, vec!{});
 
         while match self.paras.last() {
             Some(p) => p.is_empty(),
@@ -535,6 +535,19 @@ impl HTMLFormatter {
             self.paras.pop();
         }
 
+        if !self.bad_tags.is_empty() {
+            let mut para = Paragraph::new().add(
+                &ColouredString::uniform("Unsupported markup tags:", '!'));
+            for tag in self.bad_tags.iter() {
+                para.push_text(&ColouredString::uniform(
+                        &format!(" <{}>", tag), '!'), false);
+            }
+            self.paras.splice(0..0, vec!{
+                    para,
+                    Paragraph::new(),
+                });
+        }
+
         self.paras
     }
 
@@ -588,7 +601,8 @@ impl html::Receiver for HTMLFormatter {
             self.colourstack.push('s');
         } else if tag == "em" || tag == "i" {
             self.colourstack.push('_');
-        } else if tag == "span" {
+        } else if tag == "span" || tag == "html" || tag == "head" ||
+            tag == "body" {
             // do nothing, except don't report this as an unknown tag
         } else {
             self.bad_tags.insert(tag.to_string());
@@ -709,6 +723,14 @@ fn test_html() {
             ColouredString::general("URL to https://www.example.com/stuff/.",
                                     "       uuuuuuuuuuuuuuuuuuuuuuuuuuuuuu "),
         });
+
+    assert_eq!(render_html("<p>Test of some <nonsense>unsupported</nonsense> <blither>HTML tags</blither></p>", 50),
+               vec! {
+            ColouredString::general("Unsupported markup tags: <blither> <nonsense>",
+                                    "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"),
+            ColouredString::plain(""),
+            ColouredString::plain("Test of some unsupported HTML tags"),
+        });
 }
 
 // TODO: