From 6cd651ab1d4ffeb74bf9f320e95ed02562562c9b Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Mon, 25 Dec 2023 20:06:24 +0000 Subject: [PATCH] Diagnose unsupported markup tags --- src/text.rs | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/text.rs b/src/text.rs index 2bde3b7..b61efa9 100644 --- a/src/text.rs +++ b/src/text.rs @@ -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, colourstack: Vec, - bad_tags: HashSet, + bad_tags: BTreeSet, // 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 { 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("

Test of some unsupported HTML tags

", 50), + vec! { + ColouredString::general("Unsupported markup tags: ", + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"), + ColouredString::plain(""), + ColouredString::plain("Test of some unsupported HTML tags"), + }); } // TODO: -- 2.30.2