Skip to content

ShadowKyogre/python-asciimathml

 
 

Repository files navigation

python-asciimathml

Build Status

This small python module exports a single function, parse(), which parses a string containing ASCIIMathML and returns the corresponding MathML tree as an xml.etree.ElementTree instance.

Also included is a python-markdown extension that translates text enclosed between $$ into MathML.

Example usage

The function parse() generates a tree of elements:

>>> import asciimathml
>>> asciimathml.parse('sqrt 2')
<Element math at b76fb28c>

The tree can then be manipulated using the standard python library. For example we can generate its string representation:

>>> from xml.etree.ElementTree import tostring
>>> tostring(asciimathml.parse('sqrt 2'))
'<math><mstyle><msqrt><mn>2</mn></msqrt></mstyle></math>'

Or, if you want to add the attributes title and xmlns to the root node:

>>> tree = asciimathml.parse('sqrt 2')
>>> tree.set('title', 'sqrt2')
>>> tree.set('xmlns', 'http://www.w3.org/1998/Math/MathML')
>>> tostring(tree)
'<math title="sqrt2" xmlns="http://www.w3.org/1998/Math/MathML"><mstyle><msqrt><mn>2</mn></msqrt></mstyle></math>'

As you can see MathML is very verbose and is not intended to be written by hand.

And this is an example of ASCIIMathML embedded in markdown:

>>> import markdown
>>> markdown.markdown('$$ sqrt 2 $$', ['asciimathml'])
u'<p>\n<math xmlns="http://www.w3.org/1998/Math/MathML"><mstyle><msqrt><mn>2</mn></msqrt></mstyle></math>\n</p>'

Dependencies and installation

Tested with python 2.7 and python 3.4. The core module, asciimathml has no dependencies. The markdown extension mdx_asciimathml requires at least markdown 2.0.

A standard setup.py is provided (as usual it's enough to execute python setup.py install to perform the installation). You can also manually copy the two files asciimathml.py and (optionally) mdx_asciimathml.py somewhere in your PYTHONPATH.

Browser support

The current status of support for MathML by web browsers is disheartening.

Only Firefox and Safari, among the major browsers, can properly render MathML (webkit support is on the way, you can follow progress made closing the tickets of this master bug).

Also note that MathML is displayed correctly only when embedded in a XHTML or in HTML5 document.

About

python ASCIIMathML to Presentation MathML translator

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%