o ?Og string Munge whitespace in text: expand tabs and convert all other whitespace characters to spaces. Eg. " foo\tbar\n\nbaz" becomes " foo bar baz". )r expandtabsrr translateunicode_whitespace_transrtextrrr_munge_whitespaces   zTextWrapper._munge_whitespacecCs6|jdur |j|}n|j|}dd|D}|S)aN_split(text : string) -> [string] Split the text to wrap into indivisible chunks. Chunks are not quite the same as words; see _wrap_chunks() for full details. As an example, the text Look, goof-ball -- use the -b option! breaks into the following chunks: 'Look,', ' ', 'goof-', 'ball', ' ', '--', ' ', 'use', ' ', 'the', ' ', '-b', ' ', 'option!' if break_on_hyphens is True, or in: 'Look,', ' ', 'goof-ball', ' ', '--', ' ', 'use', ' ', 'the', ' ', '-b', ' ', option!' otherwise. TcSsg|]}|r|qSrr.0crrr sz&TextWrapper._split..)r wordsep_resplitwordsep_simple_rerr#chunksrrr_splits  zTextWrapper._splitcCsnd}|jj}|t|dkr5||ddkr'|||r'd||d<|d7}n|d7}|t|dksdSdS)ag_fix_sentence_endings(chunks : [string]) Correct for sentence endings buried in 'chunks'. Eg. when the original text contains "... foo.\nBar ...", munge_whitespace() and split() will convert that to [..., "foo.", " ", "Bar", ...] which has one too few spaces; this method simply changes the one space to two. rr rz N)sentence_end_researchlen)rr-iZ patsearchrrr_fix_sentence_endingss   z!TextWrapper._fix_sentence_endingsc Cs|dkrd}n||}|jrL|}|d}|jr9t||kr9|dd|}|dkr9tdd|d|Dr9|d}||d|||d|d<dS|sW||dSdS)a _handle_long_word(chunks : [string], cur_line : [string], cur_len : int, width : int) Handle a chunk of text (most likely a word, not whitespace) that is too long to fit in any line. r -rcss|]}|dkVqdS)r6Nrr%rrr sz0TextWrapper._handle_long_word..N)rrr2rfindanyappendpop) rZreversed_chunkscur_linecur_lenrZ space_leftendchunkZhyphenrrr_handle_long_words "zTextWrapper._handle_long_wordc Csg}|jdkrtd|j|jdur1|jdkr|j}n|j}t|t|j|jkr1td||r?g}d}|rB|j}n|j}|jt|}|j r\|d dkr\|r\|d=|ryt|d}|||krv| | ||7}nn|s^|rt|d|kr| ||||ttt|}|j r|r|d dkr|t|d8}|d=|r=|jdust|d|jks|r|j rt|dkr|d s||kr| |d|nb|r|d r|t|j|kr| |j| |d| |S|t|d8}|d=|s|r0|d}t|t|j|jkr0||j|d< |S| ||j |S|s8|S)a_wrap_chunks(chunks : [string]) -> [string] Wrap a sequence of text chunks and return a list of lines of length 'self.width' or less. (If 'break_long_words' is false, some lines may be longer than this.) Chunks correspond roughly to words and the whitespace between them: each chunk is indivisible (modulo 'break_long_words'), but a line break can come between any two chunks. Chunks should not have internal whitespace; ie. a chunk is either all whitespace or a "word". Whitespace chunks will be removed from the beginning and end of lines, but apart from that whitespace is preserved. rzinvalid width %r (must be > 0)Nr z#placeholder too large for max widthr5r )r ValueErrorrrrr2rlstripreverserstripr:r;r@summapjoinrstrip) rr-linesrr<r=rlZ prev_linerrr _wrap_chunkss            IzTextWrapper._wrap_chunkscCs||}||Sr)r$r.r"rrr _split_chunksXs  zTextWrapper._split_chunkscCs$||}|jr ||||S)a^wrap(text : string) -> [string] Reformat the single paragraph in 'text' so it fits in lines of no more than 'self.width' columns, and return a list of wrapped lines. Tabs in 'text' are expanded with string.expandtabs(), and all other whitespace characters (including newline) are converted to space. )rLrr4rKr,rrrr^s   zTextWrapper.wrapcCsd||S)zfill(text : string) -> string Reformat the single paragraph in 'text' to fit in lines of no more than 'self.width' columns, and return a new string containing the entire wrapped paragraph.  )rGrr"rrrrlszTextWrapper.fill) r r r TTFTTTr )__name__ __module__ __qualname____doc__r!ordZuspace _whitespacexZ word_punctZletterreescapeZ whitespaceZ nowhitespacecompileVERBOSEr)r+r0rr$r.r4r@rKrLrrrrrrrsV0   )g rr cKtdd|i|}||S)aWrap a single paragraph of text, returning a list of wrapped lines. Reformat the single paragraph in 'text' so it fits in lines of no more than 'width' columns, and return a list of wrapped lines. By default, tabs in 'text' are expanded with string.expandtabs(), and all other whitespace characters (including newline) are converted to space. See TextWrapper class for available keyword args to customize wrapping behaviour. rNr)rrr#rkwargswrrrrxs rcKrY)aFill a single paragraph of text, returning a new string. Reformat the single paragraph in 'text' to fit in lines of no more than 'width' columns, and return a new string containing the entire wrapped paragraph. As with wrap(), tabs are expanded and other whitespace characters converted to space. See TextWrapper class for available keyword args to customize wrapping behaviour. rNr)rrrZrrrrs rcKs,td|dd|}|d|S)aCollapse and truncate the given text to fit in the given width. The text first has its whitespace collapsed. If it then fits in the *width*, it is returned as is. Otherwise, as many words as possible are joined and then the placeholder is appended:: >>> textwrap.shorten("Hello world!", width=12) 'Hello world!' >>> textwrap.shorten("Hello world!", width=11) 'Hello [...]' r )rrrNr)rrrGrDr*rZrrrrs rz^[ ]+$z(^[ ]*)(?:[^ ])cCsd}td|}t|}|D]1}|dur|}q||rq||r&|}qtt||D]\}\}}||kr?|d|}nq-q |rMtd|d|}|S)aRemove any common leading whitespace from every line in `text`. This can be used to make triple-quoted strings line up with the left edge of the display, while still presenting them in the source code in indented form. Note that tabs and spaces are both treated as whitespace, but they are not equal: the lines " hello" and "\thello" are considered to have no common leading whitespace. Entirely blank lines are normalized to a newline character. Nr rrMzline = %r, margin = %rz(?m)^) _whitespace_only_resub_leading_whitespace_refindall startswith enumeratezipr*rU)r#Zmarginindentsrr3rTylinerrrrs(     rcs,durddfdd}d|S)aFAdds 'prefix' to the beginning of selected lines in 'text'. If 'predicate' is provided, 'prefix' will only be added to the lines where 'predicate(line)' is True. If 'predicate' is not provided, it will default to adding 'prefix' to all non-empty lines that do not consist solely of whitespace characters. NcSs|Sr)rDrfrrr predicateszindent..predicatec3s,dD] }|r|n|VqdS)NT) splitlinesrgrhprefixr#rrprefixed_linesszindent..prefixed_linesr )rG)r#rkrhrlrrjrrs r__main__z Hello there. This is indented.)r r)rQrU__all__rSrrrrrW MULTILINEr]r_rrrNprintrrrrs" i   3