File size: 704 Bytes
758988d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import re
from latex2mathml.converter import convert as latex_to_mathml

def convert_inline_and_block_latex_to_mathml(text):
    def block_replacer(match):
        try:
            mathml = latex_to_mathml(match.group(1))
            return f"<div class='math'>{mathml}</div>"
        except Exception:
            return match.group(0)

    def inline_replacer(match):
        try:
            mathml = latex_to_mathml(match.group(1))
            return f"<span class='math'>{mathml}</span>"
        except Exception:
            return match.group(0)

    text = re.sub(r"\$\$(.+?)\$\$", block_replacer, text, flags=re.DOTALL)
    text = re.sub(r"\\\((.+?)\\\)", inline_replacer, text)
    return text