-
Notifications
You must be signed in to change notification settings - Fork 74
/
mdx_mathjax.py
43 lines (28 loc) · 1.39 KB
/
mdx_mathjax.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import markdown
def _mathjax_handleMatch(obj, m):
node = markdown.util.etree.Element('mathjax')
node.text = markdown.util.AtomicString(m.group(2) + m.group(3) + m.group(4))
return node
class MathJaxPattern(markdown.inlinepatterns.Pattern):
def __init__(self):
markdown.inlinepatterns.Pattern.__init__(self, r'(?<!\\)(\$\$?)(.+?)(\2)')
def handleMatch(self, m):
return _mathjax_handleMatch(self, m)
class MathJaxNativeInlinePattern(markdown.inlinepatterns.Pattern):
def __init__(self):
markdown.inlinepatterns.Pattern.__init__(self, r'(\\\()(.+?)(\\\))')
def handleMatch(self, m):
return _mathjax_handleMatch(self, m)
class MathJaxNativeDisplayPattern(markdown.inlinepatterns.Pattern):
def __init__(self):
markdown.inlinepatterns.Pattern.__init__(self, r'(\\\[)(.+?)(\\\])')
def handleMatch(self, m):
return _mathjax_handleMatch(self, m)
class MathJaxExtension(markdown.Extension):
def extendMarkdown(self, md, md_globals):
# Needs to come before escape matching because \ is pretty important in LaTeX
md.inlinePatterns.add('mathjax1', MathJaxPattern(), '<escape')
md.inlinePatterns.add('mathjax2', MathJaxNativeInlinePattern(), '<escape')
md.inlinePatterns.add('mathjax3', MathJaxNativeDisplayPattern(), '<escape')
def makeExtension(configs=None):
return MathJaxExtension(configs)