Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP html_utils: improved MathML support #131

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 38 additions & 18 deletions harvestingkit/html_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,45 +42,65 @@ class MathMLParser(HTMLParser):

"""Special HTML stripper that allows MathML."""

mathml_elements = set([
'annotation', 'annotation-xml', 'maction', 'math',
'merror', 'mfenced', 'mfrac', 'mi', 'mmultiscripts',
'mn', 'mo', 'mover', 'mpadded',
'mphantom', 'mprescripts', 'mroot', 'mrow', 'mspace', 'msqrt',
'mstyle', 'msub', 'msubsup', 'msup', 'mtable', 'mtd', 'mtext',
'mtr', 'munder', 'munderover', 'none', 'semantics'
])

def __init__(self):
mathml_elements = set(['msline', 'mlongdiv', 'mstyle', 'mlabeledtr',
'mover', 'mglyph', 'msrow', 'mscarries', 'msgroup',
'mrow', 'annotation-xml', 'mphantom',
'mmultiscripts', 'msqrt', 'msub', 'mpadded',
'mtable', 'munder', 'math', 'msubsup', 'mfenced',
'mspace', 'mroot', 'maligngroup', 'msup', 'mfrac',
'munderover', 'mstack', 'annotation', 'semantics',
'none', 'mprescripts', 'mtr', 'mo', 'mn', 'mi',
'malignmark', 'mtd', 'ms', 'maction', 'merror',
'menclose', 'mscarry', 'mtext'])

def __init__(self, escape_html=False):
"""Set initial values."""
HTMLParser.__init__(self)
self.reset()
self.fed = []
self.escape_html = escape_html

def handle_data(self, d):
"""Return representation of pure text data."""
self.fed.append(d)
if self.escape_html:
self.fed.append(escape_for_xml(d))
else:
self.fed.append(d)

def handle_starttag(self, tag, attrs):
"""Return representation of html start tag and attributes."""
final_attr = ""
for key, value in attrs:
final_attr += ' {0}="{1}"'.format(key, value)
final_tag = "<{0}{1}>".format(tag, final_attr)
if tag in self.mathml_elements:
final_attr = ""
for key, value in attrs:
final_attr += ' {0}="{1}"'.format(key, value)
self.fed.append("<{0}{1}>".format(tag, final_attr))
self.fed.append(final_tag)
elif self.escape_html:
self.fed.append(escape_for_xml(final_tag))

def handle_endtag(self, tag):
"""Return representation of html end tag."""
final_tag = "</{0}>".format(tag)
if tag in self.mathml_elements:
self.fed.append("</{0}>".format(tag))
self.fed.append(final_tag)
elif self.escape_html:
self.fed.append(escape_for_xml(final_tag))

def handle_entityref(self, name):
"""Return representation of entities."""
self.fed.append('&%s;' % name)
final_entity = '&%s;' % name
if self.escape_html:
self.fed.append(escape_for_xml(final_entity))
else:
self.fed.append(final_entity)

def handle_charref(self, name):
"""Return representation of numeric entities."""
self.fed.append('&#%s;' % name)
final_charref = '&#%s;' % name
if self.escape_html:
self.fed.append(escape_for_xml(final_charref))
else:
self.fed.append(final_charref)

def get_data(self):
"""Return all the stripped data."""
Expand Down