colors: Produce email style quotes from html block quotes.

This commit is contained in:
poljar (Damir Jelić) 2018-03-01 11:55:47 +01:00
parent a606ac088a
commit 4fc5b8b41e

View file

@ -24,6 +24,7 @@ from collections import namedtuple
from matrix.globals import W from matrix.globals import W
from matrix.utils import string_strikethrough from matrix.utils import string_strikethrough
import textwrap
import webcolors import webcolors
try: try:
@ -33,6 +34,9 @@ except ImportError:
FormattedString = namedtuple('FormattedString', ['text', 'attributes']) FormattedString = namedtuple('FormattedString', ['text', 'attributes'])
quote_wrapper = textwrap.TextWrapper(
initial_indent="> ", subsequent_indent="> ")
class Formatted(): class Formatted():
@ -228,8 +232,8 @@ class Formatted():
elif name == "strikethrough" and value: elif name == "strikethrough" and value:
return string_strikethrough(string) return string_strikethrough(string)
elif name == "quote" and value: if name == "quote" and value:
return "{text}".format(text=string) return quote_wrapper.fill(string.replace("\n", ""))
elif name == "fgcolor" and value: elif name == "fgcolor" and value:
return "{color_on}{text}{color_off}".format( return "{color_on}{text}{color_off}".format(
@ -254,7 +258,7 @@ class Formatted():
return text return text
weechat_strings = map(format_string, self.substrings) weechat_strings = map(format_string, self.substrings)
return "".join(weechat_strings) return "".join(weechat_strings).rstrip("\n")
# TODO this should be a typed dict. # TODO this should be a typed dict.
@ -298,6 +302,14 @@ class MatrixHtmlParser(HTMLParser):
self._toggle_attribute("quote") self._toggle_attribute("quote")
elif tag == "blockquote": elif tag == "blockquote":
self._toggle_attribute("quote") 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": elif tag == "font":
for key, value in attrs: for key, value in attrs:
if key == "color": if key == "color":