commit
13bfb33b73
2 changed files with 24 additions and 16 deletions
|
@ -328,14 +328,13 @@ class MatrixHtmlParser(HTMLParser):
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return HTMLParser.unescape(self, text)
|
return HTMLParser.unescape(self, text)
|
||||||
|
|
||||||
def feed(self, text):
|
def add_substring(self, text, attrs):
|
||||||
text = self.unescape(text)
|
fmt_string = FormattedString(text, attrs)
|
||||||
return HTMLParser.feed(self, text)
|
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]
|
||||||
|
|
||||||
|
@ -352,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:
|
||||||
|
@ -367,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:
|
||||||
|
@ -387,20 +383,24 @@ 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:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def handle_data(self, data):
|
def handle_data(self, data):
|
||||||
self.text = self.text + data
|
self.text += data
|
||||||
|
|
||||||
|
def handle_entityref(self, name):
|
||||||
|
self.text += self.unescape("&{};".format(name))
|
||||||
|
|
||||||
|
def handle_charref(self, name):
|
||||||
|
self.text += self.unescape("&{};".format(name))
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
|
|
@ -31,3 +31,11 @@ def test_html_numeric_reference_parsing(entitydef):
|
||||||
num = entitydef[2]
|
num = entitydef[2]
|
||||||
parser = MatrixHtmlParser()
|
parser = MatrixHtmlParser()
|
||||||
assert parser.unescape('&#{};'.format(num)) == character
|
assert parser.unescape('&#{};'.format(num)) == character
|
||||||
|
|
||||||
|
|
||||||
|
def test_parsing_of_escaped_brackets():
|
||||||
|
p = MatrixHtmlParser()
|
||||||
|
p.feed('<pre><code><faketag></code></pre>')
|
||||||
|
s = p.get_substrings()
|
||||||
|
print(s)
|
||||||
|
assert s[0].text == '<faketag>' and len(s) == 1
|
||||||
|
|
Loading…
Reference in a new issue