Skip to content

Commit

Permalink
Merge pull request #230 from freelawproject/229-fix-maybe-balance-sty…
Browse files Browse the repository at this point in the history
…le-tags

fix(utils.maybe_balance_style_tags): use re.escape to form regex
  • Loading branch information
grossir authored Feb 21, 2025
2 parents dd06750 + 7f8f303 commit 929b68f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
11 changes: 5 additions & 6 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@ Fixes:

## Current

**2.6.11 - 2025-02-20**

Fixes:
- Add regex escapig to `utils.maybe_balance_style_tags`

**2.6.10 - 2025-02-20**

Features:
- Adds support to correct citation page

Changes:
- None

Fixes:
- None

## Past

**2.6.9 - 2025-02-20**
Expand Down
4 changes: 2 additions & 2 deletions eyecite/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def maybe_balance_style_tags(
end + len(closing_tag) + tolerance, len(plain_text)
)
if end_match := re.search(
rf"{span_text}\s*{closing_tag}",
rf"{re.escape(span_text)}\s*{re.escape(closing_tag)}",
plain_text[start:extended_end],
flags=re.MULTILINE,
):
Expand All @@ -298,7 +298,7 @@ def maybe_balance_style_tags(
# look for opening tag before the start
extended_start = min(start - len(opening_tag) - tolerance, 0)
if start_match := re.search(
rf"{opening_tag}\s*{span_text}",
rf"{re.escape(opening_tag)}\s*{re.escape(span_text)}",
plain_text[extended_start:end],
flags=re.MULTILINE,
):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.poetry]
version = "2.6.10"
version = "2.6.11"
authors = ["Free Law Project <[email protected]>"]
classifiers = [
"Development Status :: 5 - Production/Stable",
Expand Down
10 changes: 10 additions & 0 deletions tests/test_AnnotateTest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import re
from pathlib import Path
from unittest import TestCase

from eyecite import annotate_citations, clean_text, get_citations
from eyecite.utils import maybe_balance_style_tags


class AnnotateTest(TestCase):
Expand Down Expand Up @@ -230,6 +232,14 @@ def lower_annotator(before, text, after):
)
self.assertEqual(annotated, expected)

def test_tag_balancing(self):
"""Test trickier tag balancing cases"""
string = "Something; In <em>Nobelman </em> at 332, 113 S.Ct. 2106 (2010); Something else"
span_text = "Nobelman </em> at 332, 113 S.Ct. 2106 (2010)"
start, end = re.search(re.escape(span_text), string).span()
_, _, balanced = maybe_balance_style_tags(start, end, string)
self.assertTrue(balanced.startswith("<em>"))

def test_long_diff(self):
"""Does diffing work across a long text with many changes?"""
opinion_text = (
Expand Down

0 comments on commit 929b68f

Please sign in to comment.