Skip to main content

Extend markdown2 for use with mathjax

Project description

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[0])
finalOutput = reconstructMath(markedDownText,tmp[1])

(where “sometxt” is a string containing the markdown+mathjax text.)

Installation

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

Usage

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.

Explanation

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.

The sanitized string from sanitizeInput can then be marked up by markdown without fear of your math being mangled. Finally, reconstructMath replaces each of the placeholders in the marked up text with the appropriate codeblock. This final html can then be displayed with appropriate mathjax javascript headers.

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:

tmp=sanitizeInput(test)
reconstructMath(tmp[0],tmp[1])==test

should always evaluate to true.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

markdown2Mathjax-0.3.8.tar.gz (18.8 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page