leonarb commited on
Commit
758988d
·
verified ·
1 Parent(s): 1611df7

Create mathml_utils.py

Browse files
Files changed (1) hide show
  1. mathml_utils.py +21 -0
mathml_utils.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from latex2mathml.converter import convert as latex_to_mathml
3
+
4
+ def convert_inline_and_block_latex_to_mathml(text):
5
+ def block_replacer(match):
6
+ try:
7
+ mathml = latex_to_mathml(match.group(1))
8
+ return f"<div class='math'>{mathml}</div>"
9
+ except Exception:
10
+ return match.group(0)
11
+
12
+ def inline_replacer(match):
13
+ try:
14
+ mathml = latex_to_mathml(match.group(1))
15
+ return f"<span class='math'>{mathml}</span>"
16
+ except Exception:
17
+ return match.group(0)
18
+
19
+ text = re.sub(r"\$\$(.+?)\$\$", block_replacer, text, flags=re.DOTALL)
20
+ text = re.sub(r"\\\((.+?)\\\)", inline_replacer, text)
21
+ return text