-
-
Notifications
You must be signed in to change notification settings - Fork 246
/
arithmatex.py
409 lines (308 loc) · 14.4 KB
/
arithmatex.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
r"""
Arithmatex.
pymdownx.arithmatex
Extension that preserves the following for MathJax use:
```
$Equation$, \(Equation\)
$$
Display Equations
$$
\[
Display Equations
\]
\begin{align}
Display Equations
\end{align}
```
and `$Inline MathJax Equations$`
Inline and display equations are converted to scripts tags. You can optionally generate previews.
MIT license.
Copyright (c) 2014 - 2017 Isaac Muse <isaacmuse@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from markdown import Extension
from markdown.inlinepatterns import InlineProcessor
from markdown.blockprocessors import BlockProcessor
from markdown import util as md_util
from functools import partial
import xml.etree.ElementTree as etree
from . import util
import re
RE_SMART_DOLLAR_INLINE = r'(?:(?<!\\)((?:\\{2})+)(?=\$)|(?<!\\)(\$)(?!\s)((?:\\.|[^\\$])+?)(?<!\s)(?:\$))'
RE_DOLLAR_INLINE = r'(?:(?<!\\)((?:\\{2})+)(?=\$)|(?<!\\)(\$)((?:\\.|[^\\$])+?)(?:\$))'
RE_BRACKET_INLINE = r'(?:(?<!\\)((?:\\{2})+?)(?=\\\()|(?<!\\)(\\\()((?:\\[^)]|[^\\])+?)(?:\\\)))'
RE_DOLLAR_BLOCK = r'(?P<dollar>[$]{2})(?P<math>((?:\\.|[^\\])+?))(?P=dollar)'
RE_TEX_BLOCK = r'(?P<math2>\\begin\{(?P<env>[a-z]+\*?)\}(?:\\.|[^\\])+?\\end\{(?P=env)\})'
RE_BRACKET_BLOCK = r'\\\[(?P<math3>(?:\\[^\]]|[^\\])+?)\\\]'
def _escape(txt):
"""Basic html escaping."""
txt = txt.replace('&', '&')
txt = txt.replace('<', '<')
txt = txt.replace('>', '>')
txt = txt.replace('"', '"')
return txt
# Formatters usable with InlineHilite
@util.deprecated(
"The inline MathJax Preview formatter has been deprecated in favor of the configurable 'arithmatex_fenced_format'. "
"Please see relevant documentation for more information on how to switch before this function is "
"removed in the future."
)
def inline_mathjax_preview_format(math, language='math', class_name='arithmatex', md=None):
"""Inline math formatter with preview."""
return _inline_mathjax_format(math, preview=True)
@util.deprecated(
"The inline MathJax formatter has been deprecated in favor of the configurable 'arithmatex_fenced_format'. "
"Please see relevant documentation for more information on how to switch before this function is "
"removed in the future."
)
def inline_mathjax_format(math, language='math', class_name='arithmatex', md=None):
"""Inline math formatter."""
return _inline_mathjax_format(math, preview=False)
@util.deprecated(
"The inline generic math formatter has been deprecated in favor of the configurable 'arithmatex_inline_format'. "
"Please see relevant documentation for more information on how to switch before this function is "
"removed in the future."
)
def inline_generic_format(math, language='math', class_name='arithmatex', md=None, **kwargs):
"""Inline generic formatter."""
return _inline_generic_format(math, language, class_name, md, **kwargs)
def _inline_mathjax_format(math, language='math', class_name='arithmatex', md=None, tag='span', preview=False):
"""Inline math formatter."""
el = etree.Element(tag, {'class': 'arithmatex'})
if preview:
pre = etree.SubElement(el, 'span', {'class': 'MathJax_Preview'})
pre.text = md_util.AtomicString(math)
script = etree.SubElement(el, 'script', {'type': 'math/tex'})
script.text = md_util.AtomicString(math)
return el
def _inline_generic_format(math, language='math', class_name='arithmatex', md=None, wrap='\\({}\\)', tag='span'):
"""Inline generic formatter."""
el = etree.Element(tag, {'class': class_name})
el.text = md_util.AtomicString(wrap.format(math))
return el
def arithmatex_inline_format(**kwargs):
"""Specify which type of formatter you want and the wrapping tag."""
mode = kwargs.get('mode', 'generic')
tag = kwargs.get('tag', 'span')
preview = kwargs.get('preview', False)
if mode == 'generic':
return partial(_inline_generic_format, tag=tag)
elif mode == 'mathjax':
return partial(_inline_mathjax_format, preview=preview)
# Formatters usable with SuperFences
@util.deprecated(
"The fenced MathJax preview formatter has been deprecated in favor of the configurable 'arithmatex_fenced_format'. "
"Please see relevant documentation for more information on how to switch before this function is "
"removed in the future."
)
def fence_mathjax_preview_format(math, language='math', class_name='arithmatex', options=None, md=None, **kwargs):
"""Block MathJax formatter with preview."""
return _fence_mathjax_format(math, preview=True)
@util.deprecated(
"The fenced MathJax preview formatter has been deprecated in favor of the configurable 'arithmatex_fenced_format'. "
"Please see relevant documentation for more information on how to switch before this function is "
"removed in the future."
)
def fence_mathjax_format(math, language='math', class_name='arithmatex', options=None, md=None, **kwargs):
"""Block MathJax formatter."""
return _fence_mathjax_format(math, preview=False)
@util.deprecated(
"The generic math formatter has been deprecated in favor of the configurable 'arithmatex_fenced_format'. "
"Please see relevant documentation for more information on how to switch before this function is "
"removed in the future."
)
def fence_generic_format(math, language='math', class_name='arithmatex', options=None, md=None, **kwargs):
"""Generic block formatter."""
return _fence_generic_format(math, language, class_name, options, md, **kwargs)
def _fence_mathjax_format(
math, language='math', class_name='arithmatex', options=None, md=None, preview=False, tag="div", **kwargs
):
"""Block math formatter."""
text = f'<{tag} class="arithmatex">\n'
if preview:
text += (
'<div class="MathJax_Preview">\n' +
_escape(math) +
'\n</div>\n'
)
text += (
'<script type="math/tex; mode=display">\n' +
math +
'\n</script>\n'
)
text += '</div>'
return text
def _fence_generic_format(
math, language='math', class_name='arithmatex', options=None, md=None, wrap='\\[\n{}\n\\]', tag='div', **kwargs
):
"""Generic block formatter."""
classes = kwargs['classes']
id_value = kwargs['id_value']
attrs = kwargs['attrs']
classes.insert(0, class_name)
id_value = f' id="{id_value}"' if id_value else ''
classes = ' class="{}"'.format(' '.join(classes))
attrs = ' ' + ' '.join(f'{k}="{v}"' for k, v in attrs.items()) if attrs else ''
return f'<{tag}{id_value}{classes}{attrs}>{wrap.format(math)}</{tag}>'
def arithmatex_fenced_format(**kwargs):
"""Specify which type of formatter you want and the wrapping tag."""
mode = kwargs.get('mode', 'generic')
tag = kwargs.get('tag', 'div')
preview = kwargs.get('preview', False)
if mode == 'generic':
return partial(_fence_generic_format, tag=tag)
elif mode == 'mathjax':
return partial(_fence_mathjax_format, tag=tag, preview=preview)
class InlineArithmatexPattern(InlineProcessor):
"""Arithmatex inline pattern handler."""
ESCAPED_BSLASH = '{}{}{}'.format(md_util.STX, ord('\\'), md_util.ETX)
def __init__(self, pattern, config):
"""Initialize."""
# Generic setup
self.generic = config.get('generic', False)
wrap = config.get('tex_inline_wrap', ["\\(", "\\)"])
self.wrap = (
wrap[0].replace('{', '}}').replace('}', '}}') + '{}' + wrap[1].replace('{', '}}').replace('}', '}}')
)
self.inline_tag = config.get('inline_tag', 'span')
# Default setup
self.preview = config.get('preview', True)
InlineProcessor.__init__(self, pattern)
def handleMatch(self, m, data):
"""Handle notations and switch them to something that will be more detectable in HTML."""
# Handle escapes
groups = m.groups()
escapes = groups[0]
if not escapes and len(groups) > 3:
escapes = groups[3]
if escapes:
return escapes.replace('\\\\', self.ESCAPED_BSLASH), m.start(0), m.end(0)
# Handle Tex
math = groups[2]
if not math and len(groups) > 3:
math = groups[5]
if self.generic:
return _inline_generic_format(math, wrap=self.wrap, tag=self.inline_tag), m.start(0), m.end(0)
else:
return _inline_mathjax_format(math, tag=self.inline_tag, preview=self.preview), m.start(0), m.end(0)
class BlockArithmatexProcessor(BlockProcessor):
"""MathJax block processor to find $$MathJax$$ content."""
def __init__(self, pattern, config, md):
"""Initialize."""
# Generic setup
self.generic = config.get('generic', False)
wrap = config.get('tex_block_wrap', ['\\[', '\\]'])
self.wrap = (
wrap[0].replace('{', '}}').replace('}', '}}') + '{}' + wrap[1].replace('{', '}}').replace('}', '}}')
)
self.block_tag = config.get('block_tag', 'div')
# Default setup
self.preview = config.get('preview', False)
self.match = None
self.pattern = re.compile(pattern)
BlockProcessor.__init__(self, md.parser)
def test(self, parent, block):
"""Return 'True' for future Python Markdown block compatibility."""
self.match = self.pattern.match(block) if self.pattern is not None else None
return self.match is not None
def mathjax_output(self, parent, math):
"""Default MathJax output."""
grandparent = parent
parent = etree.SubElement(grandparent, self.block_tag, {'class': 'arithmatex'})
if self.preview:
preview = etree.SubElement(parent, 'div', {'class': 'MathJax_Preview'})
preview.text = md_util.AtomicString(math)
el = etree.SubElement(parent, 'script', {'type': 'math/tex; mode=display'})
el.text = md_util.AtomicString(math)
def generic_output(self, parent, math):
"""Generic output."""
el = etree.SubElement(parent, self.block_tag, {'class': 'arithmatex'})
el.text = md_util.AtomicString(self.wrap.format(math))
def run(self, parent, blocks):
"""Find and handle block content."""
blocks.pop(0)
groups = self.match.groupdict()
math = groups.get('math', '')
if not math:
math = groups.get('math2', '')
if not math:
math = groups.get('math3', '')
if self.generic:
self.generic_output(parent, math)
else:
self.mathjax_output(parent, math)
return True
class ArithmatexExtension(Extension):
"""Adds delete extension to Markdown class."""
def __init__(self, *args, **kwargs):
"""Initialize."""
self.config = {
'tex_inline_wrap': [
["\\(", "\\)"],
"Wrap inline content with the provided text ['open', 'close'] - Default: ['', '']"
],
'tex_block_wrap': [
["\\[", "\\]"],
"Wrap blick content with the provided text ['open', 'close'] - Default: ['', '']"
],
"smart_dollar": [True, "Use Arithmatex's smart dollars - Default True"],
"block_syntax": [
['dollar', 'square', 'begin'],
'Enable block syntax: "dollar" ($$...$$), "square" (\\[...\\]), and '
'"begin" (\\begin{env}...\\end{env}). - Default: ["dollar", "square", "begin"]'
],
"inline_syntax": [
['dollar', 'round'],
'Enable block syntax: "dollar" ($$...$$), "bracket" (\\(...\\)) '
' - Default: ["dollar", "round"]'
],
'generic': [False, "Output in a generic format for non MathJax libraries - Default: False"],
'preview': [
True,
"Insert a preview for scripts. - Default: False"
],
'block_tag': ['div', "Specify wrapper tag - Default 'div'"],
'inline_tag': ['span', "Specify wrapper tag - Default 'span'"]
}
super().__init__(*args, **kwargs)
def extendMarkdown(self, md):
"""Extend the inline and block processor objects."""
md.registerExtension(self)
util.escape_chars(md, ['$'])
config = self.getConfigs()
# Inline patterns
allowed_inline = set(config.get('inline_syntax', ['dollar', 'round']))
smart_dollar = config.get('smart_dollar', True)
inline_patterns = []
if 'dollar' in allowed_inline:
inline_patterns.append(RE_SMART_DOLLAR_INLINE if smart_dollar else RE_DOLLAR_INLINE)
if 'round' in allowed_inline:
inline_patterns.append(RE_BRACKET_INLINE)
if inline_patterns:
inline = InlineArithmatexPattern('(?:%s)' % '|'.join(inline_patterns), config)
md.inlinePatterns.register(inline, 'arithmatex-inline', 189.9)
# Block patterns
allowed_block = set(config.get('block_syntax', ['dollar', 'square', 'begin']))
block_pattern = []
if 'dollar' in allowed_block:
block_pattern.append(RE_DOLLAR_BLOCK)
if 'square' in allowed_block:
block_pattern.append(RE_BRACKET_BLOCK)
if 'begin' in allowed_block:
block_pattern.append(RE_TEX_BLOCK)
if block_pattern:
block = BlockArithmatexProcessor(r'(?s)^(?:%s)[ ]*$' % '|'.join(block_pattern), config, md)
md.parser.blockprocessors.register(block, "arithmatex-block", 79.9)
def makeExtension(*args, **kwargs):
"""Return extension."""
return ArithmatexExtension(*args, **kwargs)