�
��c@s�dZdZddkZddkZyeefWn%ej
oddf\ZZnXdddgZd Zdfd
��YZ dd�Z
dd
�Zeidei
�Zeidei
�Zd�Zedjoed�GHndS(sText wrapping and filling.
s8$Id: textwrap.py 46863 2006-06-11 19:42:51Z tim.peters $i�NiitTextWrappertwraptfills
c
Bs�eZdZeiedee��ZhZe d�Z
x!ee e�D]Ze
ee<qJWe
id�Ze
idei�Zdddeeeed�Zd�Zd �Zd
�Zd�Zd�Zd
�Zd�ZRS(s�
Object for wrapping/filling text. The public interface consists of
the wrap() and fill() methods; the other methods are just there for
subclasses to override in order to tweak the default behaviour.
If you want to completely replace the main wrapping algorithm,
you'll probably have to override _wrap_chunks().
Several instance attributes control various aspects of wrapping:
width (default: 70)
the maximum width of wrapped lines (unless break_long_words
is false)
initial_indent (default: "")
string that will be prepended to the first line of wrapped
output. Counts towards the line's width.
subsequent_indent (default: "")
string that will be prepended to all lines save the first
of wrapped output; also counts towards each line's width.
expand_tabs (default: true)
Expand tabs in input text to spaces before further processing.
Each tab will become 1 .. 8 spaces, depending on its position in
its line. If false, each tab is treated as a single character.
replace_whitespace (default: true)
Replace all whitespace characters in the input text by spaces
after tab expansion. Note that if expand_tabs is false and
replace_whitespace is true, every tab will be converted to a
single space!
fix_sentence_endings (default: false)
Ensure that sentence-ending punctuation is always followed
by two spaces. Off by default because the algorithm is
(unavoidably) imperfect.
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'.
t u sL(\s+|[^\s\w]*\w+[a-zA-Z]-(?=\w+[a-zA-Z])|(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))s[%s][\.\!\?][\"\']?iFtcCsC||_||_||_||_||_||_||_dS(N(twidthtinitial_indenttsubsequent_indenttexpand_tabstreplace_whitespacetfix_sentence_endingstbreak_long_words(tselfRRRRR R
R((s/sys/lib/python/textwrap.pyt__init__^s cCsx|io|i�}n|ioPt|t�o|i|i�}qtt|t�o|i|i�}qtn|S(s�_munge_whitespace(text : string) -> string
Munge whitespace in text: expand tabs and convert all other
whitespace characters to spaces. Eg. " foo bar
baz"
becomes " foo bar baz".
( Rt
expandtabsR t
isinstancetstrt translatetwhitespace_transtunicodetunicode_whitespace_trans(Rttext((s/sys/lib/python/textwrap.pyt_munge_whitespacers
cCs%|ii|�}td|�}|S(s�_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!'
N(t
wordsep_retsplittfiltertNone(RRtchunks((s/sys/lib/python/textwrap.pyt_split�scCs�d}|i}xk|t|�djoS||ddjo0|i||�od||d<|d7}q|d7}qWdS(sf_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.
iiRs iN(tsentence_end_retlentsearch(RRtitpat((s/sys/lib/python/textwrap.pyt_fix_sentence_endings�s )cCsjt||d�}|io+|i|d| �|d||d<n|p|i|i��ndS(s
_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.
ii�N(tmaxRtappendtpop(Rtreversed_chunkstcur_linetcur_lenRt
space_left((s/sys/lib/python/textwrap.pyt_handle_long_word�s
cCs�g}|idjotd|i��n|i�xI|oAg}d}|o
|i}n
|i}|it|�}|di�djo|o|d=nxO|oGt|d�}|||jo!|i|i��||7}q�Pq�W|o1t|d�|jo|i ||||�n|o"|di�djo|d=n|o|i|di
|��q:q:W|S(s�_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.
isinvalid width %r (must be > 0)i�R(Rt
ValueErrortreverseRRRtstripR$R%R*tjoin(RRtlinesR'R(tindentRtl((s/sys/lib/python/textwrap.pyt_wrap_chunks�s4
#cCsF|i|�}|i|�}|io|i|�n|i|�S(s^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.
(RRR
R"R2(RRR((s/sys/lib/python/textwrap.pyR s
cCsdi|i|��S(s�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.
s
(R.R(RR((s/sys/lib/python/textwrap.pyRs(t__name__t
__module__t__doc__tstringt maketranst_whitespaceRRRtordtuspacetmaptxtretcompileRt lowercaseRtTruetFalseR
RRR"R*R2RR(((s/sys/lib/python/textwrap.pyR s2"
I iFcKstd||�}|i|�S(s�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(RRtkwargstw((s/sys/lib/python/textwrap.pyR$s
cKstd||�}|i|�S(s�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(RRRBRC((s/sys/lib/python/textwrap.pyR1s s^[ ]+$s(^[ ]*)(?:[^
])cCsd}tid|�}ti|�}xZ|D]R}|djo
|}q.|i|�oq.|i|�o
|}q.d}Pq.WdoQ|oJxG|id�D]2}|p$|i|�ptd||f�q�Wn|otid|d|�}n|S(s9Remove 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.)
Ris
sline = %r, margin = %rs(?m)^N( Rt_whitespace_only_retsubt_leading_whitespace_retfindallt
startswithRtAssertionErrorR=(RtmargintindentsR0tline((s/sys/lib/python/textwrap.pytdedentCs*
t__main__s Hello there.
This is indented.(R5t__revision__R6R=R@RAt NameErrort__all__R8RRRR>t MULTILINERDRFRMR3(((s/sys/lib/python/textwrap.pys<module>s"
�
0
|