# Source: https://github.com/mayoff/python-markdown-mathjaximportmarkdowntry:# Markdown 2.1.0 changed from 2.0.3. We try importing the new version first,# but import the 2.0.3 version if it failsfrommarkdown.utilimportetree,AtomicStringexcept:frommarkdownimportetree,AtomicStringclassMathJaxPattern(markdown.inlinepatterns.Pattern):def__init__(self):markdown.inlinepatterns.Pattern.__init__(self,r'(?<!\\)(\$\$?)(.+?)\2')defhandleMatch(self,m):el=etree.Element('span')el.text=AtomicString(m.group(2)+m.group(3)+m.group(2))returnelclassMathJaxExtension(markdown.Extension):defextendMarkdown(self,md,md_globals):# Needs to come before escape matching because \ is pretty important in LaTeXmd.inlinePatterns.add('mathjax',MathJaxPattern(),'<escape')defmakeExtension(configs=None):returnMathJaxExtension(configs)