From: Simon Tatham Date: Sun, 17 Dec 2023 11:49:58 +0000 (+0000) Subject: Support
 tag in post HTML.
X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=b0a6f61b8e62ff2ff3ba6f82d67706a839ff131e;p=mastodonochrome.git

Support 
 tag in post HTML.
---

diff --git a/text.py b/text.py
index 3135abd..0f1b9f3 100644
--- a/text.py
+++ b/text.py
@@ -35,6 +35,7 @@ colourmap = {
     '-': [0, 7, 40, 36], # separator line between editor header and content
     '0': [0, 34], # something really boring, like 'none' in place of data
     'r': [0, 31], # red nastinesses like blocking/muting in Examine User
+    '>': [0, 7], # reverse-video > indicating a truncated too-long line
 }
 
 wcswidth_cache = {}
@@ -323,15 +324,34 @@ class Paragraph:
         self.words = []
         self.space_colours = []
         self.unfinished_word = ColouredString('')
+        self.wrap = True
 
         if text is not None:
             self.add(text)
             self.end_word()
 
+    def set_wrap(self, wrap):
+        self.wrap = wrap
+
     def render(self, width, laterwidth=None):
         if laterwidth is None:
             laterwidth = width
 
+        if not self.wrap:
+            line, space = ColouredString(''), ColouredString('')
+            for word, space_colour in zip(self.words, self.space_colours):
+                oldlen = len(line)
+                line += space + word
+                space = ColouredString(' ', space_colour)
+                if line.width >= width:
+                    line = next(line.split(width-2))
+                    while line.width < width-2:
+                        line += ' '
+                    line += ColouredString(">", ">")
+                    break
+            yield line
+            return
+
         # For the moment, greedy algorithm. We can worry about cleverness later
         line, space = ColouredString(''), ColouredString('')
         for word, space_colour in zip(self.words, self.space_colours):
@@ -477,6 +497,7 @@ class HTMLParser(html.parser.HTMLParser):
         self.colourstack = [' ']
         self.bad_tags = set()
         self.indent = 0
+        self.pre_tag = 0
 
     def new_para(self):
         return (Paragraph() if self.indent == 0
@@ -502,6 +523,14 @@ class HTMLParser(html.parser.HTMLParser):
             self.paras.append(self.new_para())
             return
 
+        if tag == "pre":
+            if not self.paras[-1].empty():
+                self.paras.append(Paragraph())
+            self.paras.append(self.new_para())
+            self.pre_tag += 1
+            self.colourstack.append('c')
+            return
+
         if tag == "br":
             self.paras.append(self.new_para())
             return
@@ -539,6 +568,13 @@ class HTMLParser(html.parser.HTMLParser):
                 self.paras.append(self.new_para())
             return
 
+        if tag == "pre":
+            self.pre_tag -= 1
+            self.colourstack.pop()
+            if not self.paras[-1].empty():
+                self.paras.append(self.new_para())
+            return
+
         if tag == "blockquote":
             if not self.paras[-1].empty():
                 self.paras.append(Paragraph())
@@ -551,8 +587,18 @@ class HTMLParser(html.parser.HTMLParser):
             return
 
     def handle_data(self, data):
-        data = data.replace('\n', ' ')
-        self.paras[-1].add(ColouredString(data, self.colourstack[-1]))
+        if self.pre_tag > 0:
+            def add_pre_text(data):
+                self.paras[-1].set_wrap(False)
+                self.paras[-1].add(ColouredString(data, self.colourstack[-1]))
+            lines = list(data.split('\n'))
+            for i, line in enumerate(lines):
+                add_pre_text(line)
+                if i + 1 < len(lines):
+                    self.paras.append(self.new_para())
+        else:
+            data = data.replace('\n', ' ')
+            self.paras[-1].add(ColouredString(data, self.colourstack[-1]))
 
     def done(self):
         for para in self.paras: