|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +''' |
| 4 | +Math extension for Python-Markdown |
| 5 | +================================== |
| 6 | +
|
| 7 | +Adds support for displaying math formulas using |
| 8 | +[MathJax](http://www.mathjax.org/). |
| 9 | +
|
| 10 | +Author: 2015-2017, Dmitry Shachnev <[email protected]>. |
| 11 | +''' |
| 12 | + |
| 13 | +""" |
| 14 | +Copyright 2015-2017 Dmitry Shachnev <[email protected]>. |
| 15 | +All rights reserved. |
| 16 | +
|
| 17 | +Redistribution and use in source and binary forms, with or without |
| 18 | +modification, are permitted provided that the following conditions |
| 19 | +are met: |
| 20 | +
|
| 21 | +1. Redistributions of source code must retain the above copyright |
| 22 | + notice, this list of conditions and the following disclaimer. |
| 23 | +2. Redistributions in binary form must reproduce the above copyright |
| 24 | + notice, this list of conditions and the following disclaimer in the |
| 25 | + documentation and/or other materials provided with the distribution. |
| 26 | +3. Neither the name of the author nor the names of its contributors |
| 27 | + may be used to endorse or promote products derived from this software |
| 28 | + without specific prior written permission. |
| 29 | +
|
| 30 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 31 | +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 32 | +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 33 | +ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 34 | +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 35 | +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 36 | +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 37 | +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 38 | +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 39 | +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 40 | +SUCH DAMAGE. |
| 41 | +""" |
| 42 | + |
| 43 | +from markdown.inlinepatterns import Pattern |
| 44 | +from markdown.extensions import Extension |
| 45 | +from markdown.util import AtomicString, etree |
| 46 | + |
| 47 | + |
| 48 | +class MathExtension(Extension): |
| 49 | + def __init__(self, *args, **kwargs): |
| 50 | + self.config = { |
| 51 | + 'enable_dollar_delimiter': |
| 52 | + [False, 'Enable single-dollar delimiter'], |
| 53 | + 'add_preview': [False, 'Add a preview node before each math node'], |
| 54 | + } |
| 55 | + super(MathExtension, self).__init__(*args, **kwargs) |
| 56 | + |
| 57 | + def extendMarkdown(self, md, md_globals): |
| 58 | + def _wrap_node(node, preview_text, wrapper_tag): |
| 59 | + if not self.getConfig('add_preview'): |
| 60 | + return node |
| 61 | + preview = etree.Element('span', {'class': 'MathJax_Preview'}) |
| 62 | + preview.text = AtomicString(preview_text) |
| 63 | + wrapper = etree.Element(wrapper_tag) |
| 64 | + wrapper.extend([preview, node]) |
| 65 | + return wrapper |
| 66 | + |
| 67 | + def handle_match_inline(m): |
| 68 | + node = etree.Element('script') |
| 69 | + node.set('type', 'math/tex') |
| 70 | + node.text = AtomicString(m.group(3)) |
| 71 | + return _wrap_node(node, ''.join(m.group(2, 3, 4)), 'span') |
| 72 | + |
| 73 | + def handle_match(m): |
| 74 | + node = etree.Element('script') |
| 75 | + node.set('type', 'math/tex; mode=display') |
| 76 | + if '\\begin' in m.group(2): |
| 77 | + node.text = AtomicString(''.join(m.group(2, 4, 5))) |
| 78 | + return _wrap_node(node, ''.join(m.group(1, 2, 4, 5, 6)), 'div') |
| 79 | + else: |
| 80 | + node.text = AtomicString(m.group(3)) |
| 81 | + return _wrap_node(node, ''.join(m.group(2, 3, 4)), 'div') |
| 82 | + |
| 83 | + inlinemathpatterns = ( |
| 84 | + Pattern(r'(?<!\\|\$)(\$)([^\$]+)(\$)'), # $...$ |
| 85 | + Pattern(r'(?<!\\)(\\\()(.+?)(\\\))') # \(...\) |
| 86 | + ) |
| 87 | + mathpatterns = ( |
| 88 | + Pattern(r'(?<!\\)(\$\$)([^\$]+)(\$\$)'), # $$...$$ |
| 89 | + Pattern(r'(?<!\\)(\\\[)(.+?)(\\\])'), # \[...\] |
| 90 | + Pattern(r'(?<!\\)(\\begin{([a-z]+?\*?)})(.+?)(\\end{\3})') |
| 91 | + ) |
| 92 | + if not self.getConfig('enable_dollar_delimiter'): |
| 93 | + inlinemathpatterns = inlinemathpatterns[1:] |
| 94 | + for i, pattern in enumerate(inlinemathpatterns): |
| 95 | + pattern.handleMatch = handle_match_inline |
| 96 | + md.inlinePatterns.add('math-inline-%d' % i, pattern, '<escape') |
| 97 | + for i, pattern in enumerate(mathpatterns): |
| 98 | + pattern.handleMatch = handle_match |
| 99 | + md.inlinePatterns.add('math-%d' % i, pattern, '<escape') |
| 100 | + |
| 101 | + |
| 102 | +def makeExtension(*args, **kwargs): |
| 103 | + return MathExtension(*args, **kwargs) |
0 commit comments