from django.template import Library
from django.utils.http import urlquote

from judge.math_parser import MathHTMLParser, MATHTEX_CGI

register = Library()


class MathJaxTexFallbackMath(MathHTMLParser):
    def inline_math(self, math):
        return ('<span class="inline-math">'
                    r'<img class="tex-image" src="%s?\textstyle %s" alt="%s"/>'
                    r'<span class="tex-text" style="display:none">\(%s\)</span>'
                '</span>') % (MATHTEX_CGI, urlquote(math), math, math)

    def display_math(self, math):
        return ('<span class="display-math">'
                   r'<img class="tex-image" src="%s?\displaystyle %s" alt="%s"/>'
                   r'<span class="tex-text" style="display:none">\[%s\]</span>'
                '</span>') % (MATHTEX_CGI, urlquote(math), math, math)


class MathJaxTexOnlyMath(MathHTMLParser):
    def inline_math(self, math):
        return '\(%s\)' % math

    def display_math(self, math):
        return '\[%s\]' % math


@register.filter(name='smart_math', is_safe=True)
def math(page, style='fallback'):
    if style == 'tex':
        return MathJaxTexOnlyMath.convert(page)
    else:
        return MathJaxTexFallbackMath.convert(page)

@@ -421,22 +421,20 @@
 y:none%22%3E
-%5C(%25s%5C)
+~%25s~
 %3C/span%3E'
@@ -733,22 +733,22 @@
 y:none%22%3E
-%5C%5B%25s%5C%5D
+$$%25s$$
 %3C/span%3E'
@@ -916,14 +916,12 @@
 rn '
-%5C(%25s%5C)
+~%25s~
 ' %25 
@@ -980,14 +980,14 @@
 rn '
-%5C%5B%25s%5C%5D
+$$%25s$$
 ' %25 

Use "new" math delimiters