Skip to content

Commit 257fbc9

Browse files
committed
fix: titleSs() handles apostrophes correctly, see #453
str.title() treats apostrophes as word boundaries, capitalizing the following letter (e.g. "page's title" became "Page'S Title"). Apply the same magicword workaround used for 'ß' so apostrophes are preserved as in-word characters. This issue was reported in #453.
1 parent 76898f6 commit 257fbc9

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

otterwiki/util.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,27 @@ def split_path(path: str) -> List[str]:
116116

117117
def titleSs(s):
118118
"""
119-
This function is a workaround for str.title() not knowing uppercase 'ß'.
119+
This function is a workaround for str.title() not knowing uppercase 'ß'
120+
and treating the apostrophe as a word boundary.
120121
"""
121-
if 'ß' not in s:
122-
return s.title()
123-
_magicword = 'M🙉A🙈G🙊I🐤C🐣W🐥O🦆R🐔D'
124-
while _magicword in s:
125-
_magicword = 2 * _magicword
126-
s = s.replace('ß', _magicword)
122+
_eszett = ''
123+
if 'ß' in s:
124+
_eszett = 'E🙉S🙈Z🙊E🐤T🐣T'
125+
while _eszett in s:
126+
_eszett = 2 * _eszett
127+
s = s.replace('ß', _eszett)
128+
_apostrophe = ''
129+
if "'" in s:
130+
_apostrophe = 'A🙉P🙈O🙊S🐤T🐣R🐥O🦆P🐔H🦜E'
131+
while _apostrophe in s:
132+
_apostrophe = 2 * _apostrophe
133+
s = s.replace("'", _apostrophe)
127134
s = s.title()
128-
return re.sub(re.escape(_magicword), 'ß', s, flags=re.IGNORECASE)
135+
if _eszett:
136+
s = re.sub(re.escape(_eszett), 'ß', s, flags=re.IGNORECASE)
137+
if _apostrophe:
138+
s = re.sub(re.escape(_apostrophe), "'", s, flags=re.IGNORECASE)
139+
return s
129140

130141

131142
def get_pagepath(pagename):

tests/test_util.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ def test_titleSs():
169169
)
170170
assert "\"Foobarß\"" == titleSs("\"foobarß\"")
171171
assert "Foobarß" == titleSs("foobarß")
172+
assert "Page's Title" == titleSs("page's title")
173+
assert "Don't Stop" == titleSs("don't stop")
174+
assert "It's Foobarß" == titleSs("it's foobarß")
172175

173176

174177
def test_patchset2filedict():

0 commit comments

Comments
 (0)