MCPcopy Create free account
hub / github.com/PageBot/PageBot / titlecase

Function titlecase

Lib/pagebot/contributions/filibuster/titlecase.py:23–58  ·  view source on GitHub ↗

Titlecases input text. This filter changes all words to Title Caps, and attempts to be clever about *uncapitalizing* SMALL words like a/an/the in the input. The list of "SMALL words" which are not capped comes from the New York Times Manual of Style, plus 'vs' and 'v'.

(text)

Source from the content-addressed store, hash-verified

21SUBPHRASE = re.compile('([:.;?!][ ])(%s)' % SMALL)
22
23def titlecase(text):
24 """Titlecases input text.
25
26 This filter changes all words to Title Caps, and attempts to be clever
27 about *uncapitalizing* SMALL words like a/an/the in the input.
28
29 The list of "SMALL words" which are not capped comes from the New York
30 Times Manual of Style, plus 'vs' and 'v'.
31 """
32 words = re.split(r'\s', text)
33 line = []
34
35 for word in words:
36 if INLINE_PERIOD.search(word) or UC_ELSEWHERE.match(word):
37 line.append(word)
38 continue
39 if SMALL_WORDS.match(word):
40 line.append(word.lower())
41 continue
42 line.append(CAPFIRST.sub(lambda m: m.group(0).upper(), word))
43
44 line = " ".join(line)
45
46 line = SMALL_FIRST.sub(lambda m: '%s%s' % (
47 m.group(1),
48 m.group(2).capitalize()
49 ), line)
50
51 line = SMALL_LAST.sub(lambda m: m.group(0).capitalize(), line)
52
53 line = SUBPHRASE.sub(lambda m: '%s%s' % (
54 m.group(1),
55 m.group(2).capitalize()
56 ), line)
57
58 return line
59
60class TitlecaseTests(unittest.TestCase):
61 """Tests to ensure titlecase follows all of the rules"""

Callers 15

replacetagMethod · 0.90
test_q_and_aMethod · 0.85
test_at_and_tMethod · 0.85
test_apple_dealMethod · 0.85
test_this_v_thatMethod · 0.85
test_this_v_that2Method · 0.85
test_this_vs_thatMethod · 0.85
test_this_vs_that2Method · 0.85
test_apple_secMethod · 0.85
test_small_word_endMethod · 0.85

Calls 3

matchMethod · 0.80
subMethod · 0.80
appendMethod · 0.45

Tested by 15

test_q_and_aMethod · 0.68
test_at_and_tMethod · 0.68
test_apple_dealMethod · 0.68
test_this_v_thatMethod · 0.68
test_this_v_that2Method · 0.68
test_this_vs_thatMethod · 0.68
test_this_vs_that2Method · 0.68
test_apple_secMethod · 0.68
test_small_word_endMethod · 0.68