'-': [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 = {}
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):
self.colourstack = [' ']
self.bad_tags = set()
self.indent = 0
+ self.pre_tag = 0
def new_para(self):
return (Paragraph() if self.indent == 0
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
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())
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: