Mercurial > cpython-lru_cache-weakref
annotate Lib/textwrap.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | d7aff4423172 |
| children |
| rev | line source |
|---|---|
|
24111
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
1 """Text wrapping and filling. |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
2 """ |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
3 |
|
25757
0838c4247917
Fix copyright date: the core of this code actually dates back to 1999
Greg Ward <gward@python.net>
parents:
25534
diff
changeset
|
4 # Copyright (C) 1999-2001 Gregory P. Ward. |
| 29474 | 5 # Copyright (C) 2002, 2003 Python Software Foundation. |
|
23743
2e1def417ecb
Record copyright and author.
Greg Ward <gward@python.net>
parents:
23742
diff
changeset
|
6 # Written by Greg Ward <gward@python.net> |
|
2e1def417ecb
Record copyright and author.
Greg Ward <gward@python.net>
parents:
23742
diff
changeset
|
7 |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
8 __revision__ = "$Id$" |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
9 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
10 import string, re |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
11 |
|
77072
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
56076
diff
changeset
|
12 try: |
|
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
56076
diff
changeset
|
13 _unicode = unicode |
|
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
56076
diff
changeset
|
14 except NameError: |
|
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
56076
diff
changeset
|
15 # If Python is built without Unicode support, the unicode type |
|
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
56076
diff
changeset
|
16 # will not exist. Fake one. |
|
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
56076
diff
changeset
|
17 class _unicode(object): |
|
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
56076
diff
changeset
|
18 pass |
|
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
56076
diff
changeset
|
19 |
| 29474 | 20 # Do the right thing with boolean values for all known Python versions |
| 21 # (so this module can be copied to projects that don't depend on Python | |
|
48119
c2b5a0ab3070
Remove assignment to True/False to silence the SyntaxWarning that is triggered
Brett Cannon <bcannon@gmail.com>
parents:
46874
diff
changeset
|
22 # 2.3, e.g. Optik and Docutils) by uncommenting the block of code below. |
|
c2b5a0ab3070
Remove assignment to True/False to silence the SyntaxWarning that is triggered
Brett Cannon <bcannon@gmail.com>
parents:
46874
diff
changeset
|
23 #try: |
|
c2b5a0ab3070
Remove assignment to True/False to silence the SyntaxWarning that is triggered
Brett Cannon <bcannon@gmail.com>
parents:
46874
diff
changeset
|
24 # True, False |
|
c2b5a0ab3070
Remove assignment to True/False to silence the SyntaxWarning that is triggered
Brett Cannon <bcannon@gmail.com>
parents:
46874
diff
changeset
|
25 #except NameError: |
|
c2b5a0ab3070
Remove assignment to True/False to silence the SyntaxWarning that is triggered
Brett Cannon <bcannon@gmail.com>
parents:
46874
diff
changeset
|
26 # (True, False) = (1, 0) |
| 29474 | 27 |
|
50065
ae98681058b2
Merged revisions 67571,67574-67576,67579-67581,67583,67591,67597,67608,67631 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48119
diff
changeset
|
28 __all__ = ['TextWrapper', 'wrap', 'fill', 'dedent'] |
|
27630
6567f4829e7e
Add __all__ (suggested by Raymond Hettinger).
Greg Ward <gward@python.net>
parents:
26614
diff
changeset
|
29 |
|
26614
2c7bdbb50e76
Hardcode the recognized whitespace characters to the US-ASCII whitespace
Greg Ward <gward@python.net>
parents:
26600
diff
changeset
|
30 # Hardcode the recognized whitespace characters to the US-ASCII |
|
2c7bdbb50e76
Hardcode the recognized whitespace characters to the US-ASCII whitespace
Greg Ward <gward@python.net>
parents:
26600
diff
changeset
|
31 # whitespace characters. The main reason for doing this is that in |
|
2c7bdbb50e76
Hardcode the recognized whitespace characters to the US-ASCII whitespace
Greg Ward <gward@python.net>
parents:
26600
diff
changeset
|
32 # ISO-8859-1, 0xa0 is non-breaking whitespace, so in certain locales |
|
2c7bdbb50e76
Hardcode the recognized whitespace characters to the US-ASCII whitespace
Greg Ward <gward@python.net>
parents:
26600
diff
changeset
|
33 # that character winds up in string.whitespace. Respecting |
|
2c7bdbb50e76
Hardcode the recognized whitespace characters to the US-ASCII whitespace
Greg Ward <gward@python.net>
parents:
26600
diff
changeset
|
34 # string.whitespace in those cases would 1) make textwrap treat 0xa0 the |
|
2c7bdbb50e76
Hardcode the recognized whitespace characters to the US-ASCII whitespace
Greg Ward <gward@python.net>
parents:
26600
diff
changeset
|
35 # same as any other whitespace char, which is clearly wrong (it's a |
|
2c7bdbb50e76
Hardcode the recognized whitespace characters to the US-ASCII whitespace
Greg Ward <gward@python.net>
parents:
26600
diff
changeset
|
36 # *non-breaking* space), 2) possibly cause problems with Unicode, |
|
2c7bdbb50e76
Hardcode the recognized whitespace characters to the US-ASCII whitespace
Greg Ward <gward@python.net>
parents:
26600
diff
changeset
|
37 # since 0xa0 is not in range(128). |
|
27630
6567f4829e7e
Add __all__ (suggested by Raymond Hettinger).
Greg Ward <gward@python.net>
parents:
26614
diff
changeset
|
38 _whitespace = '\t\n\x0b\x0c\r ' |
|
26614
2c7bdbb50e76
Hardcode the recognized whitespace characters to the US-ASCII whitespace
Greg Ward <gward@python.net>
parents:
26600
diff
changeset
|
39 |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
40 class TextWrapper: |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
41 """ |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
42 Object for wrapping/filling text. The public interface consists of |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
43 the wrap() and fill() methods; the other methods are just there for |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
44 subclasses to override in order to tweak the default behaviour. |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
45 If you want to completely replace the main wrapping algorithm, |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
46 you'll probably have to override _wrap_chunks(). |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
47 |
|
23766
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
48 Several instance attributes control various aspects of wrapping: |
|
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
49 width (default: 70) |
|
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
50 the maximum width of wrapped lines (unless break_long_words |
|
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
51 is false) |
|
23770
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
52 initial_indent (default: "") |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
53 string that will be prepended to the first line of wrapped |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
54 output. Counts towards the line's width. |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
55 subsequent_indent (default: "") |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
56 string that will be prepended to all lines save the first |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
57 of wrapped output; also counts towards each line's width. |
|
23738
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
58 expand_tabs (default: true) |
|
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
59 Expand tabs in input text to spaces before further processing. |
|
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
60 Each tab will become 1 .. 8 spaces, depending on its position in |
|
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
61 its line. If false, each tab is treated as a single character. |
|
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
62 replace_whitespace (default: true) |
|
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
63 Replace all whitespace characters in the input text by spaces |
|
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
64 after tab expansion. Note that if expand_tabs is false and |
|
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
65 replace_whitespace is true, every tab will be converted to a |
|
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
66 single space! |
|
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
67 fix_sentence_endings (default: false) |
|
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
68 Ensure that sentence-ending punctuation is always followed |
| 27884 | 69 by two spaces. Off by default because the algorithm is |
|
23738
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
70 (unavoidably) imperfect. |
|
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
71 break_long_words (default: true) |
|
23766
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
72 Break words longer than 'width'. If false, those words will not |
|
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
73 be broken, and some lines might be longer than 'width'. |
|
46874
c7d1a170c94c
#2659: add ``break_on_hyphens`` to TextWrapper.
Georg Brandl <georg@python.org>
parents:
46561
diff
changeset
|
74 break_on_hyphens (default: true) |
|
c7d1a170c94c
#2659: add ``break_on_hyphens`` to TextWrapper.
Georg Brandl <georg@python.org>
parents:
46561
diff
changeset
|
75 Allow breaking hyphenated words. If true, wrapping will occur |
|
c7d1a170c94c
#2659: add ``break_on_hyphens`` to TextWrapper.
Georg Brandl <georg@python.org>
parents:
46561
diff
changeset
|
76 preferably on whitespaces and right after hyphens part of |
|
c7d1a170c94c
#2659: add ``break_on_hyphens`` to TextWrapper.
Georg Brandl <georg@python.org>
parents:
46561
diff
changeset
|
77 compound words. |
|
41158
1d80d84c7792
Patch #1581073: add a flag to textwrap that prevents the dropping of
Georg Brandl <georg@python.org>
parents:
38748
diff
changeset
|
78 drop_whitespace (default: true) |
|
1d80d84c7792
Patch #1581073: add a flag to textwrap that prevents the dropping of
Georg Brandl <georg@python.org>
parents:
38748
diff
changeset
|
79 Drop leading and trailing whitespace from lines. |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
80 """ |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
81 |
|
27630
6567f4829e7e
Add __all__ (suggested by Raymond Hettinger).
Greg Ward <gward@python.net>
parents:
26614
diff
changeset
|
82 whitespace_trans = string.maketrans(_whitespace, ' ' * len(_whitespace)) |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
83 |
|
26579
e8940d4cd8ca
Fix SF bug #622831 (I think): add unicode_whitespace_trans class
Greg Ward <gward@python.net>
parents:
25884
diff
changeset
|
84 unicode_whitespace_trans = {} |
|
26600
75bfc24fd748
Tweak generation of unicode_whitespace_trans -- clearer, more
Greg Ward <gward@python.net>
parents:
26580
diff
changeset
|
85 uspace = ord(u' ') |
|
27630
6567f4829e7e
Add __all__ (suggested by Raymond Hettinger).
Greg Ward <gward@python.net>
parents:
26614
diff
changeset
|
86 for x in map(ord, _whitespace): |
|
26600
75bfc24fd748
Tweak generation of unicode_whitespace_trans -- clearer, more
Greg Ward <gward@python.net>
parents:
26580
diff
changeset
|
87 unicode_whitespace_trans[x] = uspace |
|
26579
e8940d4cd8ca
Fix SF bug #622831 (I think): add unicode_whitespace_trans class
Greg Ward <gward@python.net>
parents:
25884
diff
changeset
|
88 |
|
24263
e6033254df21
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24111
diff
changeset
|
89 # This funky little regex is just the trick for splitting |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
90 # text up into word-wrappable chunks. E.g. |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
91 # "Hello there -- you goof-ball, use the -b option!" |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
92 # splits into |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
93 # Hello/ /there/ /--/ /you/ /goof-/ball,/ /use/ /the/ /-b/ /option! |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
94 # (after stripping out empty strings). |
|
50424
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
95 wordsep_re = re.compile( |
|
34691
0725f0d5a5c7
SF #1149508: ensure textwrap handles hyphenated numbers correctly,
Greg Ward <gward@python.net>
parents:
34311
diff
changeset
|
96 r'(\s+|' # any whitespace |
|
50257
e4675113d10e
Merged revisions 67654,67676-67677,67681,67692,67725,67746,67748,67761,67784-67785,67787-67788,67802,67832,67848-67849,67859,67862-67864,67880,67882,67885,67889-67892,67895 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50065
diff
changeset
|
97 r'[^\s\w]*\w+[^0-9\W]-(?=\w+[^0-9\W])|' # hyphenated words |
|
34691
0725f0d5a5c7
SF #1149508: ensure textwrap handles hyphenated numbers correctly,
Greg Ward <gward@python.net>
parents:
34311
diff
changeset
|
98 r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))') # em-dash |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
99 |
|
46874
c7d1a170c94c
#2659: add ``break_on_hyphens`` to TextWrapper.
Georg Brandl <georg@python.org>
parents:
46561
diff
changeset
|
100 # This less funky little regex just split on recognized spaces. E.g. |
|
c7d1a170c94c
#2659: add ``break_on_hyphens`` to TextWrapper.
Georg Brandl <georg@python.org>
parents:
46561
diff
changeset
|
101 # "Hello there -- you goof-ball, use the -b option!" |
|
c7d1a170c94c
#2659: add ``break_on_hyphens`` to TextWrapper.
Georg Brandl <georg@python.org>
parents:
46561
diff
changeset
|
102 # splits into |
|
c7d1a170c94c
#2659: add ``break_on_hyphens`` to TextWrapper.
Georg Brandl <georg@python.org>
parents:
46561
diff
changeset
|
103 # Hello/ /there/ /--/ /you/ /goof-ball,/ /use/ /the/ /-b/ /option!/ |
|
50424
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
104 wordsep_simple_re = re.compile(r'(\s+)') |
|
46874
c7d1a170c94c
#2659: add ``break_on_hyphens`` to TextWrapper.
Georg Brandl <georg@python.org>
parents:
46561
diff
changeset
|
105 |
|
32210
eb41a489cc2f
SF #965425: fix so hyphenated words surrounded by punctuation are
Greg Ward <gward@python.net>
parents:
32088
diff
changeset
|
106 # XXX this is not locale- or charset-aware -- string.lowercase |
|
eb41a489cc2f
SF #965425: fix so hyphenated words surrounded by punctuation are
Greg Ward <gward@python.net>
parents:
32088
diff
changeset
|
107 # is US-ASCII only (and therefore English-only) |
|
23739
27f48314ba9d
Convert _fix_sentence_endings() to use a regex, and augment it to
Greg Ward <gward@python.net>
parents:
23738
diff
changeset
|
108 sentence_end_re = re.compile(r'[%s]' # lowercase letter |
|
27f48314ba9d
Convert _fix_sentence_endings() to use a regex, and augment it to
Greg Ward <gward@python.net>
parents:
23738
diff
changeset
|
109 r'[\.\!\?]' # sentence-ending punct. |
|
27f48314ba9d
Convert _fix_sentence_endings() to use a regex, and augment it to
Greg Ward <gward@python.net>
parents:
23738
diff
changeset
|
110 r'[\"\']?' # optional end-of-quote |
|
46561
9e05f15f921b
Issue 2635: fix bug in the fix_sentence_endings option to textwrap.fill.
Mark Dickinson <dickinsm@gmail.com>
parents:
44781
diff
changeset
|
111 r'\Z' # end of chunk |
|
23739
27f48314ba9d
Convert _fix_sentence_endings() to use a regex, and augment it to
Greg Ward <gward@python.net>
parents:
23738
diff
changeset
|
112 % string.lowercase) |
|
23738
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
113 |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
114 |
|
32088
52fe93b656cf
SF #847346: merge from release23-maint branch: remove misguided
Greg Ward <gward@python.net>
parents:
30244
diff
changeset
|
115 def __init__(self, |
|
52fe93b656cf
SF #847346: merge from release23-maint branch: remove misguided
Greg Ward <gward@python.net>
parents:
30244
diff
changeset
|
116 width=70, |
|
52fe93b656cf
SF #847346: merge from release23-maint branch: remove misguided
Greg Ward <gward@python.net>
parents:
30244
diff
changeset
|
117 initial_indent="", |
|
52fe93b656cf
SF #847346: merge from release23-maint branch: remove misguided
Greg Ward <gward@python.net>
parents:
30244
diff
changeset
|
118 subsequent_indent="", |
|
52fe93b656cf
SF #847346: merge from release23-maint branch: remove misguided
Greg Ward <gward@python.net>
parents:
30244
diff
changeset
|
119 expand_tabs=True, |
|
52fe93b656cf
SF #847346: merge from release23-maint branch: remove misguided
Greg Ward <gward@python.net>
parents:
30244
diff
changeset
|
120 replace_whitespace=True, |
|
52fe93b656cf
SF #847346: merge from release23-maint branch: remove misguided
Greg Ward <gward@python.net>
parents:
30244
diff
changeset
|
121 fix_sentence_endings=False, |
|
41158
1d80d84c7792
Patch #1581073: add a flag to textwrap that prevents the dropping of
Georg Brandl <georg@python.org>
parents:
38748
diff
changeset
|
122 break_long_words=True, |
|
46874
c7d1a170c94c
#2659: add ``break_on_hyphens`` to TextWrapper.
Georg Brandl <georg@python.org>
parents:
46561
diff
changeset
|
123 drop_whitespace=True, |
|
c7d1a170c94c
#2659: add ``break_on_hyphens`` to TextWrapper.
Georg Brandl <georg@python.org>
parents:
46561
diff
changeset
|
124 break_on_hyphens=True): |
|
23766
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
125 self.width = width |
|
23770
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
126 self.initial_indent = initial_indent |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
127 self.subsequent_indent = subsequent_indent |
|
23744
ea035490f624
Make all of TextWrapper's options keyword args to the constructor.
Greg Ward <gward@python.net>
parents:
23743
diff
changeset
|
128 self.expand_tabs = expand_tabs |
|
ea035490f624
Make all of TextWrapper's options keyword args to the constructor.
Greg Ward <gward@python.net>
parents:
23743
diff
changeset
|
129 self.replace_whitespace = replace_whitespace |
|
ea035490f624
Make all of TextWrapper's options keyword args to the constructor.
Greg Ward <gward@python.net>
parents:
23743
diff
changeset
|
130 self.fix_sentence_endings = fix_sentence_endings |
|
ea035490f624
Make all of TextWrapper's options keyword args to the constructor.
Greg Ward <gward@python.net>
parents:
23743
diff
changeset
|
131 self.break_long_words = break_long_words |
|
41158
1d80d84c7792
Patch #1581073: add a flag to textwrap that prevents the dropping of
Georg Brandl <georg@python.org>
parents:
38748
diff
changeset
|
132 self.drop_whitespace = drop_whitespace |
|
46874
c7d1a170c94c
#2659: add ``break_on_hyphens`` to TextWrapper.
Georg Brandl <georg@python.org>
parents:
46561
diff
changeset
|
133 self.break_on_hyphens = break_on_hyphens |
|
24263
e6033254df21
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24111
diff
changeset
|
134 |
|
50424
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
135 # recompile the regexes for Unicode mode -- done in this clumsy way for |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
136 # backwards compatibility because it's rather common to monkey-patch |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
137 # the TextWrapper class' wordsep_re attribute. |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
138 self.wordsep_re_uni = re.compile(self.wordsep_re.pattern, re.U) |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
139 self.wordsep_simple_re_uni = re.compile( |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
140 self.wordsep_simple_re.pattern, re.U) |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
141 |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
142 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
143 # -- Private methods ----------------------------------------------- |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
144 # (possibly useful for subclasses to override) |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
145 |
|
23740
a2136ae9d4f7
Conform to the bloody coding standards: "def foo()" not "def foo ()".
Greg Ward <gward@python.net>
parents:
23739
diff
changeset
|
146 def _munge_whitespace(self, text): |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
147 """_munge_whitespace(text : string) -> string |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
148 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
149 Munge whitespace in text: expand tabs and convert all other |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
150 whitespace characters to spaces. Eg. " foo\tbar\n\nbaz" |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
151 becomes " foo bar baz". |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
152 """ |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
153 if self.expand_tabs: |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
154 text = text.expandtabs() |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
155 if self.replace_whitespace: |
|
26579
e8940d4cd8ca
Fix SF bug #622831 (I think): add unicode_whitespace_trans class
Greg Ward <gward@python.net>
parents:
25884
diff
changeset
|
156 if isinstance(text, str): |
|
e8940d4cd8ca
Fix SF bug #622831 (I think): add unicode_whitespace_trans class
Greg Ward <gward@python.net>
parents:
25884
diff
changeset
|
157 text = text.translate(self.whitespace_trans) |
|
77072
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
56076
diff
changeset
|
158 elif isinstance(text, _unicode): |
|
26579
e8940d4cd8ca
Fix SF bug #622831 (I think): add unicode_whitespace_trans class
Greg Ward <gward@python.net>
parents:
25884
diff
changeset
|
159 text = text.translate(self.unicode_whitespace_trans) |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
160 return text |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
161 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
162 |
|
23740
a2136ae9d4f7
Conform to the bloody coding standards: "def foo()" not "def foo ()".
Greg Ward <gward@python.net>
parents:
23739
diff
changeset
|
163 def _split(self, text): |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
164 """_split(text : string) -> [string] |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
165 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
166 Split the text to wrap into indivisible chunks. Chunks are |
|
56076
1d2e6c441a53
Optimize optimization and fix method name in docstring.
Georg Brandl <georg@python.org>
parents:
50424
diff
changeset
|
167 not quite the same as words; see _wrap_chunks() for full |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
168 details. As an example, the text |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
169 Look, goof-ball -- use the -b option! |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
170 breaks into the following chunks: |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
171 'Look,', ' ', 'goof-', 'ball', ' ', '--', ' ', |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
172 'use', ' ', 'the', ' ', '-b', ' ', 'option!' |
|
46874
c7d1a170c94c
#2659: add ``break_on_hyphens`` to TextWrapper.
Georg Brandl <georg@python.org>
parents:
46561
diff
changeset
|
173 if break_on_hyphens is True, or in: |
|
c7d1a170c94c
#2659: add ``break_on_hyphens`` to TextWrapper.
Georg Brandl <georg@python.org>
parents:
46561
diff
changeset
|
174 'Look,', ' ', 'goof-ball', ' ', '--', ' ', |
|
c7d1a170c94c
#2659: add ``break_on_hyphens`` to TextWrapper.
Georg Brandl <georg@python.org>
parents:
46561
diff
changeset
|
175 'use', ' ', 'the', ' ', '-b', ' ', option!' |
|
c7d1a170c94c
#2659: add ``break_on_hyphens`` to TextWrapper.
Georg Brandl <georg@python.org>
parents:
46561
diff
changeset
|
176 otherwise. |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
177 """ |
|
77072
d7aff4423172
Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents:
56076
diff
changeset
|
178 if isinstance(text, _unicode): |
|
50424
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
179 if self.break_on_hyphens: |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
180 pat = self.wordsep_re_uni |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
181 else: |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
182 pat = self.wordsep_simple_re_uni |
|
46874
c7d1a170c94c
#2659: add ``break_on_hyphens`` to TextWrapper.
Georg Brandl <georg@python.org>
parents:
46561
diff
changeset
|
183 else: |
|
50424
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
184 if self.break_on_hyphens: |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
185 pat = self.wordsep_re |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
186 else: |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
187 pat = self.wordsep_simple_re |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
50257
diff
changeset
|
188 chunks = pat.split(text) |
|
41158
1d80d84c7792
Patch #1581073: add a flag to textwrap that prevents the dropping of
Georg Brandl <georg@python.org>
parents:
38748
diff
changeset
|
189 chunks = filter(None, chunks) # remove empty chunks |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
190 return chunks |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
191 |
|
23740
a2136ae9d4f7
Conform to the bloody coding standards: "def foo()" not "def foo ()".
Greg Ward <gward@python.net>
parents:
23739
diff
changeset
|
192 def _fix_sentence_endings(self, chunks): |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
193 """_fix_sentence_endings(chunks : [string]) |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
194 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
195 Correct for sentence endings buried in 'chunks'. Eg. when the |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
196 original text contains "... foo.\nBar ...", munge_whitespace() |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
197 and split() will convert that to [..., "foo.", " ", "Bar", ...] |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
198 which has one too few spaces; this method simply changes the one |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
199 space to two. |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
200 """ |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
201 i = 0 |
|
56076
1d2e6c441a53
Optimize optimization and fix method name in docstring.
Georg Brandl <georg@python.org>
parents:
50424
diff
changeset
|
202 patsearch = self.sentence_end_re.search |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
203 while i < len(chunks)-1: |
|
56076
1d2e6c441a53
Optimize optimization and fix method name in docstring.
Georg Brandl <georg@python.org>
parents:
50424
diff
changeset
|
204 if chunks[i+1] == " " and patsearch(chunks[i]): |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
205 chunks[i+1] = " " |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
206 i += 2 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
207 else: |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
208 i += 1 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
209 |
|
35235
f9c5ae69c16e
textwrap now processes text chucks at O(n) speed instead of O(n**2).
Raymond Hettinger <python@rcn.com>
parents:
34691
diff
changeset
|
210 def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width): |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
211 """_handle_long_word(chunks : [string], |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
212 cur_line : [string], |
|
23770
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
213 cur_len : int, width : int) |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
214 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
215 Handle a chunk of text (most likely a word, not whitespace) that |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
216 is too long to fit in any line. |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
217 """ |
|
44781
17009d6211b8
Fix #1146: TextWrap vs words 1-character shorter than the width.
Georg Brandl <georg@python.org>
parents:
41158
diff
changeset
|
218 # Figure out when indent is larger than the specified width, and make |
|
17009d6211b8
Fix #1146: TextWrap vs words 1-character shorter than the width.
Georg Brandl <georg@python.org>
parents:
41158
diff
changeset
|
219 # sure at least one character is stripped off on every pass |
|
17009d6211b8
Fix #1146: TextWrap vs words 1-character shorter than the width.
Georg Brandl <georg@python.org>
parents:
41158
diff
changeset
|
220 if width < 1: |
|
17009d6211b8
Fix #1146: TextWrap vs words 1-character shorter than the width.
Georg Brandl <georg@python.org>
parents:
41158
diff
changeset
|
221 space_left = 1 |
|
17009d6211b8
Fix #1146: TextWrap vs words 1-character shorter than the width.
Georg Brandl <georg@python.org>
parents:
41158
diff
changeset
|
222 else: |
|
17009d6211b8
Fix #1146: TextWrap vs words 1-character shorter than the width.
Georg Brandl <georg@python.org>
parents:
41158
diff
changeset
|
223 space_left = width - cur_len |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
224 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
225 # If we're allowed to break long words, then do so: put as much |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
226 # of the next chunk onto the current line as will fit. |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
227 if self.break_long_words: |
|
35235
f9c5ae69c16e
textwrap now processes text chucks at O(n) speed instead of O(n**2).
Raymond Hettinger <python@rcn.com>
parents:
34691
diff
changeset
|
228 cur_line.append(reversed_chunks[-1][:space_left]) |
|
f9c5ae69c16e
textwrap now processes text chucks at O(n) speed instead of O(n**2).
Raymond Hettinger <python@rcn.com>
parents:
34691
diff
changeset
|
229 reversed_chunks[-1] = reversed_chunks[-1][space_left:] |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
230 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
231 # Otherwise, we have to preserve the long word intact. Only add |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
232 # it to the current line if there's nothing already there -- |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
233 # that minimizes how much we violate the width constraint. |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
234 elif not cur_line: |
|
35235
f9c5ae69c16e
textwrap now processes text chucks at O(n) speed instead of O(n**2).
Raymond Hettinger <python@rcn.com>
parents:
34691
diff
changeset
|
235 cur_line.append(reversed_chunks.pop()) |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
236 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
237 # If we're not allowed to break long words, and there's already |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
238 # text on the current line, do nothing. Next time through the |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
239 # main loop of _wrap_chunks(), we'll wind up here again, but |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
240 # cur_len will be zero, so the next line will be entirely |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
241 # devoted to the long word that we can't handle right now. |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
242 |
|
23766
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
243 def _wrap_chunks(self, chunks): |
|
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
244 """_wrap_chunks(chunks : [string]) -> [string] |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
245 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
246 Wrap a sequence of text chunks and return a list of lines of |
|
23766
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
247 length 'self.width' or less. (If 'break_long_words' is false, |
|
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
248 some lines may be longer than this.) Chunks correspond roughly |
|
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
249 to words and the whitespace between them: each chunk is |
|
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
250 indivisible (modulo 'break_long_words'), but a line break can |
|
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
251 come between any two chunks. Chunks should not have internal |
|
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
252 whitespace; ie. a chunk is either all whitespace or a "word". |
|
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
253 Whitespace chunks will be removed from the beginning and end of |
|
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
254 lines, but apart from that whitespace is preserved. |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
255 """ |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
256 lines = [] |
|
29005
3055c9bdff5f
SF #726446: raise ValueError if width <= 0.
Greg Ward <gward@python.net>
parents:
27884
diff
changeset
|
257 if self.width <= 0: |
|
3055c9bdff5f
SF #726446: raise ValueError if width <= 0.
Greg Ward <gward@python.net>
parents:
27884
diff
changeset
|
258 raise ValueError("invalid width %r (must be > 0)" % self.width) |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
259 |
|
35235
f9c5ae69c16e
textwrap now processes text chucks at O(n) speed instead of O(n**2).
Raymond Hettinger <python@rcn.com>
parents:
34691
diff
changeset
|
260 # Arrange in reverse order so items can be efficiently popped |
|
f9c5ae69c16e
textwrap now processes text chucks at O(n) speed instead of O(n**2).
Raymond Hettinger <python@rcn.com>
parents:
34691
diff
changeset
|
261 # from a stack of chucks. |
|
f9c5ae69c16e
textwrap now processes text chucks at O(n) speed instead of O(n**2).
Raymond Hettinger <python@rcn.com>
parents:
34691
diff
changeset
|
262 chunks.reverse() |
|
f9c5ae69c16e
textwrap now processes text chucks at O(n) speed instead of O(n**2).
Raymond Hettinger <python@rcn.com>
parents:
34691
diff
changeset
|
263 |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
264 while chunks: |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
265 |
|
23770
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
266 # Start the list of chunks that will make up the current line. |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
267 # cur_len is just the length of all the chunks in cur_line. |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
268 cur_line = [] |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
269 cur_len = 0 |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
270 |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
271 # Figure out which static string will prefix this line. |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
272 if lines: |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
273 indent = self.subsequent_indent |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
274 else: |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
275 indent = self.initial_indent |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
276 |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
277 # Maximum width for this line. |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
278 width = self.width - len(indent) |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
279 |
|
26580
d4b76817c9e6
Fix SF bug #622849: in _wrap_chunks(), ensure that leading whitespace in
Greg Ward <gward@python.net>
parents:
26579
diff
changeset
|
280 # First chunk on line is whitespace -- drop it, unless this |
|
d4b76817c9e6
Fix SF bug #622849: in _wrap_chunks(), ensure that leading whitespace in
Greg Ward <gward@python.net>
parents:
26579
diff
changeset
|
281 # is the very beginning of the text (ie. no lines started yet). |
|
41158
1d80d84c7792
Patch #1581073: add a flag to textwrap that prevents the dropping of
Georg Brandl <georg@python.org>
parents:
38748
diff
changeset
|
282 if self.drop_whitespace and chunks[-1].strip() == '' and lines: |
|
35235
f9c5ae69c16e
textwrap now processes text chucks at O(n) speed instead of O(n**2).
Raymond Hettinger <python@rcn.com>
parents:
34691
diff
changeset
|
283 del chunks[-1] |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
284 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
285 while chunks: |
|
35235
f9c5ae69c16e
textwrap now processes text chucks at O(n) speed instead of O(n**2).
Raymond Hettinger <python@rcn.com>
parents:
34691
diff
changeset
|
286 l = len(chunks[-1]) |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
287 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
288 # Can at least squeeze this chunk onto the current line. |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
289 if cur_len + l <= width: |
|
35235
f9c5ae69c16e
textwrap now processes text chucks at O(n) speed instead of O(n**2).
Raymond Hettinger <python@rcn.com>
parents:
34691
diff
changeset
|
290 cur_line.append(chunks.pop()) |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
291 cur_len += l |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
292 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
293 # Nope, this line is full. |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
294 else: |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
295 break |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
296 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
297 # The current line is full, and the next chunk is too big to |
|
24263
e6033254df21
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24111
diff
changeset
|
298 # fit on *any* line (not just this one). |
|
35235
f9c5ae69c16e
textwrap now processes text chucks at O(n) speed instead of O(n**2).
Raymond Hettinger <python@rcn.com>
parents:
34691
diff
changeset
|
299 if chunks and len(chunks[-1]) > width: |
|
23770
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
300 self._handle_long_word(chunks, cur_line, cur_len, width) |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
301 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
302 # If the last chunk on this line is all whitespace, drop it. |
|
41158
1d80d84c7792
Patch #1581073: add a flag to textwrap that prevents the dropping of
Georg Brandl <georg@python.org>
parents:
38748
diff
changeset
|
303 if self.drop_whitespace and cur_line and cur_line[-1].strip() == '': |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
304 del cur_line[-1] |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
305 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
306 # Convert current line back to a string and store it in list |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
307 # of all lines (return value). |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
308 if cur_line: |
|
23770
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
309 lines.append(indent + ''.join(cur_line)) |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
310 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
311 return lines |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
312 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
313 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
314 # -- Public interface ---------------------------------------------- |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
315 |
|
23766
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
316 def wrap(self, text): |
|
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
317 """wrap(text : string) -> [string] |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
318 |
|
24111
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
319 Reformat the single paragraph in 'text' so it fits in lines of |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
320 no more than 'self.width' columns, and return a list of wrapped |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
321 lines. Tabs in 'text' are expanded with string.expandtabs(), |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
322 and all other whitespace characters (including newline) are |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
323 converted to space. |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
324 """ |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
325 text = self._munge_whitespace(text) |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
326 chunks = self._split(text) |
|
23738
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
327 if self.fix_sentence_endings: |
|
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
328 self._fix_sentence_endings(chunks) |
|
23766
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
329 return self._wrap_chunks(chunks) |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
330 |
|
23770
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
331 def fill(self, text): |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
332 """fill(text : string) -> string |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
333 |
|
24111
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
334 Reformat the single paragraph in 'text' to fit in lines of no |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
335 more than 'self.width' columns, and return a new string |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
336 containing the entire wrapped paragraph. |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
337 """ |
|
23770
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
338 return "\n".join(self.wrap(text)) |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
339 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
340 |
|
24111
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
341 # -- Convenience interface --------------------------------------------- |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
342 |
|
23767
5fb08d832eac
Allow the standalone wrap() and fill() functions to take arbitrary
Greg Ward <gward@python.net>
parents:
23766
diff
changeset
|
343 def wrap(text, width=70, **kwargs): |
|
24111
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
344 """Wrap a single paragraph of text, returning a list of wrapped lines. |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
345 |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
346 Reformat the single paragraph in 'text' so it fits in lines of no |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
347 more than 'width' columns, and return a list of wrapped lines. By |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
348 default, tabs in 'text' are expanded with string.expandtabs(), and |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
349 all other whitespace characters (including newline) are converted to |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
350 space. See TextWrapper class for available keyword args to customize |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
351 wrapping behaviour. |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
352 """ |
|
23767
5fb08d832eac
Allow the standalone wrap() and fill() functions to take arbitrary
Greg Ward <gward@python.net>
parents:
23766
diff
changeset
|
353 w = TextWrapper(width=width, **kwargs) |
|
5fb08d832eac
Allow the standalone wrap() and fill() functions to take arbitrary
Greg Ward <gward@python.net>
parents:
23766
diff
changeset
|
354 return w.wrap(text) |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
355 |
|
23770
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
356 def fill(text, width=70, **kwargs): |
|
24111
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
357 """Fill a single paragraph of text, returning a new string. |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
358 |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
359 Reformat the single paragraph in 'text' to fit in lines of no more |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
360 than 'width' columns, and return a new string containing the entire |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
361 wrapped paragraph. As with wrap(), tabs are expanded and other |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
362 whitespace characters converted to space. See TextWrapper class for |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
363 available keyword args to customize wrapping behaviour. |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
364 """ |
|
23767
5fb08d832eac
Allow the standalone wrap() and fill() functions to take arbitrary
Greg Ward <gward@python.net>
parents:
23766
diff
changeset
|
365 w = TextWrapper(width=width, **kwargs) |
|
23770
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
366 return w.fill(text) |
|
29022
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
367 |
|
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
368 |
|
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
369 # -- Loosely related functionality ------------------------------------- |
|
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
370 |
|
38734
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
371 _whitespace_only_re = re.compile('^[ \t]+$', re.MULTILINE) |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
372 _leading_whitespace_re = re.compile('(^[ \t]*)(?:[^ \t\n])', re.MULTILINE) |
|
29022
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
373 |
|
38734
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
374 def dedent(text): |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
375 """Remove any common leading whitespace from every line in `text`. |
|
29022
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
376 |
|
38734
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
377 This can be used to make triple-quoted strings line up with the left |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
378 edge of the display, while still presenting them in the source code |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
379 in indented form. |
|
29022
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
380 |
|
38734
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
381 Note that tabs and spaces are both treated as whitespace, but they |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
382 are not equal: the lines " hello" and "\thello" are |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
383 considered to have no common leading whitespace. (This behaviour is |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
384 new in Python 2.5; older versions of this module incorrectly |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
385 expanded tabs before searching for common leading whitespace.) |
|
29022
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
386 """ |
|
38734
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
387 # Look for the longest leading string of spaces and tabs common to |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
388 # all lines. |
|
29022
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
389 margin = None |
|
38734
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
390 text = _whitespace_only_re.sub('', text) |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
391 indents = _leading_whitespace_re.findall(text) |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
392 for indent in indents: |
|
29022
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
393 if margin is None: |
|
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
394 margin = indent |
|
38734
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
395 |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
396 # Current line more deeply indented than previous winner: |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
397 # no change (previous winner is still on top). |
|
38748
b50ab288ae27
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
38734
diff
changeset
|
398 elif indent.startswith(margin): |
|
b50ab288ae27
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
38734
diff
changeset
|
399 pass |
|
38734
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
400 |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
401 # Current line consistent with and no deeper than previous winner: |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
402 # it's the new winner. |
|
38748
b50ab288ae27
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
38734
diff
changeset
|
403 elif margin.startswith(indent): |
|
b50ab288ae27
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
38734
diff
changeset
|
404 margin = indent |
|
29022
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
405 |
|
38734
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
406 # Current line and previous winner have no common whitespace: |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
407 # there is no margin. |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
408 else: |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
409 margin = "" |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
410 break |
|
29022
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
411 |
|
38734
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
412 # sanity check (testing/debugging only) |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
413 if 0 and margin: |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
414 for line in text.split("\n"): |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
415 assert not line or line.startswith(margin), \ |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
416 "line = %r, margin = %r" % (line, margin) |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
417 |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
418 if margin: |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
419 text = re.sub(r'(?m)^' + margin, '', text) |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
420 return text |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
421 |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
422 if __name__ == "__main__": |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
423 #print dedent("\tfoo\n\tbar") |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
424 #print dedent(" \thello there\n \t how are you?") |
|
9d8478f732d5
Bug #1361643: fix textwrap.dedent() so it handles tabs appropriately,
Greg Ward <gward@python.net>
parents:
35235
diff
changeset
|
425 print dedent("Hello there.\n This is indented.") |
