9
9
10
10
class InvalidMathEquation (Exception ):
11
11
"""Raised when converting mathjax equations to plain text fails"""
12
- pass
13
12
14
13
15
14
class PlainTextMath :
@@ -36,7 +35,6 @@ class PlainTextMath:
36
35
)
37
36
regex_replacements = (
38
37
# Makes text bold, so not required in plain text.
39
- (re .compile (r'\\mathbf{(.*?)}' ), r"\1" ),
40
38
(re .compile (r'{\\bf (.*?)}' ), r"\1" ),
41
39
)
42
40
extract_inner_texts = (
@@ -52,7 +50,7 @@ def _nested_bracket_matcher(equation: str, opening_pattern: str) -> str:
52
50
53
51
Args:
54
52
equation: string
55
- opening_pattern: for example, \ mathbf{
53
+ opening_pattern: for example, ` \\ mathbf{`
56
54
57
55
Returns:
58
56
String inside the eqn brackets
@@ -108,18 +106,27 @@ def _fraction_handler(self, equation: str) -> str:
108
106
equation = equation [:n_start ] + f"({ numerator } /{ denominator } )" + equation [n_end + d_end :]
109
107
return equation
110
108
109
+ def _nested_text_extractor (self , equation : str , pattern : str ) -> str :
110
+ """
111
+ Recursively extracts text from equation for given pattern
112
+ """
113
+ try :
114
+ start , inner_start , inner_end , end = self ._nested_bracket_matcher (equation , pattern )
115
+ inner_text = equation [inner_start :inner_end ]
116
+ inner_text = self ._nested_text_extractor (inner_text , pattern )
117
+ equation = equation [:start ] + inner_text + equation [end :]
118
+ except InvalidMathEquation :
119
+ pass
120
+ return equation
121
+
111
122
def _handle_replacements (self , equation : str ) -> str :
112
123
"""
113
124
Makes a bunch of replacements in equation string.
114
125
"""
115
126
for q , replacement in self .eqn_replacements :
116
127
equation = equation .replace (q , replacement )
117
128
for pattern in self .extract_inner_texts :
118
- try :
119
- start , inner_start , inner_end , end = self ._nested_bracket_matcher (equation , pattern )
120
- equation = equation [:start ] + equation [inner_start :inner_end ] + equation [end :]
121
- except InvalidMathEquation :
122
- continue
129
+ equation = self ._nested_text_extractor (equation , pattern )
123
130
for pattern , replacement in self .regex_replacements :
124
131
equation = re .sub (pattern , replacement , equation )
125
132
return equation
0 commit comments