MatrixHtmlParser: Factor out adding substrings into a method.
This commit is contained in:
parent
c6e34dd0b1
commit
392698b9b5
1 changed files with 10 additions and 12 deletions
|
@ -328,10 +328,13 @@ class MatrixHtmlParser(HTMLParser):
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return HTMLParser.unescape(self, text)
|
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):
|
def _toggle_attribute(self, attribute):
|
||||||
if self.text:
|
if self.text:
|
||||||
self.substrings.append(
|
self.add_substring(self.text, self.attributes.copy())
|
||||||
FormattedString(self.text, self.attributes.copy()))
|
|
||||||
self.text = ""
|
self.text = ""
|
||||||
self.attributes[attribute] = not self.attributes[attribute]
|
self.attributes[attribute] = not self.attributes[attribute]
|
||||||
|
|
||||||
|
@ -348,11 +351,9 @@ class MatrixHtmlParser(HTMLParser):
|
||||||
self._toggle_attribute("quote")
|
self._toggle_attribute("quote")
|
||||||
elif tag == "br":
|
elif tag == "br":
|
||||||
if self.text:
|
if self.text:
|
||||||
self.substrings.append(
|
self.add_substring(self.text, self.attributes.copy())
|
||||||
FormattedString(self.text, self.attributes.copy()))
|
|
||||||
self.text = "\n"
|
self.text = "\n"
|
||||||
self.substrings.append(
|
self.add_substring(self.text, DEFAULT_ATRIBUTES.copy())
|
||||||
FormattedString(self.text, DEFAULT_ATRIBUTES.copy()))
|
|
||||||
self.text = ""
|
self.text = ""
|
||||||
elif tag == "font":
|
elif tag == "font":
|
||||||
for key, value in attrs:
|
for key, value in attrs:
|
||||||
|
@ -363,8 +364,7 @@ class MatrixHtmlParser(HTMLParser):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if self.text:
|
if self.text:
|
||||||
self.substrings.append(
|
self.add_substring(self.text, self.attributes.copy())
|
||||||
FormattedString(self.text, self.attributes.copy()))
|
|
||||||
self.text = ""
|
self.text = ""
|
||||||
self.attributes["fgcolor"] = color
|
self.attributes["fgcolor"] = color
|
||||||
else:
|
else:
|
||||||
|
@ -383,8 +383,7 @@ class MatrixHtmlParser(HTMLParser):
|
||||||
self._toggle_attribute("quote")
|
self._toggle_attribute("quote")
|
||||||
elif tag == "font":
|
elif tag == "font":
|
||||||
if self.text:
|
if self.text:
|
||||||
self.substrings.append(
|
self.add_substring(self.text, self.attributes.copy())
|
||||||
FormattedString(self.text, self.attributes.copy()))
|
|
||||||
self.text = ""
|
self.text = ""
|
||||||
self.attributes["fgcolor"] = None
|
self.attributes["fgcolor"] = None
|
||||||
else:
|
else:
|
||||||
|
@ -401,8 +400,7 @@ class MatrixHtmlParser(HTMLParser):
|
||||||
|
|
||||||
def get_substrings(self):
|
def get_substrings(self):
|
||||||
if self.text:
|
if self.text:
|
||||||
self.substrings.append(
|
self.add_substring(self.text, self.attributes.copy())
|
||||||
FormattedString(self.text, self.attributes.copy()))
|
|
||||||
|
|
||||||
return self.substrings
|
return self.substrings
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue