MatrixHtmlParser: Factor out adding substrings into a method.

This commit is contained in:
Denis Kasak 2018-03-22 20:47:10 +01:00
parent c6e34dd0b1
commit 392698b9b5

View file

@ -328,10 +328,13 @@ class MatrixHtmlParser(HTMLParser):
except AttributeError:
return HTMLParser.unescape(self, text)
def add_substring(self, text, attrs):
fmt_string = FormattedString(text, attrs)
self.substrings.append(fmt_string)
def _toggle_attribute(self, attribute):
if self.text:
self.substrings.append(
FormattedString(self.text, self.attributes.copy()))
self.add_substring(self.text, self.attributes.copy())
self.text = ""
self.attributes[attribute] = not self.attributes[attribute]
@ -348,11 +351,9 @@ class MatrixHtmlParser(HTMLParser):
self._toggle_attribute("quote")
elif tag == "br":
if self.text:
self.substrings.append(
FormattedString(self.text, self.attributes.copy()))
self.add_substring(self.text, self.attributes.copy())
self.text = "\n"
self.substrings.append(
FormattedString(self.text, DEFAULT_ATRIBUTES.copy()))
self.add_substring(self.text, DEFAULT_ATRIBUTES.copy())
self.text = ""
elif tag == "font":
for key, value in attrs:
@ -363,8 +364,7 @@ class MatrixHtmlParser(HTMLParser):
continue
if self.text:
self.substrings.append(
FormattedString(self.text, self.attributes.copy()))
self.add_substring(self.text, self.attributes.copy())
self.text = ""
self.attributes["fgcolor"] = color
else:
@ -383,8 +383,7 @@ class MatrixHtmlParser(HTMLParser):
self._toggle_attribute("quote")
elif tag == "font":
if self.text:
self.substrings.append(
FormattedString(self.text, self.attributes.copy()))
self.add_substring(self.text, self.attributes.copy())
self.text = ""
self.attributes["fgcolor"] = None
else:
@ -401,8 +400,7 @@ class MatrixHtmlParser(HTMLParser):
def get_substrings(self):
if self.text:
self.substrings.append(
FormattedString(self.text, self.attributes.copy()))
self.add_substring(self.text, self.attributes.copy())
return self.substrings