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)
| 21 | SUBPHRASE = re.compile('([:.;?!][ ])(%s)' % SMALL) |
| 22 | |
| 23 | def 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 | |
| 60 | class TitlecaseTests(unittest.TestCase): |
| 61 | """Tests to ensure titlecase follows all of the rules""" |