Skip to content

Commit

Permalink
Add parse XML from file for MathMLParser
Browse files Browse the repository at this point in the history
  • Loading branch information
belerico committed Apr 18, 2020
1 parent 1d39e0a commit d12244a
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions py_asciimath/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ def parse(
xml,
dtd=None,
dtd_validation=True,
from_file=False,
network=False,
ns_clean=True,
resolve_entities=False,
Expand All @@ -260,6 +261,9 @@ def parse(
`mathml1`, `mathml2` or `mathml3`
dtd_validation (bool, optional): Validate XML against DTD during
parsing. Defaults to True.
from_file (bool, optional): If True, load the XML file from the
path specified by `xml`.
Defaults to False
network (bool, optional): Validate against remote DTD.
Defaults to False.
ns_clean (bool, optional): Clean up redundant namespace
Expand All @@ -272,13 +276,14 @@ def parse(
lxml.etree._Element: Root of the parsed and possibly
validated MathML XML
"""
encoding = MathMLParser.get_encoding(xml)
if encoding is None:
logging.warning("The XML encoding is None: default to UTF-8")
encoding = "UTF-8"
if dtd_validation:
xml = MathMLParser.set_doctype(xml, network, dtd=dtd)
xml = xml.encode(encoding)
if not from_file:
encoding = MathMLParser.get_encoding(xml)
if encoding is None:
logging.warning("The XML encoding is None: default to UTF-8")
encoding = "UTF-8"
if dtd_validation:
xml = MathMLParser.set_doctype(xml, network, dtd=dtd)
xml = xml.encode(encoding)
if dtd_validation:
logging.info("Loading dtd and validating...")
mathml_parser = MathMLParser.get_parser(
Expand All @@ -288,4 +293,7 @@ def parse(
resolve_entities=resolve_entities,
**kwargs
)
return lxml.etree.fromstring(xml, mathml_parser)
if from_file:
return lxml.etree.parse(xml, mathml_parser)
else:
return lxml.etree.fromstring(xml, mathml_parser)

0 comments on commit d12244a

Please sign in to comment.