py_asciimath.parser.parser

class py_asciimath.parser.parser.MathMLParser(*args, **kwargs)

Bases: object

static get_doctype(dtd, network)

Get a MathML DOCTYPE declaration

Parameters:
  • dtd (str) – MathML DTD type. Must be on of the following: mathml1, mathml2 or mathml3
  • network (bool) – If True, get a public DTD; otherwise a local DTD declaration will be set
Raises:

NotImplementedError – Other DTD declarations but MathML will be discarded

Returns:

DOCTYPE declaration following the specifics

Return type:

str

classmethod get_doctype_version(xml)

Get the MathML DTD version from DOCTYPE declaration

Parameters:xml (str) – XML document
Raises:Exception – If multiple DOCTYPE declarations are found
Returns:MathML DTD version (1,2,3); None if DOCTYPE is not found
Return type:str
classmethod get_encoding(s)

Get the encoding from the XML declaration

The XML declaration is supposed to match the following pattern: (<\?xml.*?(encoding=(?:’|")(.*?)(?:’|"))?\?>). If the XML declaration is not found, then None will be returned

Parameters:s (str) – XML string
Raises:Exception – If the XML declaration is not at the beginning of the string
Returns:Encoding of the XML document
Return type:str
static get_parser(dtd_validation=True, network=False, ns_clean=True, resolve_entities=False, **kwargs)

Create a MathML XML parser

Parameters:
  • dtd_validation (bool, optional) – Validate XML against DTD during parsing. Defaults to True.
  • network (bool, optional) – Validate against remote DTD. Defaults to False.
  • ns_clean (bool, optional) – Clean up redundant namespace declarations. Defaults to True.
  • resolve_entities (bool, optional) – replace entities by their text value. Defaults to False.
  • **kwargs – Additional ~lxml.extree.XMLParser options
Returns:

MathML parser following the specifications

Return type:

lxml.etree.XMLParser

static parse(xml, dtd=None, dtd_validation=True, from_file=False, network=False, ns_clean=True, resolve_entities=False, **kwargs)

Parse a MathML XML

Parameters:
  • xml (str) – String representing a MathML XML.
  • dtd (str) – MathML DTD type. Must be on of the following: 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 declarations. Defaults to True.
  • resolve_entities (bool, optional) – replace entities by their text value. Defaults to False.
  • **kwargs – Additional ~lxml.extree.XMLParser options
Returns:

Root of the parsed and possibly

validated MathML XML

Return type:

lxml.etree._Element

classmethod set_doctype(s, network, dtd=None)

Set MathML DOCTYPE of the XML document

The DOCTYPE field is supposed to match the following pattern: (<!DOCTYPE math ([A-Z]+).*?mathml(\d)?.dtd">). If the MathML DOCTYPE is not found, then the MathML3 DTD will be returned

Parameters:
  • s (str) – XML string
  • network (bool) – If True, return the PUBLIC MathML DTD; otherwise use the local MathML DTD
  • dtd (str) – Version of the default MathML DTD, If the DTD is not found. If None will be used MathML3 DTD Defaults to None
Raises:

Exception – If the XML declaration is not at the beginning of the string

Returns:

s replaced with the DTD following the specifications

and the MathML version

Return type:

str, str