Skip to content

Commit 9b631f7

Browse files
authored
Fix bugs in title-casing (#1636)
Words like `dog's` with a straight or curly apostrophe caused the `s` to be uppercase. Also, phrases like `"a small dog"` (including the quotes) would leave the `a` lowercase. Fixes #1635
1 parent b296b3a commit 9b631f7

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/guiguts/maintext.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3244,11 +3244,14 @@ def capitalize_first_letter(match: re.Match[str]) -> str:
32443244

32453245
# Look for word characters either at the start of the string, or which
32463246
# immediately follow whitespace or punctuation; then apply capitalization.
3247-
s2 = re.sub(r"(?<=\s|^|\p{P}\s?)(\w+)", capitalize_first_letter, s.lower())
3247+
s2 = re.sub(
3248+
r"(?<=[\s\"“‘']|^|\p{P}\s?)(\w[\w'’]*)", capitalize_first_letter, s.lower()
3249+
)
32483250

32493251
# Edge case: if the string started with a word found in exception_words, it
3250-
# will have been lowercased erroneously.
3251-
return s2[0].upper() + s2[1:]
3252+
# will have been lowercased erroneously. Also need to allow for leading quotes/apostrophe
3253+
# so uppercase first letter.
3254+
return re.sub(r"(\p{L})", lambda m: m.group(1).upper(), s2, count=1)
32523255

32533256
def set_languages(self, languages: str) -> None:
32543257
"""Set languages used in text.

0 commit comments

Comments
 (0)