forked from mitya57/python-markdown-math
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdx_math.py
92 lines (76 loc) · 3.84 KB
/
mdx_math.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-
'''
Math extension for Python-Markdown
==================================
Adds support for displaying math formulas using [MathJax](http://www.mathjax.org/).
Author: 2015, Dmitry Shachnev <[email protected]>.
'''
import markdown
class MathExtension(markdown.extensions.Extension):
def __init__(self, *args, **kwargs):
self.config = {
'enable_dollar_delimiter': [False, 'Enable single-dollar delimiter'],
'render_to_span': [False,
'Render to span elements rather than script for fallback'],
}
super(MathExtension, self).__init__(*args, **kwargs)
def extendMarkdown(self, md, md_globals):
def handle_match_inline(m):
# Create parent-container element
node_both = markdown.util.etree.Element('span')
# if render_to_span (fallback for non-js) is set - create an
# element for that
if self.getConfig('render_to_span'):
node_span = markdown.util.etree.Element('span')
node_span.set('class', 'MathJax_Preview')
node_span.text = ("\\\\(" + markdown.util.AtomicString(
m.group(3)) + "\\\\)")
node_both.append(node_span) # append to parent-container
# Create MathJax element
node_script = markdown.util.etree.Element('script')
node_script.set('type', 'math/tex')
node_script.text = markdown.util.AtomicString(m.group(3))
node_both.append(node_script) # append to parent-container
return node_both
def handle_match(m):
# Create parent-container element
node_both = markdown.util.etree.Element('span')
# if render_to_span (fallback for non-js) is set - create an
# element for that
if self.getConfig('render_to_span'):
node_div = markdown.util.etree.Element('div')
node_div.set('class', 'MathJax_Preview')
if '\\begin' in m.group(2):
node_div.text = markdown.util.AtomicString(
m.group(2) + m.group(4) + m.group(5))
else:
node_div.text = markdown.util.AtomicString(m.group(3))
node_both.append(node_div) # Append span to parent-container
node_script = markdown.util.etree.Element('script')
node_script.set('type', 'math/tex; mode=display')
if '\\begin' in m.group(2):
node_script.text = markdown.util.AtomicString(
m.group(2) + m.group(4) + m.group(5))
else:
node_script.text = markdown.util.AtomicString(m.group(3))
node_both.append(node_script) # Append script to parent-container
return node_both
inlinemathpatterns = (
markdown.inlinepatterns.Pattern(r'(?<!\\|\$)(\$)([^\$]+)(\$)'), # $...$
markdown.inlinepatterns.Pattern(r'(?<!\\)(\\\()(.+?)(\\\))') # \(...\)
)
mathpatterns = (
markdown.inlinepatterns.Pattern(r'(?<!\\)(\$\$)([^\$]+)(\$\$)'), # $$...$$
markdown.inlinepatterns.Pattern(r'(?<!\\)(\\\[)(.+?)(\\\])'), # \[...\]
markdown.inlinepatterns.Pattern(r'(?<!\\)(\\begin{([a-z]+?\*?)})(.+?)(\\end{\3})')
)
if not self.getConfig('enable_dollar_delimiter'):
inlinemathpatterns = inlinemathpatterns[1:]
for i, pattern in enumerate(inlinemathpatterns):
pattern.handleMatch = handle_match_inline
md.inlinePatterns.add('math-inline-%d' % i, pattern, '<escape')
for i, pattern in enumerate(mathpatterns):
pattern.handleMatch = handle_match
md.inlinePatterns.add('math-%d' % i, pattern, '<escape')
def makeExtension(*args, **kwargs):
return MathExtension(*args, **kwargs)