From: Simon Tatham Date: Sat, 2 Dec 2023 17:48:11 +0000 (+0000) Subject: Handle tags X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=d447ea5970f15859e7445422aa35212763e9d659;p=mastodonochrome.git Handle tags --- diff --git a/text.py b/text.py index 684035a..a57a878 100644 --- 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], # tags + '#': [0, 36], # #hashtags + '@': [0, 32], # @mentions of a user + 's': [0, 1], # 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
, 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 = "

Test of some literal code

" self.assertEqual(self.parse_html(html), [ ColouredString('Test of some literal code', - ' cccccccccccc'), + ' ccccccc cccc'), + ]) + + html = "

Test of some strong text

" + self.assertEqual(self.parse_html(html), [ + ColouredString('Test of some strong text', + ' ssssss ssss'), ]) html = """

Test of a #hashtag

"""