Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions titlecase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
24 changes: 24 additions & 0 deletions titlecase/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down