Extend markdown2 for use with mathjax
markdown2Mathjax (https://github.com/constantAmateur/markdown2Mathjax) is a simple package that allows you to use latex style math generated by MathJax (http://mathjax.org) in conjunction with the popular markdown format. There are two implementations of markdown in python, this package is designed to be used with the more widely used and actively maintained of the two, called markdown2 (https://github.com/trentm/python-markdown2). Typical usage is as follows:
#!/usr/bin/env python from markdown2Mathjax import sanitizeInput, reconstructMath from markdown2 import markdown tmp = sanitizeInput(sometxt) markdedDownText = markdown(tmp) finalOutput = reconstructMath(markedDownText,tmp)
(where “sometxt” is a string containing the markdown+mathjax text.)
markdown2Mathjax is hosted on pypi, so can be installed either from source, using:
tar zxvf markdown2Mathjax-x.x.x.tar.gz cd markdown2Mathjax-x.x.x python setup.py install
or using pip (or easy_install if you’d prefer):
pip install markdown2Mathjax
By default, math is marked by single $ signs for inline math and double $$ signs for equations. However, any delimiting strings may be used by changing the keyword arguments to the sanitizeInput and reconstructMath functions. Note that by default mathjax does NOT use single dollars signs to delimit inline math, so if you use the defaults for this package, you will have to instruct mathjax to treat it appropriately.
The sanitizeInput function finds any instances of math (as determined by the supplied delimiters) and replaces them with a placeholder. This placeholder can be specified by the user, but it is recommended you use the supplied default of “$0$”. The placeholder has to be something that won’t be processed by markdown, and won’t ever be PRODUCED by markdown. The first of these requirements is easy enough to meet, the second can be somewhat more subtle. If the placeholder already exists in the supplied user text, it is noted by sanitizeInput and will be left alone when reconstruction math after markdown processing.
Each time a valid math block is encountered and replaced by a placeholder, the math is saved in a list (called codeblocks) which is then returned by the sanitizeInput function. Each entry in codeblocks is prefixed with either 0 (for a literal occurance of the placeholder in the text), a 1 for inline math or a 2 for a separate equation.
Another way to think about this is than reconstructMath is basically the inverse of sanitizeInput. So without using markdown in between, nothing should change between the two (except when reconstructMath also does some extra cleanup of the math codeblocks). That is:
should always evaluate to true.