@@ -77,14 +77,18 @@ def set_small_word_list(small=SMALL):
7777 SUBPHRASE = regex .compile (r'([:.;?!][ ])(%s)' % small )
7878
7979
80- def titlecase (text , callback = None , small_first_last = True , preserve_blank_lines = False ):
80+ def titlecase (text , callback = None , small_first_last = True , preserve_blank_lines = False , normalise_space_characters = False ):
8181 """
8282 :param text: Titlecases input text
8383 :param callback: Callback function that returns the titlecase version of a specific word
8484 :param small_first_last: Capitalize small words (e.g. 'A') at the beginning; disabled when recursing
85+ :param preserve_blank_lines: Preserve blank lines in the output
86+ :param normalise_space_characters: Convert all original spaces to normal space characters
8587 :type text: str
8688 :type callback: function
8789 :type small_first_last: bool
90+ :type preserve_blank_lines: bool
91+ :type normalise_space_characters: bool
8892
8993 This filter changes all words to Title Caps, and attempts to be clever
9094 about *un*capitalizing SMALL words like a/an/the in the input.
@@ -100,7 +104,9 @@ def titlecase(text, callback=None, small_first_last=True, preserve_blank_lines=F
100104 processed = []
101105 for line in lines :
102106 all_caps = line .upper () == line
103- words = regex .split ('[\t ]' , line )
107+ split_line = regex .split (r'(\s)' , line )
108+ words = split_line [::2 ]
109+ spaces = split_line [1 ::2 ]
104110 tc_line = []
105111 for word in words :
106112 if callback :
@@ -188,7 +194,13 @@ def titlecase(text, callback=None, small_first_last=True, preserve_blank_lines=F
188194 lambda m : m .group (0 ).capitalize (), tc_line [- 1 ]
189195 )
190196
191- result = " " .join (tc_line )
197+ if normalise_space_characters :
198+ result = " " .join (tc_line )
199+ else :
200+ line_to_be_joined = tc_line + spaces
201+ line_to_be_joined [::2 ] = tc_line
202+ line_to_be_joined [1 ::2 ] = spaces
203+ result = "" .join (line_to_be_joined )
192204
193205 result = SUBPHRASE .sub (lambda m : '%s%s' % (
194206 m .group (1 ),
0 commit comments