├── .gitignore ├── README.md └── mdx_mathjax.py /.gitignore: -------------------------------------------------------------------------------- 1 | mdx_mathjax.pyc 2 | README.html 3 | __pycache__ 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # About `python-markdown-mathjax` 3 | 4 | This is a trivial [`python-markdown`](https://github.com/waylan/Python-Markdown) extension for embedding LaTeX math markup in Markdown so that [MathJax](http://www.mathjax.org/) can process it. 5 | 6 | I assume that you'll be using `$...$` and `$$...$$` to surround your LaTeX math markup. MathJax already recognizes `$$...$$` by default, but you need to tell it to recognize `$...$`: 7 | 8 | 13 | 14 | The `python-markdown` processor doesn't normally recognize `$...$` or `$$...$$`, so it tends to wreak havoc on your math markup by treating `*` and `_` as delimiters for italics and boldface and removing backslashes. This extension tells `python-markdown` not to look for markup inside `$...$` and `$$...$$`. 15 | 16 | ## Installing the extension 17 | 18 | You can install the extension in one of two ways: 19 | 20 | - You can put it right into your `python-markdown` installation. Run this command: 21 | 22 | python -c 'import markdown; print markdown.__path__[0]' 23 | 24 | That prints the path of the `markdown` module. It should have a subdirectory named `extensions`. Rename `mdx_mathjax.py` to `mathjax.py` and copy it to that `extensions` subdirectory. 25 | 26 | If `python-markdown` was installed as a zipped egg file, you won't find that `markdown/extensions` directory because it's inside the egg file. You'll have to delete the egg file (you can find the name in the output of the command above) and reinstall it unzipped: `easy_install -Z Markdown`. Then you should have a `markdown/extensions` directory in which to put `mathjax.py`. 27 | 28 | - You can put it in your `PYTHONPATH`. If you do this, don't rename the file. It needs to be named `mdx_mathjax.py`. 29 | 30 | ## Using the extension from the command-line 31 | 32 | After you've installed the file, you need to tell `python-markdown` to use it. If you're using the `markdown` command-line program, use `-x mathjax` on the command line, like this: 33 | 34 | markdown -x mathjax p-equals-np-proof.md > p-equals-np-proof.html 35 | 36 | By default, `markdown` won't report an error if it can't find the extension. It will just silently continue on, and your math markup will probably get munged. If you give `markdown` the `-v` flag, it will report the problem, but it will still continue on and won't exit with an error code. 37 | 38 | ## Using the extension from a Python program 39 | 40 | If you're using the `markdown` module from a Python program, you will need to import `mdx_mathjax` if you put the file `mdx_mathjax.py` in your `PYTHONPATH`. If you renamed it `mathjax.py` and put it in the `markdown/extensions` directory, you don't import it. You can use a `try`/`except` block to handle both cases: 41 | 42 | import markdown 43 | try: import mdx_mathjax 44 | except: pass 45 | mdProcessor = markdown.Markdown(extensions=['mathjax']) 46 | myHtmlFragment = mdProcessor.convert(r"Euler's identity, $e^{i\pi} = -1$, is widely considered the most beautiful theorem in mathematics.") 47 | 48 | If you're using the `markdown` module from a Python program, and it can't load the `mathjax` extension, it will raise a `markdown.MarkdownException` error. 49 | 50 | # No Copyright 51 | 52 | The author of this repository, Rob Mayoff, dedicates the contents of this repository to the public domain, in accordance with the [CC0 1.0 Universal Public Domain Dedication](https://creativecommons.org/publicdomain/zero/1.0/). 53 | reproduced in the file named COPYRIGHT in this repository. 54 | 55 | -------------------------------------------------------------------------------- /mdx_mathjax.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | import markdown 4 | import cgi 5 | 6 | class MathJaxPattern(markdown.inlinepatterns.Pattern): 7 | 8 | def __init__(self, md): 9 | markdown.inlinepatterns.Pattern.__init__(self, r'(?