î ½ê[ Jã@sÕdZddlZddddddgZd ZGd d„dƒZd d d„Zd d d„Zdd„Zejdej ƒZ ejdej ƒZ dd„Z ddd„Z edkrÑee dƒƒndS)zText wrapping and filling. éNÚ TextWrapperÚwrapÚfillÚdedentÚindentÚshortenz c@s eZdZdZiZedƒZxeD]Zeeeeƒ string Munge whitespace in text: expand tabs and convert all other whitespace characters to spaces. Eg. " foo bar baz" becomes " foo bar baz". )rÚ expandtabsrrÚ translateÚunicode_whitespace_trans)rÚtextrrrÚ_munge_whitespace„s   zTextWrapper._munge_whitespacecCsM|jdkr$|jj|ƒ}n|jj|ƒ}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)Ú.0Úcrrrú ¥s z&TextWrapper._split..)rÚ wordsep_reÚsplitÚwordsep_simple_re)rrÚchunksrrrÚ_split’s zTextWrapper._splitcCs|d}|jj}xc|t|ƒdkrw||ddkrj|||ƒrjd||d<|d7}q|d7}qWdS)af_fix_sentence_endings(chunks : [string]) Correct for sentence endings buried in 'chunks'. Eg. when the original text contains "... foo. Bar ...", 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. rérz éN)Úsentence_end_reÚsearchÚlen)rr'ÚiZ patsearchrrrÚ_fix_sentence_endings¨s  $ z!TextWrapper._fix_sentence_endingscCs~|dkrd}n ||}|jr^|j|dd|…ƒ|d|d…|d [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 widthr r0r0r0r0r0r0r0r0r0r0r0r0)rÚ ValueErrorr rrr-r ÚlstripÚreverserÚstripr1r2r5ÚsumÚmapÚjoinÚrstrip) rr'Úlinesrr3r4rÚlZ prev_linerrrÚ _wrap_chunksÛsp   (    %   %     zTextWrapper._wrap_chunkscCs|j|ƒ}|j|ƒS)N)r r()rrrrrÚ _split_chunksBszTextWrapper._split_chunkscCs5|j|ƒ}|jr(|j|ƒn|j|ƒ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. )rArr/r@)rrr'rrrrHs  zTextWrapper.wrapcCsdj|j|ƒƒS)zÞfill(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. Ú )r<r)rrrrrrVszTextWrapper.fill)Ú__name__Ú __module__Ú __qualname__Ú__doc__rÚordZuspaceÚ _whitespaceÚxÚreÚcompiler$r&r+rr r(r/r5r@rArrrrrrrs: /       ! g  r cKstd||}|j|ƒS)aÈWrap 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. r)rr)rrÚkwargsÚwrrrrbs cKstd||}|j|ƒS)a–Fill 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. r)rr)rrrLrMrrrros cKs:td|dd|}|jdj|jƒjƒƒƒS)a«Collapse 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 [...]' rr r)r)rrr<r9r%)rrrLrMrrrr{s z^[ ]+$z(^[ ]*)(?:[^ ])cCsùd}tjd|ƒ}tj|ƒ}xT|D]L}|dkrI|}q.|j|ƒr[q.|j|ƒrs|}q.d}Pq.WdrÓ|rÓxF|jdƒD]2}| sš|j|ƒsštd||fƒ‚qšWn|rõtjd|d|ƒ}n|S)a9Remove 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 " hello" are considered to have no common leading whitespace. (This behaviour is new in Python 2.5; older versions of this module incorrectly expanded tabs before searching for common leading whitespace.) Nr rrBzline = %r, margin = %rz(?m)^)Ú_whitespace_only_reÚsubÚ_leading_whitespace_reÚfindallÚ startswithr%ÚAssertionErrorrJ)rZmarginÚindentsrÚlinerrrrs&     csCˆdkrdd„‰n‡‡‡fdd†}dj|ƒƒ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 |jƒS)N)r9)rUrrrÚ predicateÊszindent..predicatec3s9x2ˆjdƒD]!}ˆ|ƒr,ˆ|n|VqWdS)NT)Ú splitlines)rU)rVÚprefixrrrÚprefixed_linesÍszindent..prefixed_linesr )r<)rrXrVrYr)rVrXrrrÁs Ú__main__z Hello there. This is indented.)rFrJÚ__all__rHrrrrrKÚ MULTILINErNrPrrrCÚprintrrrrÚs  ÿM   1