From 4fc5b8b41ee372c2033ee2f76052d64b70ad3e99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?poljar=20=28Damir=20Jeli=C4=87=29?= Date: Thu, 1 Mar 2018 11:55:47 +0100 Subject: [PATCH] colors: Produce email style quotes from html block quotes. --- matrix/colors.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/matrix/colors.py b/matrix/colors.py index 17fb51b..21ed444 100644 --- a/matrix/colors.py +++ b/matrix/colors.py @@ -24,6 +24,7 @@ from collections import namedtuple from matrix.globals import W from matrix.utils import string_strikethrough +import textwrap import webcolors try: @@ -33,6 +34,9 @@ except ImportError: FormattedString = namedtuple('FormattedString', ['text', 'attributes']) +quote_wrapper = textwrap.TextWrapper( + initial_indent="> ", subsequent_indent="> ") + class Formatted(): @@ -228,8 +232,8 @@ class Formatted(): elif name == "strikethrough" and value: return string_strikethrough(string) - elif name == "quote" and value: - return "“{text}”".format(text=string) + if name == "quote" and value: + return quote_wrapper.fill(string.replace("\n", "")) elif name == "fgcolor" and value: return "{color_on}{text}{color_off}".format( @@ -254,7 +258,7 @@ class Formatted(): return text weechat_strings = map(format_string, self.substrings) - return "".join(weechat_strings) + return "".join(weechat_strings).rstrip("\n") # TODO this should be a typed dict. @@ -298,6 +302,14 @@ class MatrixHtmlParser(HTMLParser): self._toggle_attribute("quote") elif tag == "blockquote": self._toggle_attribute("quote") + elif tag == "br": + if self.text: + self.substrings.append( + FormattedString(self.text, self.attributes.copy())) + self.text = "\n" + self.substrings.append( + FormattedString(self.text, DEFAULT_ATRIBUTES.copy())) + self.text = "" elif tag == "font": for key, value in attrs: if key == "color":