From e3ad319ab37a437d91e6bf348b5f438a5bde0ee8 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Tue, 26 Dec 2023 10:29:18 +0000 Subject: [PATCH] Wait, we can match on strings! Much nicer. --- src/text.rs | 126 ++++++++++++++++++++++++++++------------------------ 1 file changed, 67 insertions(+), 59 deletions(-) diff --git a/src/text.rs b/src/text.rs index e5e684e..aa20f6a 100644 --- a/src/text.rs +++ b/src/text.rs @@ -590,69 +590,77 @@ impl HTMLFormatter { impl html::Receiver for HTMLFormatter { fn start_tag(&mut self, tag: &str, attrs: &HashMap) { - if tag == "a" { - let mut colour = ' '; - if attrs.get("href").is_some() { - colour = 'u'; - } - if let Some(classes) = attrs.get("class") { - if classes.split(' ').any(|x| x == "hashtag") { - colour = '#'; - } else if classes.split(' ').any(|x| x == "mention") { - colour = '@'; + match tag { + "a" => { + let mut colour = ' '; + if attrs.get("href").is_some() { + colour = 'u'; } - } - self.colourstack.push(colour); - } else if tag == "p" { - if !self.last_para().is_empty() { - self.paras.push(Paragraph::new()); - } - self.paras.push(self.new_para()); - } else if tag == "pre" { - if !self.last_para().is_empty() { - self.paras.push(Paragraph::new()); - } - self.paras.push(self.new_para()); - self.pre_tag += 1; - self.colourstack.push('c'); - } else if tag == "br" { - self.paras.push(self.new_para()); - } else if tag == "blockquote" { - self.indent += 2; - self.paras.push(self.new_para()); - } else if tag == "code" { - self.colourstack.push('c'); - } else if tag == "strong" { - self.colourstack.push('s'); - } else if tag == "em" || tag == "i" { - self.colourstack.push('_'); - } 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_owned()); + if let Some(classes) = attrs.get("class") { + if classes.split(' ').any(|x| x == "hashtag") { + colour = '#'; + } else if classes.split(' ').any(|x| x == "mention") { + colour = '@'; + } + } + self.colourstack.push(colour); + }, + "p" => { + if !self.last_para().is_empty() { + self.paras.push(Paragraph::new()); + } + self.paras.push(self.new_para()); + }, + "pre" => { + if !self.last_para().is_empty() { + self.paras.push(Paragraph::new()); + } + self.paras.push(self.new_para()); + self.pre_tag += 1; + self.colourstack.push('c'); + }, + "br" => self.paras.push(self.new_para()), + "blockquote" => { + self.indent += 2; + self.paras.push(self.new_para()); + }, + "code" => self.colourstack.push('c'), + "strong" => self.colourstack.push('s'), + "em" | "i" => self.colourstack.push('_'), + + // do nothing, except don't report these as unknown tags + "span" | "html" | "head" | "body" => (), + + _ => { + self.bad_tags.insert(tag.to_owned()); + }, } } fn end_tag(&mut self, tag: &str, _attrs: &HashMap) { - if tag == "a" || tag == "code" || tag == "strong" || tag == "em" || - tag == "i" { - self.colourstack.pop(); - } else if tag == "p" { - if !self.last_para().is_empty() { - self.paras.push(Paragraph::new()); - } - } else if tag == "pre" { - self.pre_tag -= 1; - self.colourstack.pop(); - if !self.last_para().is_empty() { - self.paras.push(Paragraph::new()); - } - } else if tag == "blockquote" { - if !self.last_para().is_empty() { - self.paras.push(Paragraph::new()); - } - self.indent -= 2; - self.paras.push(self.new_para()); + match tag { + "p" => { + if !self.last_para().is_empty() { + self.paras.push(Paragraph::new()); + } + }, + "pre" => { + self.pre_tag -= 1; + self.colourstack.pop(); + if !self.last_para().is_empty() { + self.paras.push(Paragraph::new()); + } + }, + "blockquote" => { + if !self.last_para().is_empty() { + self.paras.push(Paragraph::new()); + } + self.indent -= 2; + self.paras.push(self.new_para()); + }, + "a" | "code" | "strong" | "em" | "i" => { + self.colourstack.pop(); + }, + _ => (), } } fn text(&mut self, text: &str) { -- 2.30.2