chiark / gitweb /
Wait, we can match on strings! Much nicer.
authorSimon Tatham <anakin@pobox.com>
Tue, 26 Dec 2023 10:29:18 +0000 (10:29 +0000)
committerSimon Tatham <anakin@pobox.com>
Tue, 26 Dec 2023 10:29:18 +0000 (10:29 +0000)
src/text.rs

index e5e684ee90368c114fb3a626cc08aeba5da3d669..aa20f6a91e7c9ea306459a94ce2f322d09d38383 100644 (file)
@@ -590,69 +590,77 @@ impl HTMLFormatter {
 
 impl html::Receiver for HTMLFormatter {
     fn start_tag(&mut self, tag: &str, attrs: &HashMap<String, String>) {
-        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<String, String>) {
-        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) {