chiark / gitweb /
Handle <strong> tags
authorSimon Tatham <anakin@pobox.com>
Sat, 2 Dec 2023 17:48:11 +0000 (17:48 +0000)
committerSimon Tatham <anakin@pobox.com>
Sat, 2 Dec 2023 17:48:11 +0000 (17:48 +0000)
text.py

diff --git a/text.py b/text.py
index 684035a5f5bd8b24158ce0977d309b7157f4160a..a57a878618da3c709fbc95e79c511113235f6c8e 100644 (file)
--- a/text.py
+++ b/text.py
@@ -12,16 +12,18 @@ import wcwidth
 # data that converts each one into a list of integers that go in an
 # ECMA-48 style SGR control sequence.
 colourmap = {
-    ' ': [],
-    'S': [0, 1, 7, 44, 37],
-    'D': [0, 7, 44, 37],
-    'F': [0, 1, 32],
-    'f': [0, 32],
-    'c': [0, 33],
-    '#': [0, 36],
-    '@': [0, 32],
-    'M': [0, 1, 4, 35],
-    'm': [0, 35],
+    ' ': [], # default
+    'S': [0, 1, 7, 44, 37], # message separator line, other than the date
+    'D': [0, 7, 44, 37], # date on a message separator line
+    'F': [0, 1, 32], # username in a From line
+    'f': [0, 32], # username in other headers like Via
+    'c': [0, 33], # <code> tags
+    '#': [0, 36], # #hashtags
+    '@': [0, 32], # @mentions of a user
+    's': [0, 1], # <strong> tags
+    'u': [0, 1, 4, 34], # URL
+    'M': [0, 1, 4, 35], # media URL
+    'm': [0, 35], # media description
 }
 
 class ColouredString:
@@ -191,7 +193,8 @@ class HTMLParser(html.parser.HTMLParser):
         if tag == "a":
             classes = set(attrdict.get("class", "").split())
             colour = ("#" if "hashtag" in classes else
-                      "@" if "mention" in classes else " ")
+                      "@" if "mention" in classes else
+                      "u" if "href" in attrdict else " ")
             self.colourstack.append(colour)
             return
 
@@ -212,6 +215,10 @@ class HTMLParser(html.parser.HTMLParser):
             self.colourstack.append('c')
             return
 
+        if tag == "strong":
+            self.colourstack.append('s')
+            return
+
         # FIXME: need <pre>, e.g. in
         # https://neuromatch.social/@mstimberg/111375114784712346
         # and _perhaps_ that ought to generate paragraphs with a
@@ -228,7 +235,7 @@ class HTMLParser(html.parser.HTMLParser):
                 self.paras.append(Paragraph())
             return
 
-        if tag in {"a", "code"}:
+        if tag in {"a", "code", "strong"}:
             self.colourstack.pop()
             return
 
@@ -300,7 +307,13 @@ class RenderTests(unittest.TestCase):
         html = "<p>Test of some <code>literal code</code></p>"
         self.assertEqual(self.parse_html(html), [
             ColouredString('Test of some literal code',
-                           '             cccccccccccc'),
+                           '             ccccccc cccc'),
+        ])
+
+        html = "<p>Test of some <strong>strong text</strong></p>"
+        self.assertEqual(self.parse_html(html), [
+            ColouredString('Test of some strong text',
+                           '             ssssss ssss'),
         ])
 
         html = """<p>Test of a <a href="https://some.instance/tags/hashtag" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>hashtag</span></a></p>"""