From ab2e3a0783d9b3f91dd4a8c463ee222684738442 Mon Sep 17 00:00:00 2001 From: Nash Aschenbath Date: Wed, 25 Nov 2026 19:03:02 +0800 Subject: [PATCH] Normalize apostrophe names before casing all-capital input --- titlecase/__init__.py | 2 ++ titlecase/tests.py | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/titlecase/__init__.py b/titlecase/__init__.py index fd24431..4bf15ba 100755 --- a/titlecase/__init__.py +++ b/titlecase/__init__.py @@ -123,6 +123,8 @@ def titlecase(text, callback=None, small_first_last=True, preserve_blank_lines=F continue if APOS_SECOND.match(word): + if all_caps: + word = word.lower() if len(word[0]) == 1 and word[0] not in 'aeiouAEIOU': word = word[0].lower() + word[1] + word[2].upper() + word[3:] else: diff --git a/titlecase/tests.py b/titlecase/tests.py index 9265ec0..d698f17 100644 --- a/titlecase/tests.py +++ b/titlecase/tests.py @@ -416,6 +416,30 @@ def test_technical_acronyms(self): 'Sending UDP Packets Over PPPoE Works Great') +class TestApostropheNames(unittest.TestCase): + def test_apostrophe_names_on_all_caps_lines(self): + for source, expected in [ + ("D'ARTEMIS OF LONDON", "d'Artemis of London"), + ("O'BRIEN AND D'ARCY", "O'Brien and d'Arcy"), + ("O'BRIEN'S HOUSE IS FOR SALE", "O'Brien's House Is for Sale"), + ("L'AVENTURE", "l'Aventure"), + ("O‘BRIEN AND D‘ARCY", "O‘Brien and d‘Arcy"), + ("meet O'BRIEN and d'Artemis", "Meet O'BRIEN and d'Artemis"), + ]: + with self.subTest(source=source): + self.assertEqual(titlecase(source), expected) + + def test_apostrophe_callback_keeps_original_input(self): + calls = [] + + def callback(word, **kwargs): + calls.append((word, kwargs['all_caps'])) + return word + + self.assertEqual(titlecase("O'BRIEN", callback=callback), "O'BRIEN") + self.assertEqual(calls, [("O'BRIEN", True)]) + + class TestBlankLines(unittest.TestCase): # Really, it's a bit odd that the default behavior is to delete blank lines, # but that's what it was from day one, so we're kind of stuck with that.