diff --git a/add_links/match.py b/add_links/match.py index 732e3ba..4c6af97 100644 --- a/add_links/match.py +++ b/add_links/match.py @@ -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() diff --git a/test_match.py b/test_match.py index c50470e..a9bbb7a 100644 --- a/test_match.py +++ b/test_match.py @@ -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()