From 609c05ae2ad02751dd72823fc738c3751362a24c Mon Sep 17 00:00:00 2001
From: Andreas Kloeckner <inform@tiker.net>
Date: Sun, 24 Aug 2014 00:19:59 -0500
Subject: [PATCH] Make Mathjax and Markdown get along

---
 course/content.py     |  2 ++
 course/mdx_mathjax.py | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)
 create mode 100644 course/mdx_mathjax.py

diff --git a/course/content.py b/course/content.py
index ec237219..c8cdd493 100644
--- a/course/content.py
+++ b/course/content.py
@@ -330,10 +330,12 @@ def markup_to_html(course, repo, commit_sha, text):
         template = env.from_string(text)
         text = template.render()
 
+    from course.mdx_mathjax import MathJaxExtension
     import markdown
     return markdown.markdown(text,
         extensions=[
             LinkFixerExtension(course, commit_sha),
+            MathJaxExtension(),
             "extra",
             ],
         output_format="html5")
diff --git a/course/mdx_mathjax.py b/course/mdx_mathjax.py
new file mode 100644
index 00000000..89de7904
--- /dev/null
+++ b/course/mdx_mathjax.py
@@ -0,0 +1,32 @@
+# Downloaded from https://github.com/mayoff/python-markdown-mathjax/issues/3
+
+import markdown
+from markdown.postprocessors import Postprocessor
+
+
+class MathJaxPattern(markdown.inlinepatterns.Pattern):
+    def __init__(self):
+        markdown.inlinepatterns.Pattern.__init__(self, r'(?<!\\)(\$\$?)(.+?)\2')
+
+    def handleMatch(self, m):
+        node = markdown.util.etree.Element('mathjax')
+        node.text = markdown.util.AtomicString(m.group(2) + m.group(3) + m.group(2))
+        return node
+
+
+class MathJaxPostprocessor(Postprocessor):
+    def run(self, text):
+        text = text.replace('<mathjax>', '')
+        text = text.replace('</mathjax>', '')
+        return 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(), '<escape')
+        md.postprocessors['mathjax'] = MathJaxPostprocessor(md)
+
+
+def makeExtension(configs=None):
+    return MathJaxExtension(configs)
-- 
GitLab