-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmdx_mathjax.py
More file actions
25 lines (16 loc) · 832 Bytes
/
mdx_mathjax.py
File metadata and controls
25 lines (16 loc) · 832 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import markdown
import cgi
class MathJaxPattern(markdown.inlinepatterns.Pattern):
def __init__(self, md):
markdown.inlinepatterns.Pattern.__init__(self, r'(?<!\\)(\$\$?)(.+?)\2', md)
def handleMatch(self, m):
# Pass the math code through, unmodified except for basic entity substitutions.
# Stored in htmlStash so it doesn't get further processed by Markdown.
text = cgi.escape(m.group(2) + m.group(3) + m.group(2))
return self.markdown.htmlStash.store(text)
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('mathjax', MathJaxPattern(md), '<escape')
def makeExtension(configs=[]):
return MathJaxExtension(configs)