Skip to content

Commit 085316e

Browse files
committed
Enhance EquationFixer to support bracket blocks with optional blank lines and indentation; add corresponding test case in test.py and bump version to 1.1.7
1 parent e2675e4 commit 085316e

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.1.6"
1+
__version__ = "1.1.7"

src/markdown_equations_fixer/cli.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,27 @@ def fix_equations(self, content: str) -> str:
3434
"""Fix mathematical equations in markdown content."""
3535
try:
3636
# Pattern to match block equations delimited by standalone lines with [ and ]
37-
# Example:
38-
# [\n
39-
# equation content\n
40-
# ]
41-
# This converts to:
42-
# $$\n
43-
# equation content\n
44-
# $$
45-
content = re.sub(
46-
r"(?ms)^\[\s*$\n(.*?)\n^\]\s*$",
47-
r"$$\n\1\n$$",
48-
content,
37+
# Allow optional blank/whitespace-only lines after [ and before ]
38+
# and preserve inner content trimmed of leading/trailing blank lines.
39+
bracket_block_pattern = re.compile(
40+
r"(?ms)^[\t ]*\[[\t ]*\r?\n(.*?)(?:\r?\n)^[\t ]*\][\t ]*(?:\r?\n|$)",
4941
)
5042

43+
def _replace_bracket_block(match: re.Match) -> str:
44+
inner = match.group(1)
45+
# Trim leading/trailing blank lines in the captured block
46+
lines = inner.splitlines()
47+
start = 0
48+
end = len(lines)
49+
while start < end and lines[start].strip() == "":
50+
start += 1
51+
while end > start and lines[end - 1].strip() == "":
52+
end -= 1
53+
inner_trimmed = "\n".join(lines[start:end])
54+
return f"$$\n{inner_trimmed}\n$$\n"
55+
56+
content = bracket_block_pattern.sub(_replace_bracket_block, content)
57+
5158
# Pattern to match block equations: \[ ... \]
5259
# The re.DOTALL flag allows the match to span multiple lines.
5360
content = re.sub(

src/test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ def test_fix_bracket_line_block(fixer):
4949
assert fixer.fix_equations(input_text) == expected
5050

5151

52+
def test_fix_bracket_block_with_blank_lines_and_indent(fixer):
53+
"""Blocks with extra blank lines and indentation should be converted."""
54+
input_text = (
55+
"\n[\n\n Z₁ = XW₁ + b₁\n\n]\n\n" # first block
56+
"[\n A₁ = ReLU(Z₁)\n]\n\n" # second block
57+
"[\n Z₂ = A₁W₂ + b₂\n]\n\n" # third block
58+
"[\n \\hat{y} = Z₂\n]\n\n" # fourth block
59+
"[\n L = \\frac{1}{2m} \\sum (y - \\hat{y})^2\n]\n" # fifth block
60+
)
61+
output = fixer.fix_equations(input_text)
62+
# Ensure each becomes a $$ block and inner content preserved sans surrounding blanks
63+
assert "$$\nZ₁ = XW₁ + b₁\n$$" in output
64+
assert "$$\nA₁ = ReLU(Z₁)\n$$" in output
65+
assert "$$\nZ₂ = A₁W₂ + b₂\n$$" in output
66+
assert "$$\n\\hat{y} = Z₂\n$$" in output
67+
assert "$$\nL = \\frac{1}{2m} \\sum (y - \\hat{y})^2\n$$" in output
68+
5269
def test_validate_paths_recursive(temp_dir):
5370
"""Test recursive path validation."""
5471
paths = [temp_dir]

0 commit comments

Comments
 (0)