Avoid splitting links on italic text

This commit is contained in:
Edward Betts 2026-05-18 23:28:28 +01:00
parent 165acb8e2e
commit d1efae7745
2 changed files with 26 additions and 1 deletions

View file

@ -116,10 +116,15 @@ def find_cite_template_spans(text: str) -> list[tuple[int, int]]:
def parse_cite(text: str) -> typing.Iterator[tuple[str, str]]:
"""Parse citations yielding (type, chunk) tuples, skipping ref tags, cite templates, and external links."""
regions = [(m.start(), m.end()) for m in re_cite.finditer(text)]
link_spans = [(m.start(), m.end()) for m in re_link_in_text.finditer(text)]
regions.extend(find_cite_template_spans(text))
regions.extend((m.start(), m.end()) for m in re_no_param_template.finditer(text))
regions.extend((m.start(), m.end()) for m in re_external_link.finditer(text))
regions.extend((m.start(), m.end()) for m in re_italic.finditer(text))
regions.extend(
(m.start(), m.end())
for m in re_italic.finditer(text)
if not any(start <= m.start() and m.end() <= end for start, end in link_spans)
)
regions.extend((m.start(), m.end()) for m in re_bullet_with_url.finditer(text))
regions.sort()

View file

@ -70,6 +70,26 @@ class FindLinkInContentTests(unittest.TestCase):
self.assertEqual(replacement, "electoral fraud|ballot stuffing")
self.assertEqual(replaced_text, "ballot stuffing")
def test_italic_text_inside_existing_link_does_not_split_link(self) -> None:
content = (
'For instance, "[[capitalism]]" is the name for the '
"[[Capitalist mode of production (Marxist theory)|capitalist "
"''mode of production'']] in which the means of production"
)
new_content, replacement, replaced_text = find_link_in_content(
"capitalist mode of production", content, "Means of production"
)
self.assertEqual(
new_content,
'For instance, "[[capitalism]]" is the name for the '
"[[Capitalist mode of production (Marxist theory)|capitalist "
"''mode of production'']] in which the [[means of production]]",
)
self.assertEqual(replacement, "means of production")
self.assertEqual(replaced_text, "means of production")
if __name__ == "__main__":
unittest.main()