# -*- coding: utf-8 -*-

'''
Math extension for Python-Markdown
==================================

Adds support for displaying math formulas using [MathJax](http://www.mathjax.org/).

Author: 2015, Dmitry Shachnev <mitya57@gmail.com>.
'''

import markdown

class MathExtension(markdown.extensions.Extension):
    def __init__(self, *args, **kwargs):
        self.config = {
            'enable_dollar_delimiter': [False, 'Enable single-dollar delimiter'],
        }
        super(MathExtension, self).__init__(*args, **kwargs)

    def extendMarkdown(self, md, md_globals):
        def handle_match_inline(m):
            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

        configs = self.getConfigs()
        inlinemathpatterns = (
            markdown.inlinepatterns.Pattern(r'(?<!\\|\$)(\$)([^\$]+)(\$)'),  #  $...$
            markdown.inlinepatterns.Pattern(r'(?<!\\)(\\\()(.+?)(\\\))')     # \(...\)
        )
        mathpatterns = (
            markdown.inlinepatterns.Pattern(r'(?<!\\)(\$\$)([^\$]+)(\$\$)'), # $$...$$
            markdown.inlinepatterns.Pattern(r'(?<!\\)(\\\[)(.+?)(\\\])'),    # \[...\]
            markdown.inlinepatterns.Pattern(r'(?<!\\)(\\begin{([a-z]+?\*?)})(.+?)(\\end{\3})')
        )
        if not configs['enable_dollar_delimiter']:
            inlinemathpatterns = inlinemathpatterns[1:]
        for i, pattern in enumerate(inlinemathpatterns):
            pattern.handleMatch = handle_match_inline
            md.inlinePatterns.add('math-inline-%d' % i, pattern, '<escape')
        for i, pattern in enumerate(mathpatterns):
            pattern.handleMatch = handle_match
            md.inlinePatterns.add('math-%d' % i, pattern, '<escape')

def makeExtension(*args, **kwargs):
    return MathExtension(*args, **kwargs)

@@ -644,38 +644,35 @@
 .etree.Element('
-script
+div
 ')%0A            n
@@ -680,32 +680,33 @@
 de.set('
-type', 'math/tex
+class', 'equation
 ')%0A     
@@ -709,38 +709,48 @@
            node.
-text =
+set('data-expr',
  markdown.util.A
@@ -764,32 +764,105 @@
 ring(m.group(3))
+)%0A            node.set('displayMode', 'False')%0A            node.text = ''
 %0A            ret
@@ -952,14 +952,97 @@
 nt('
-script
+div')%0A            node.set('class', 'equation')%0A            node.set('displayMode', 'True
 ')%0A 
@@ -1148,38 +1148,48 @@
            node.
-text =
+set('data-expr',
  markdown.util.A
@@ -1237,16 +1237,17 @@
 roup(5))
+)
 %0A       
@@ -1278,22 +1278,32 @@
    node.
-text =
+set('data-expr',
  markdow
@@ -1333,16 +1333,44 @@
 roup(3))
+)%0A            node.text = ''
 %0A       

Change for KaTeX style