diff --git a/add_links/match.py b/add_links/match.py index a3440e7..732e3ba 100644 --- a/add_links/match.py +++ b/add_links/match.py @@ -205,7 +205,11 @@ def match_found(m: re.Match[str], q: str, linkto: str | None) -> str: if pos == 0 or m.string[pos - 1] == "\n": replacement = replacement[0].upper() + replacement[1:] if linkto: - if linkto[0].isupper() and replacement[0] == linkto[0].lower(): + if ( + linkto[0].isupper() + and replacement[0].islower() + and not is_title_case(replacement) + ): linkto = linkto[0].lower() + linkto[1:] elif replacement[0].isupper(): linkto = linkto[0].upper() + linkto[1:] diff --git a/test_match.py b/test_match.py new file mode 100644 index 0000000..c50470e --- /dev/null +++ b/test_match.py @@ -0,0 +1,75 @@ +import unittest + +from add_links.match import NoMatch, find_link_in_content + + +class FindLinkInContentTests(unittest.TestCase): + def test_links_first_of_two_adjacent_existing_links(self) -> None: + content = ( + "'''''North Star''''' is a 1974 British [[thriller novel]] by " + "[[Hammond Innes]].Vinson & Kirkpatrick p.455 " + "A man tries to prevent a plot to blow up a [[North Sea]] [[oil rig]]." + ) + + new_content, replacement, replaced_text = find_link_in_content( + "North Sea oil", content + ) + + self.assertEqual( + new_content, + "'''''North Star''''' is a 1974 British [[thriller novel]] by " + "[[Hammond Innes]].Vinson & Kirkpatrick p.455 " + "A man tries to prevent a plot to blow up a " + "[[North Sea oil|North Sea]] [[oil rig]].", + ) + self.assertEqual(replacement, "North Sea oil") + self.assertEqual(replaced_text, "North Sea]] [[oil") + + def test_merges_existing_link_with_following_plain_text(self) -> None: + content = "[[anti-globalization]] movement" + + new_content, replacement, replaced_text = find_link_in_content( + "anti-globalization movement", content + ) + + self.assertEqual(new_content, "[[anti-globalization movement]]") + self.assertEqual(replacement, "anti-globalization movement") + self.assertEqual(replaced_text, "anti-globalization]] movement") + + def test_links_prefix_before_existing_link(self) -> None: + content = "cross-platform [[interchange station]]" + + new_content, replacement, replaced_text = find_link_in_content( + "cross-platform interchange", content + ) + + self.assertEqual( + new_content, + "[[cross-platform interchange|cross-platform]] [[interchange station]]", + ) + self.assertEqual(replacement, "cross-platform interchange") + self.assertEqual(replaced_text, "cross-platform [[interchange") + + def test_lowercases_redirect_target_for_sentence_case_display_text(self) -> None: + content = ( + "The absence of a voters' roll requirement was controversial, with " + "the NCA alleging potential rigging through ballot stuffing and " + "coercion in rural areas." + ) + + new_content, replacement, replaced_text = find_link_in_content( + "ballot stuffing", content, "Electoral fraud" + ) + + self.assertEqual( + new_content, + "The absence of a voters' roll requirement was controversial, with " + "the NCA alleging potential rigging through " + "[[electoral fraud|ballot stuffing]] and coercion in rural areas.", + ) + self.assertEqual(replacement, "electoral fraud|ballot stuffing") + self.assertEqual(replaced_text, "ballot stuffing") + + +if __name__ == "__main__": + unittest.main()