# # Copyright (c) 2017 Intel Corporation # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # -*- coding: utf-8 -*- ''' Math extension for Python-Markdown ================================== Adds support for displaying math formulas using [MathJax](http://www.mathjax.org/). Author: 2015, Dmitry Shachnev . ''' import markdown class MathExtension(markdown.extensions.Extension): def __init__(self, *args, **kwargs): self.config = { 'enable_dollar_delimiter': [False, 'Enable single-dollar delimiter'], 'render_to_span': [False, 'Render to span elements rather than script for fallback'], } super(MathExtension, self).__init__(*args, **kwargs) def extendMarkdown(self, md, md_globals): def handle_match_inline(m): if self.getConfig('render_to_span'): node = markdown.util.etree.Element('span') node.set('class', 'tex') node.text = ("\\\\(" + markdown.util.AtomicString(m.group(3)) + "\\\\)") else: node = markdown.util.etree.Element('script') node.set('type', 'math/tex') node.text = markdown.util.AtomicString(m.group(3)) return node def handle_match(m): node = markdown.util.etree.Element('script') node.set('type', 'math/tex; mode=display') if '\\begin' in m.group(2): node.text = markdown.util.AtomicString(m.group(2) + m.group(4) + m.group(5)) else: node.text = markdown.util.AtomicString(m.group(3)) return node inlinemathpatterns = ( markdown.inlinepatterns.Pattern(r'(?