Code Monkey home page Code Monkey logo

python-asciimathml's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.