8
8
9
9
10
10
class InvalidMathEquation (Exception ):
11
- """Raised when converting mathjax equations to plain text fails"""
11
+ """Raised when mathjax equation is invalid. This is used to skip all transformations."""
12
+
13
+
14
+ class EqnPatternNotFound (Exception ):
15
+ """Raised when a pattern is not found in equation. This is used to skip a specific transformation."""
12
16
13
17
14
18
class PlainTextMath :
@@ -58,7 +62,7 @@ def _nested_bracket_matcher(equation: str, opening_pattern: str) -> str:
58
62
"""
59
63
start = equation .find (opening_pattern )
60
64
if start == - 1 :
61
- raise InvalidMathEquation ()
65
+ raise EqnPatternNotFound ()
62
66
open_count = 0
63
67
inner_start = start + len (opening_pattern )
64
68
for i , char in enumerate (equation [inner_start :]):
@@ -88,7 +92,7 @@ def _fraction_handler(self, equation: str) -> str:
88
92
"""
89
93
try :
90
94
n_start , n_inner_start , n_inner_end , n_end = self ._nested_bracket_matcher (equation , "\\ frac{" )
91
- except InvalidMathEquation :
95
+ except EqnPatternNotFound :
92
96
return equation
93
97
94
98
numerator = equation [n_inner_start :n_inner_end ]
@@ -97,7 +101,7 @@ def _fraction_handler(self, equation: str) -> str:
97
101
98
102
try :
99
103
_ , d_inner_start , d_inner_end , d_end = self ._nested_bracket_matcher (equation [n_end :], "{" )
100
- except InvalidMathEquation :
104
+ except EqnPatternNotFound :
101
105
return equation
102
106
103
107
denominator = equation [n_end + d_inner_start :n_end + d_inner_end ]
@@ -116,7 +120,7 @@ def _nested_text_extractor(self, equation: str, pattern: str) -> str:
116
120
inner_text = equation [inner_start :inner_end ]
117
121
inner_text = self ._nested_text_extractor (inner_text , pattern )
118
122
equation = equation [:start ] + inner_text + equation [end :]
119
- except InvalidMathEquation :
123
+ except EqnPatternNotFound :
120
124
pass
121
125
return equation
122
126
@@ -138,10 +142,15 @@ def run(self, eqn_matches: re.Match) -> str:
138
142
"""
139
143
groups = eqn_matches .groups ()
140
144
for group in groups :
141
- if group :
145
+ if not group :
146
+ continue
147
+ original = group
148
+ try :
142
149
group = self ._handle_replacements (group )
143
150
group = self ._fraction_handler (group )
144
151
return unicodeit .replace (group )
152
+ except Exception : # pylint: disable=broad-except
153
+ return original
145
154
return None
146
155
147
156
0 commit comments