Skip to content

Commit 28f3eed

Browse files
fix(research): keep trailing punctuation out of links
1 parent 1e17dbc commit 28f3eed

2 files changed

Lines changed: 46 additions & 10 deletions

File tree

‎src/visual_report.py‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,23 @@ def _autolink_urls(md_text: str) -> str:
6161
def _link(match: re.Match) -> str:
6262
url = match.group(1)
6363
trailing = ""
64-
# Research models commonly cite a bare URL as ``[https://example]``.
65-
# The opening bracket is outside this match, but the old URL pattern
66-
# consumed the closing bracket and made it part of the generated href.
67-
# Remove only unmatched closing brackets so a balanced IPv6 literal
68-
# such as ``http://[::1]`` remains intact.
69-
while url.endswith("]") and url.count("]") > url.count("["):
70-
url = url[:-1]
71-
trailing = "]" + trailing
64+
# Research prose commonly ends a bare URL with sentence punctuation,
65+
# and models also emit citation-like ``[https://example]`` text. Peel
66+
# those delimiters from right to left so ``https://example].`` keeps
67+
# both ``]`` and ``.`` in the prose rather than in the generated href.
68+
# An unmatched closing bracket is removed, while a balanced IPv6
69+
# literal such as ``http://[::1]`` remains part of the URL.
70+
while url:
71+
last = url[-1]
72+
if last in ".,;:!?":
73+
url = url[:-1]
74+
trailing = last + trailing
75+
continue
76+
if last == "]" and url.count("]") > url.count("["):
77+
url = url[:-1]
78+
trailing = last + trailing
79+
continue
80+
break
7281
return f"[{url}]({url}){trailing}"
7382

7483
# Match bare URLs not already inside ](...)

‎tests/test_visual_report_nonstring.py‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,34 @@ def test_bracketed_bare_url_keeps_bracket_out_of_href():
3131
assert soup.get_text() == "See [https://example.com/article] for details."
3232

3333

34+
def test_bracketed_bare_url_keeps_closing_bracket_and_period_out_of_href():
35+
text = "Some sentence [https://thisisareference.com/]. The next sentence is here."
36+
soup = BeautifulSoup(_md_to_html(text), "html.parser")
37+
link = soup.find("a")
38+
39+
assert link is not None
40+
assert link["href"] == "https://thisisareference.com/"
41+
assert link.get_text() == "https://thisisareference.com/"
42+
assert soup.get_text() == text
43+
44+
45+
def test_bare_url_keeps_sentence_period_out_of_href():
46+
text = "See https://example.com/article. Then continue."
47+
soup = BeautifulSoup(_md_to_html(text), "html.parser")
48+
link = soup.find("a")
49+
50+
assert link is not None
51+
assert link["href"] == "https://example.com/article"
52+
assert link.get_text() == "https://example.com/article"
53+
assert soup.get_text() == text
54+
55+
3456
def test_balanced_ipv6_bracket_remains_part_of_url():
35-
linked = _autolink_urls("Open http://[::1] locally.")
57+
text = "Open http://[::1]. Then continue."
58+
soup = BeautifulSoup(_md_to_html(text), "html.parser")
59+
link = soup.find("a")
3660

37-
assert "](http://[::1])" in linked
61+
assert link is not None
62+
assert link["href"] == "http://[::1]"
63+
assert link.get_text() == "http://[::1]"
64+
assert soup.get_text() == text

0 commit comments

Comments
 (0)