Implement MatrixHtmlParser.unescape shim.
This is for Python 2/3 compatibility, since Python 3 deprecates the instance method and Python 2 doesn't have html.unescape.
This commit is contained in:
parent
0a868b80bb
commit
4be54d032e
1 changed files with 12 additions and 0 deletions
|
@ -32,6 +32,7 @@ try:
|
|||
except ImportError:
|
||||
from html.parser import HTMLParser
|
||||
|
||||
import html
|
||||
from html.entities import name2codepoint
|
||||
|
||||
FormattedString = namedtuple('FormattedString', ['text', 'attributes'])
|
||||
|
@ -316,6 +317,17 @@ class MatrixHtmlParser(HTMLParser):
|
|||
self.substrings = [] # type: List[FormattedString]
|
||||
self.attributes = DEFAULT_ATRIBUTES.copy()
|
||||
|
||||
def unescape(self, text):
|
||||
"""Shim to unescape HTML in both Python 2 and 3.
|
||||
|
||||
The instance method was deprecated in Python 3 and html.unescape
|
||||
doesn't exist in Python 2 so this is needed.
|
||||
"""
|
||||
try:
|
||||
return html.unescape(text)
|
||||
except AttributeError:
|
||||
return HTMLParser.unescape(self, text)
|
||||
|
||||
def feed(self, text):
|
||||
text = self.unescape(text)
|
||||
return HTMLParser.feed(self, text)
|
||||
|
|
Loading…
Add table
Reference in a new issue