# -*- 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>.
Source: https://github.com/mitya57/python-markdown-math,
commit db12837790a0e260c87da4edd9dc276361501897 plus some fixes.
'''

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

        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 self.getConfig('enable_dollar_delimiter'):
            inlinemathpatterns = inlinemathpatterns[1:]

        # version 3.0 moves version_info to __version_info__
        markdown_version = markdown.version_info[0]\
            if "version_info" in dir(markdown) \
            else markdown.__version_info__[0]

        if markdown_version == 2:
            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')
        else:
            for i, pattern in enumerate(inlinemathpatterns):
                pattern.handleMatch = handle_match_inline
                md.inlinePatterns.register(pattern, 'math-inline-%d' % i, 100)

            for i, pattern in enumerate(mathpatterns):
                pattern.handleMatch = handle_match
                md.inlinePatterns.register(pattern, 'math-%d' % i, 100)



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