Skip to content

Commit 43e7332

Browse files
committed
feat: return original eqn if invalid
Do not convert it partially
1 parent 698856b commit 43e7332

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

openedx/core/djangoapps/content/search/plain_text_math.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88

99

1010
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."""
1216

1317

1418
class PlainTextMath:
@@ -58,7 +62,7 @@ def _nested_bracket_matcher(equation: str, opening_pattern: str) -> str:
5862
"""
5963
start = equation.find(opening_pattern)
6064
if start == -1:
61-
raise InvalidMathEquation()
65+
raise EqnPatternNotFound()
6266
open_count = 0
6367
inner_start = start + len(opening_pattern)
6468
for i, char in enumerate(equation[inner_start:]):
@@ -88,7 +92,7 @@ def _fraction_handler(self, equation: str) -> str:
8892
"""
8993
try:
9094
n_start, n_inner_start, n_inner_end, n_end = self._nested_bracket_matcher(equation, "\\frac{")
91-
except InvalidMathEquation:
95+
except EqnPatternNotFound:
9296
return equation
9397

9498
numerator = equation[n_inner_start:n_inner_end]
@@ -97,7 +101,7 @@ def _fraction_handler(self, equation: str) -> str:
97101

98102
try:
99103
_, d_inner_start, d_inner_end, d_end = self._nested_bracket_matcher(equation[n_end:], "{")
100-
except InvalidMathEquation:
104+
except EqnPatternNotFound:
101105
return equation
102106

103107
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:
116120
inner_text = equation[inner_start:inner_end]
117121
inner_text = self._nested_text_extractor(inner_text, pattern)
118122
equation = equation[:start] + inner_text + equation[end:]
119-
except InvalidMathEquation:
123+
except EqnPatternNotFound:
120124
pass
121125
return equation
122126

@@ -138,10 +142,15 @@ def run(self, eqn_matches: re.Match) -> str:
138142
"""
139143
groups = eqn_matches.groups()
140144
for group in groups:
141-
if group:
145+
if not group:
146+
continue
147+
original = group
148+
try:
142149
group = self._handle_replacements(group)
143150
group = self._fraction_handler(group)
144151
return unicodeit.replace(group)
152+
except Exception: # pylint: disable=broad-except
153+
return original
145154
return None
146155

147156

openedx/core/djangoapps/content/search/tests/test_documents.py

+5
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,11 @@ def test_mathjax_plain_text_conversion_for_search(self):
574574
('Fraction error: \\( \\frac{2} \\)', 'Fraction error: \\frac{2}'),
575575
('Fraction error 2: \\( \\frac{\\frac{2}{3}{4} \\)', 'Fraction error 2: \\frac{\\frac{2}{3}{4}'),
576576
('Unclosed: [mathjaxinline]x^2', 'Unclosed: [mathjaxinline]x^2'),
577+
(
578+
'Missing closing bracket: \\( \\frac{\\frac{2} {3}{\\frac{4}{3}} \\)',
579+
'Missing closing bracket: \\frac{\\frac{2} {3}{\\frac{4}{3}}'
580+
),
581+
('No equation: normal text', 'No equation: normal text'),
577582
]
578583
# pylint: enable=line-too-long
579584
block = BlockFactory.create(

0 commit comments

Comments
 (0)