expansion.}
\end{memberdesc}
+\begin{memberdesc}{drop_whitespace}
+(default: \code{True}) If true, whitespace that, after wrapping, happens
+to end up at the beginning or end of a line is dropped (leading whitespace
+in the first line is always preserved, though).
+\versionadded[Whitespace was always dropped in earlier versions]{2.6}
+\end{memberdesc}
+
\begin{memberdesc}{initial_indent}
(default: \code{''}) String that will be prepended to the first line
of wrapped output. Counts towards the length of the first line.
self.check_wrap(text, 30,
[" This is a sentence with", "leading whitespace."])
+ def test_no_drop_whitespace(self):
+ # SF patch #1581073
+ text = " This is a sentence with much whitespace."
+ self.check_wrap(text, 10,
+ [" This is a", " ", "sentence ",
+ "with ", "much white", "space."],
+ drop_whitespace=False)
+
if test_support.have_unicode:
def test_unicode(self):
# *Very* simple test of wrapping Unicode strings. I'm sure
break_long_words (default: true)
Break words longer than 'width'. If false, those words will not
be broken, and some lines might be longer than 'width'.
+ drop_whitespace (default: true)
+ Drop leading and trailing whitespace from lines.
"""
whitespace_trans = string.maketrans(_whitespace, ' ' * len(_whitespace))
expand_tabs=True,
replace_whitespace=True,
fix_sentence_endings=False,
- break_long_words=True):
+ break_long_words=True,
+ drop_whitespace=True):
self.width = width
self.initial_indent = initial_indent
self.subsequent_indent = subsequent_indent
self.replace_whitespace = replace_whitespace
self.fix_sentence_endings = fix_sentence_endings
self.break_long_words = break_long_words
+ self.drop_whitespace = drop_whitespace
# -- Private methods -----------------------------------------------
'use', ' ', 'the', ' ', '-b', ' ', 'option!'
"""
chunks = self.wordsep_re.split(text)
- chunks = filter(None, chunks)
+ chunks = filter(None, chunks) # remove empty chunks
return chunks
def _fix_sentence_endings(self, chunks):
# First chunk on line is whitespace -- drop it, unless this
# is the very beginning of the text (ie. no lines started yet).
- if chunks[-1].strip() == '' and lines:
+ if self.drop_whitespace and chunks[-1].strip() == '' and lines:
del chunks[-1]
while chunks:
self._handle_long_word(chunks, cur_line, cur_len, width)
# If the last chunk on this line is all whitespace, drop it.
- if cur_line and cur_line[-1].strip() == '':
+ if self.drop_whitespace and cur_line and cur_line[-1].strip() == '':
del cur_line[-1]
# Convert current line back to a string and store it in list