Mercurial > cpython-lru_cache-weakref
annotate Lib/textwrap.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 0bd257cd3e88 |
| 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 |
|
71037
8c17e898e0e8
remove unused imports (closes #12432)
Benjamin Peterson <benjamin@python.org>
parents:
56368
diff
changeset
|
8 import re |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
9 |
|
86386
0bd257cd3e88
Add shorten to __all_ (issues #18585 and #18725).
Serhiy Storchaka <storchaka@gmail.com>
parents:
86381
diff
changeset
|
10 __all__ = ['TextWrapper', 'wrap', 'fill', 'dedent', 'indent', 'shorten'] |
|
27630
6567f4829e7e
Add __all__ (suggested by Raymond Hettinger).
Greg Ward <gward@python.net>
parents:
26614
diff
changeset
|
11 |
|
26614
2c7bdbb50e76
Hardcode the recognized whitespace characters to the US-ASCII whitespace
Greg Ward <gward@python.net>
parents:
26600
diff
changeset
|
12 # 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
|
13 # 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
|
14 # 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
|
15 # 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
|
16 # 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
|
17 # 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
|
18 # *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
|
19 # since 0xa0 is not in range(128). |
|
27630
6567f4829e7e
Add __all__ (suggested by Raymond Hettinger).
Greg Ward <gward@python.net>
parents:
26614
diff
changeset
|
20 _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
|
21 |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
22 class TextWrapper: |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
23 """ |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
24 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
|
25 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
|
26 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
|
27 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
|
28 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
|
29 |
|
23766
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
30 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
|
31 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
|
32 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
|
33 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
|
34 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
|
35 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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 Expand tabs in input text to spaces before further processing. |
|
77050
d38e821c1b80
#13152: Allow to specify a custom tabsize for expanding tabs in textwrap
Hynek Schlawack <hs@ox.cx>
parents:
71541
diff
changeset
|
42 Each tab will become 0 .. 'tabsize' spaces, depending on its position |
|
d38e821c1b80
#13152: Allow to specify a custom tabsize for expanding tabs in textwrap
Hynek Schlawack <hs@ox.cx>
parents:
71541
diff
changeset
|
43 in its line. If false, each tab is treated as a single character. |
|
d38e821c1b80
#13152: Allow to specify a custom tabsize for expanding tabs in textwrap
Hynek Schlawack <hs@ox.cx>
parents:
71541
diff
changeset
|
44 tabsize (default: 8) |
|
d38e821c1b80
#13152: Allow to specify a custom tabsize for expanding tabs in textwrap
Hynek Schlawack <hs@ox.cx>
parents:
71541
diff
changeset
|
45 Expand tabs in input text to 0 .. 'tabsize' spaces, unless |
|
d38e821c1b80
#13152: Allow to specify a custom tabsize for expanding tabs in textwrap
Hynek Schlawack <hs@ox.cx>
parents:
71541
diff
changeset
|
46 'expand_tabs' is false. |
|
23738
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
47 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
|
48 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
|
49 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
|
50 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
|
51 single space! |
|
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
52 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
|
53 Ensure that sentence-ending punctuation is always followed |
| 27884 | 54 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
|
55 (unavoidably) imperfect. |
|
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
56 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
|
57 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
|
58 be broken, and some lines might be longer than 'width'. |
|
47067
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
59 break_on_hyphens (default: true) |
|
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
60 Allow breaking hyphenated words. If true, wrapping will occur |
|
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
61 preferably on whitespaces and right after hyphens part of |
|
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
62 compound words. |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
63 drop_whitespace (default: true) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
64 Drop leading and trailing whitespace from lines. |
|
86381
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
65 max_lines (default: None) |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
66 Truncate wrapped lines. |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
67 placeholder (default: ' [...]') |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
68 Append to the last line of truncated text. |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
69 """ |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
70 |
|
26579
e8940d4cd8ca
Fix SF bug #622831 (I think): add unicode_whitespace_trans class
Greg Ward <gward@python.net>
parents:
25884
diff
changeset
|
71 unicode_whitespace_trans = {} |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
72 uspace = ord(' ') |
|
42142
a53e48be6df9
Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41969
diff
changeset
|
73 for x in _whitespace: |
|
a53e48be6df9
Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41969
diff
changeset
|
74 unicode_whitespace_trans[ord(x)] = uspace |
|
26579
e8940d4cd8ca
Fix SF bug #622831 (I think): add unicode_whitespace_trans class
Greg Ward <gward@python.net>
parents:
25884
diff
changeset
|
75 |
|
24263
e6033254df21
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24111
diff
changeset
|
76 # 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
|
77 # 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
|
78 # "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
|
79 # splits into |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
80 # 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
|
81 # (after stripping out empty strings). |
|
34691
0725f0d5a5c7
SF #1149508: ensure textwrap handles hyphenated numbers correctly,
Greg Ward <gward@python.net>
parents:
34311
diff
changeset
|
82 wordsep_re = re.compile( |
|
0725f0d5a5c7
SF #1149508: ensure textwrap handles hyphenated numbers correctly,
Greg Ward <gward@python.net>
parents:
34311
diff
changeset
|
83 r'(\s+|' # any whitespace |
|
50157
740ce9a523ff
Issue #4163: textwrap module: allow word splitting on a hyphen preceded by a non-ASCII letter.
Antoine Pitrou <solipsis@pitrou.net>
parents:
50078
diff
changeset
|
84 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
|
85 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
|
86 |
|
47067
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
87 # This less funky little regex just split on recognized spaces. E.g. |
|
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
88 # "Hello there -- you goof-ball, use the -b option!" |
|
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
89 # splits into |
|
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
90 # Hello/ /there/ /--/ /you/ /goof-ball,/ /use/ /the/ /-b/ /option!/ |
|
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
91 wordsep_simple_re = re.compile(r'(\s+)') |
|
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
92 |
|
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
93 # XXX this is not locale- or charset-aware -- string.lowercase |
|
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
94 # is US-ASCII only (and therefore English-only) |
|
42544
077c68186e23
Quick fix for a new problem here -- using string.lowercase somehow caused
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
95 sentence_end_re = re.compile(r'[a-z]' # lowercase letter |
|
23739
27f48314ba9d
Convert _fix_sentence_endings() to use a regex, and augment it to
Greg Ward <gward@python.net>
parents:
23738
diff
changeset
|
96 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
|
97 r'[\"\']?' # optional end-of-quote |
|
47067
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
98 r'\Z') # end of chunk |
|
23738
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
99 |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
100 |
|
32088
52fe93b656cf
SF #847346: merge from release23-maint branch: remove misguided
Greg Ward <gward@python.net>
parents:
30244
diff
changeset
|
101 def __init__(self, |
|
52fe93b656cf
SF #847346: merge from release23-maint branch: remove misguided
Greg Ward <gward@python.net>
parents:
30244
diff
changeset
|
102 width=70, |
|
52fe93b656cf
SF #847346: merge from release23-maint branch: remove misguided
Greg Ward <gward@python.net>
parents:
30244
diff
changeset
|
103 initial_indent="", |
|
52fe93b656cf
SF #847346: merge from release23-maint branch: remove misguided
Greg Ward <gward@python.net>
parents:
30244
diff
changeset
|
104 subsequent_indent="", |
|
52fe93b656cf
SF #847346: merge from release23-maint branch: remove misguided
Greg Ward <gward@python.net>
parents:
30244
diff
changeset
|
105 expand_tabs=True, |
|
52fe93b656cf
SF #847346: merge from release23-maint branch: remove misguided
Greg Ward <gward@python.net>
parents:
30244
diff
changeset
|
106 replace_whitespace=True, |
|
52fe93b656cf
SF #847346: merge from release23-maint branch: remove misguided
Greg Ward <gward@python.net>
parents:
30244
diff
changeset
|
107 fix_sentence_endings=False, |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
108 break_long_words=True, |
|
47067
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
109 drop_whitespace=True, |
|
77050
d38e821c1b80
#13152: Allow to specify a custom tabsize for expanding tabs in textwrap
Hynek Schlawack <hs@ox.cx>
parents:
71541
diff
changeset
|
110 break_on_hyphens=True, |
|
86381
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
111 tabsize=8, |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
112 *, |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
113 max_lines=None, |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
114 placeholder=' [...]'): |
|
23766
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
115 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
|
116 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
|
117 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
|
118 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
|
119 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
|
120 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
|
121 self.break_long_words = break_long_words |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
122 self.drop_whitespace = drop_whitespace |
|
47067
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
123 self.break_on_hyphens = break_on_hyphens |
|
77050
d38e821c1b80
#13152: Allow to specify a custom tabsize for expanding tabs in textwrap
Hynek Schlawack <hs@ox.cx>
parents:
71541
diff
changeset
|
124 self.tabsize = tabsize |
|
86381
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
125 self.max_lines = max_lines |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
126 self.placeholder = placeholder |
|
24263
e6033254df21
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24111
diff
changeset
|
127 |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
128 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
129 # -- Private methods ----------------------------------------------- |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
130 # (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
|
131 |
|
23740
a2136ae9d4f7
Conform to the bloody coding standards: "def foo()" not "def foo ()".
Greg Ward <gward@python.net>
parents:
23739
diff
changeset
|
132 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
|
133 """_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
|
134 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
135 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
|
136 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
|
137 becomes " foo bar baz". |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
138 """ |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
139 if self.expand_tabs: |
|
77050
d38e821c1b80
#13152: Allow to specify a custom tabsize for expanding tabs in textwrap
Hynek Schlawack <hs@ox.cx>
parents:
71541
diff
changeset
|
140 text = text.expandtabs(self.tabsize) |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
141 if self.replace_whitespace: |
|
43144
5c30f15a6b87
string.maketrans() now produces translation tables for bytes.translate() -- wrong module?
Georg Brandl <georg@python.org>
parents:
42544
diff
changeset
|
142 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
|
143 return text |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
144 |
|
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 _split(self, text): |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
147 """_split(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 Split the text to wrap into indivisible chunks. Chunks are |
|
56368
c9bd62efe6ce
Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50157
diff
changeset
|
150 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
|
151 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
|
152 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
|
153 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
|
154 'Look,', ' ', 'goof-', 'ball', ' ', '--', ' ', |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
155 'use', ' ', 'the', ' ', '-b', ' ', 'option!' |
|
47067
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
156 if break_on_hyphens is True, or in: |
|
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
157 'Look,', ' ', 'goof-ball', ' ', '--', ' ', |
|
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
158 'use', ' ', 'the', ' ', '-b', ' ', option!' |
|
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
159 otherwise. |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
160 """ |
|
47067
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
161 if self.break_on_hyphens is True: |
|
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
162 chunks = self.wordsep_re.split(text) |
|
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
163 else: |
|
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46664
diff
changeset
|
164 chunks = self.wordsep_simple_re.split(text) |
|
42142
a53e48be6df9
Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41969
diff
changeset
|
165 chunks = [c for c in chunks if c] |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
166 return chunks |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
167 |
|
23740
a2136ae9d4f7
Conform to the bloody coding standards: "def foo()" not "def foo ()".
Greg Ward <gward@python.net>
parents:
23739
diff
changeset
|
168 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
|
169 """_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
|
170 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
171 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
|
172 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
|
173 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
|
174 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
|
175 space to two. |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
176 """ |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
177 i = 0 |
|
56368
c9bd62efe6ce
Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50157
diff
changeset
|
178 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
|
179 while i < len(chunks)-1: |
|
56368
c9bd62efe6ce
Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50157
diff
changeset
|
180 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
|
181 chunks[i+1] = " " |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
182 i += 2 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
183 else: |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
184 i += 1 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
185 |
|
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
|
186 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
|
187 """_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
|
188 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
|
189 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
|
190 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
191 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
|
192 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
|
193 """ |
|
44782
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
43144
diff
changeset
|
194 # Figure out when indent is larger than the specified width, and make |
|
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
43144
diff
changeset
|
195 # sure at least one character is stripped off on every pass |
|
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
43144
diff
changeset
|
196 if width < 1: |
|
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
43144
diff
changeset
|
197 space_left = 1 |
|
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
43144
diff
changeset
|
198 else: |
|
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
43144
diff
changeset
|
199 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
|
200 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
201 # 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
|
202 # 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
|
203 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
|
204 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
|
205 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
|
206 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
207 # 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
|
208 # 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
|
209 # 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
|
210 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
|
211 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
|
212 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
213 # 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
|
214 # 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
|
215 # 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
|
216 # 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
|
217 # 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
|
218 |
|
23766
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
219 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
|
220 """_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
|
221 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
222 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
|
223 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
|
224 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
|
225 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
|
226 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
|
227 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
|
228 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
|
229 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
|
230 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
|
231 """ |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
232 lines = [] |
|
29005
3055c9bdff5f
SF #726446: raise ValueError if width <= 0.
Greg Ward <gward@python.net>
parents:
27884
diff
changeset
|
233 if self.width <= 0: |
|
3055c9bdff5f
SF #726446: raise ValueError if width <= 0.
Greg Ward <gward@python.net>
parents:
27884
diff
changeset
|
234 raise ValueError("invalid width %r (must be > 0)" % self.width) |
|
86381
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
235 if self.max_lines is not None: |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
236 if self.max_lines > 1: |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
237 indent = self.subsequent_indent |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
238 else: |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
239 indent = self.initial_indent |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
240 if len(indent) + len(self.placeholder.lstrip()) > self.width: |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
241 raise ValueError("placeholder too large for max width") |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
242 |
|
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
|
243 # 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
|
244 # 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
|
245 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
|
246 |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
247 while chunks: |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
248 |
|
23770
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
249 # 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
|
250 # 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
|
251 cur_line = [] |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
252 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
|
253 |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
254 # 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
|
255 if lines: |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
256 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
|
257 else: |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
258 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
|
259 |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
260 # 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
|
261 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
|
262 |
|
26580
d4b76817c9e6
Fix SF bug #622849: in _wrap_chunks(), ensure that leading whitespace in
Greg Ward <gward@python.net>
parents:
26579
diff
changeset
|
263 # 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
|
264 # is the very beginning of the text (ie. no lines started yet). |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
265 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
|
266 del chunks[-1] |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
267 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
268 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
|
269 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
|
270 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
271 # 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
|
272 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
|
273 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
|
274 cur_len += l |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
275 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
276 # 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
|
277 else: |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
278 break |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
279 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
280 # 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
|
281 # 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
|
282 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
|
283 self._handle_long_word(chunks, cur_line, cur_len, width) |
|
86381
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
284 cur_len = sum(map(len, cur_line)) |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
285 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
286 # If the last chunk on this line is all whitespace, drop it. |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
287 if self.drop_whitespace and cur_line and cur_line[-1].strip() == '': |
|
86381
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
288 cur_len -= len(cur_line[-1]) |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
289 del cur_line[-1] |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
290 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
291 if cur_line: |
|
86381
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
292 if (self.max_lines is None or |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
293 len(lines) + 1 < self.max_lines or |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
294 (not chunks or |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
295 self.drop_whitespace and |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
296 len(chunks) == 1 and |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
297 not chunks[0].strip()) and cur_len <= width): |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
298 # Convert current line back to a string and store it in |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
299 # list of all lines (return value). |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
300 lines.append(indent + ''.join(cur_line)) |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
301 else: |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
302 while cur_line: |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
303 if (cur_line[-1].strip() and |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
304 cur_len + len(self.placeholder) <= width): |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
305 cur_line.append(self.placeholder) |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
306 lines.append(indent + ''.join(cur_line)) |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
307 break |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
308 cur_len -= len(cur_line[-1]) |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
309 del cur_line[-1] |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
310 else: |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
311 if lines: |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
312 prev_line = lines[-1].rstrip() |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
313 if (len(prev_line) + len(self.placeholder) <= |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
314 self.width): |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
315 lines[-1] = prev_line + self.placeholder |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
316 break |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
317 lines.append(indent + self.placeholder.lstrip()) |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
318 break |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
319 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
320 return lines |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
321 |
|
85140
c27ec198d3d1
Issue #18585: Add :func:`textwrap.shorten` to collapse and truncate a piece of text to a given length.
Antoine Pitrou <solipsis@pitrou.net>
parents:
77407
diff
changeset
|
322 def _split_chunks(self, text): |
|
c27ec198d3d1
Issue #18585: Add :func:`textwrap.shorten` to collapse and truncate a piece of text to a given length.
Antoine Pitrou <solipsis@pitrou.net>
parents:
77407
diff
changeset
|
323 text = self._munge_whitespace(text) |
|
c27ec198d3d1
Issue #18585: Add :func:`textwrap.shorten` to collapse and truncate a piece of text to a given length.
Antoine Pitrou <solipsis@pitrou.net>
parents:
77407
diff
changeset
|
324 return self._split(text) |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
325 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
326 # -- Public interface ---------------------------------------------- |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
327 |
|
23766
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
328 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
|
329 """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
|
330 |
|
24111
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
331 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
|
332 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
|
333 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
|
334 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
|
335 converted to space. |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
336 """ |
|
85140
c27ec198d3d1
Issue #18585: Add :func:`textwrap.shorten` to collapse and truncate a piece of text to a given length.
Antoine Pitrou <solipsis@pitrou.net>
parents:
77407
diff
changeset
|
337 chunks = self._split_chunks(text) |
|
23738
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
338 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
|
339 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
|
340 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
|
341 |
|
23770
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
342 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
|
343 """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
|
344 |
|
24111
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
345 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
|
346 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
|
347 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
|
348 """ |
|
23770
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
349 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
|
350 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
351 |
|
24111
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
352 # -- Convenience interface --------------------------------------------- |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
353 |
|
23767
5fb08d832eac
Allow the standalone wrap() and fill() functions to take arbitrary
Greg Ward <gward@python.net>
parents:
23766
diff
changeset
|
354 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
|
355 """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
|
356 |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
357 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
|
358 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
|
359 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
|
360 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
|
361 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
|
362 wrapping behaviour. |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
363 """ |
|
23767
5fb08d832eac
Allow the standalone wrap() and fill() functions to take arbitrary
Greg Ward <gward@python.net>
parents:
23766
diff
changeset
|
364 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
|
365 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
|
366 |
|
23770
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
367 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
|
368 """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
|
369 |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
370 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
|
371 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
|
372 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
|
373 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
|
374 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
|
375 """ |
|
23767
5fb08d832eac
Allow the standalone wrap() and fill() functions to take arbitrary
Greg Ward <gward@python.net>
parents:
23766
diff
changeset
|
376 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
|
377 return w.fill(text) |
|
29022
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
378 |
|
86381
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
379 def shorten(text, width, **kwargs): |
|
85140
c27ec198d3d1
Issue #18585: Add :func:`textwrap.shorten` to collapse and truncate a piece of text to a given length.
Antoine Pitrou <solipsis@pitrou.net>
parents:
77407
diff
changeset
|
380 """Collapse and truncate the given text to fit in the given width. |
|
c27ec198d3d1
Issue #18585: Add :func:`textwrap.shorten` to collapse and truncate a piece of text to a given length.
Antoine Pitrou <solipsis@pitrou.net>
parents:
77407
diff
changeset
|
381 |
|
c27ec198d3d1
Issue #18585: Add :func:`textwrap.shorten` to collapse and truncate a piece of text to a given length.
Antoine Pitrou <solipsis@pitrou.net>
parents:
77407
diff
changeset
|
382 The text first has its whitespace collapsed. If it then fits in |
|
c27ec198d3d1
Issue #18585: Add :func:`textwrap.shorten` to collapse and truncate a piece of text to a given length.
Antoine Pitrou <solipsis@pitrou.net>
parents:
77407
diff
changeset
|
383 the *width*, it is returned as is. Otherwise, as many words |
|
c27ec198d3d1
Issue #18585: Add :func:`textwrap.shorten` to collapse and truncate a piece of text to a given length.
Antoine Pitrou <solipsis@pitrou.net>
parents:
77407
diff
changeset
|
384 as possible are joined and then the placeholder is appended:: |
|
c27ec198d3d1
Issue #18585: Add :func:`textwrap.shorten` to collapse and truncate a piece of text to a given length.
Antoine Pitrou <solipsis@pitrou.net>
parents:
77407
diff
changeset
|
385 |
|
c27ec198d3d1
Issue #18585: Add :func:`textwrap.shorten` to collapse and truncate a piece of text to a given length.
Antoine Pitrou <solipsis@pitrou.net>
parents:
77407
diff
changeset
|
386 >>> textwrap.shorten("Hello world!", width=12) |
|
c27ec198d3d1
Issue #18585: Add :func:`textwrap.shorten` to collapse and truncate a piece of text to a given length.
Antoine Pitrou <solipsis@pitrou.net>
parents:
77407
diff
changeset
|
387 'Hello world!' |
|
c27ec198d3d1
Issue #18585: Add :func:`textwrap.shorten` to collapse and truncate a piece of text to a given length.
Antoine Pitrou <solipsis@pitrou.net>
parents:
77407
diff
changeset
|
388 >>> textwrap.shorten("Hello world!", width=11) |
|
85203
be5481bf4c57
Fix the default placeholder in textwrap.shorten() to be " [...]".
Antoine Pitrou <solipsis@pitrou.net>
parents:
85140
diff
changeset
|
389 'Hello [...]' |
|
85140
c27ec198d3d1
Issue #18585: Add :func:`textwrap.shorten` to collapse and truncate a piece of text to a given length.
Antoine Pitrou <solipsis@pitrou.net>
parents:
77407
diff
changeset
|
390 """ |
|
86381
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
391 w = TextWrapper(width=width, max_lines=1, **kwargs) |
|
2e8c424dc638
Issue #18725: The textwrap module now supports truncating multiline text.
Serhiy Storchaka <storchaka@gmail.com>
parents:
85203
diff
changeset
|
392 return w.fill(' '.join(text.strip().split())) |
|
85140
c27ec198d3d1
Issue #18585: Add :func:`textwrap.shorten` to collapse and truncate a piece of text to a given length.
Antoine Pitrou <solipsis@pitrou.net>
parents:
77407
diff
changeset
|
393 |
|
29022
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
394 |
|
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
395 # -- Loosely related functionality ------------------------------------- |
|
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
396 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
397 _whitespace_only_re = re.compile('^[ \t]+$', re.MULTILINE) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
398 _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
|
399 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
400 def dedent(text): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
401 """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
|
402 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
403 This can be used to make triple-quoted strings line up with the left |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
404 edge of the display, while still presenting them in the source code |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
405 in indented form. |
|
29022
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
406 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
407 Note that tabs and spaces are both treated as whitespace, but they |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
408 are not equal: the lines " hello" and "\thello" are |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
409 considered to have no common leading whitespace. (This behaviour is |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
410 new in Python 2.5; older versions of this module incorrectly |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
411 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
|
412 """ |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
413 # Look for the longest leading string of spaces and tabs common to |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
414 # all lines. |
|
29022
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
415 margin = None |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
416 text = _whitespace_only_re.sub('', text) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
417 indents = _leading_whitespace_re.findall(text) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
418 for indent in indents: |
|
29022
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
419 if margin is None: |
|
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
420 margin = indent |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
421 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
422 # Current line more deeply indented than previous winner: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
423 # no change (previous winner is still on top). |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
424 elif indent.startswith(margin): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
425 pass |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
426 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
427 # Current line consistent with and no deeper than previous winner: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
428 # it's the new winner. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
429 elif margin.startswith(indent): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
430 margin = indent |
|
29022
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
431 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
432 # Current line and previous winner have no common whitespace: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
433 # there is no margin. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
434 else: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
435 margin = "" |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
436 break |
|
29022
2f743de8c5e2
SF patch #598163 (Ville Vainio, vvainio@users.sourceforge.net):
Greg Ward <gward@python.net>
parents:
29007
diff
changeset
|
437 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
438 # sanity check (testing/debugging only) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
439 if 0 and margin: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
440 for line in text.split("\n"): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
441 assert not line or line.startswith(margin), \ |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
442 "line = %r, margin = %r" % (line, margin) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
443 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
444 if margin: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
445 text = re.sub(r'(?m)^' + margin, '', text) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
446 return text |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
447 |
|
77407
6f7afe25d681
Close #13857: Added textwrap.indent() function (initial patch by Ezra
Nick Coghlan <ncoghlan@gmail.com>
parents:
77050
diff
changeset
|
448 |
|
6f7afe25d681
Close #13857: Added textwrap.indent() function (initial patch by Ezra
Nick Coghlan <ncoghlan@gmail.com>
parents:
77050
diff
changeset
|
449 def indent(text, prefix, predicate=None): |
|
6f7afe25d681
Close #13857: Added textwrap.indent() function (initial patch by Ezra
Nick Coghlan <ncoghlan@gmail.com>
parents:
77050
diff
changeset
|
450 """Adds 'prefix' to the beginning of selected lines in 'text'. |
|
6f7afe25d681
Close #13857: Added textwrap.indent() function (initial patch by Ezra
Nick Coghlan <ncoghlan@gmail.com>
parents:
77050
diff
changeset
|
451 |
|
6f7afe25d681
Close #13857: Added textwrap.indent() function (initial patch by Ezra
Nick Coghlan <ncoghlan@gmail.com>
parents:
77050
diff
changeset
|
452 If 'predicate' is provided, 'prefix' will only be added to the lines |
|
6f7afe25d681
Close #13857: Added textwrap.indent() function (initial patch by Ezra
Nick Coghlan <ncoghlan@gmail.com>
parents:
77050
diff
changeset
|
453 where 'predicate(line)' is True. If 'predicate' is not provided, |
|
6f7afe25d681
Close #13857: Added textwrap.indent() function (initial patch by Ezra
Nick Coghlan <ncoghlan@gmail.com>
parents:
77050
diff
changeset
|
454 it will default to adding 'prefix' to all non-empty lines that do not |
|
6f7afe25d681
Close #13857: Added textwrap.indent() function (initial patch by Ezra
Nick Coghlan <ncoghlan@gmail.com>
parents:
77050
diff
changeset
|
455 consist solely of whitespace characters. |
|
6f7afe25d681
Close #13857: Added textwrap.indent() function (initial patch by Ezra
Nick Coghlan <ncoghlan@gmail.com>
parents:
77050
diff
changeset
|
456 """ |
|
6f7afe25d681
Close #13857: Added textwrap.indent() function (initial patch by Ezra
Nick Coghlan <ncoghlan@gmail.com>
parents:
77050
diff
changeset
|
457 if predicate is None: |
|
6f7afe25d681
Close #13857: Added textwrap.indent() function (initial patch by Ezra
Nick Coghlan <ncoghlan@gmail.com>
parents:
77050
diff
changeset
|
458 def predicate(line): |
|
6f7afe25d681
Close #13857: Added textwrap.indent() function (initial patch by Ezra
Nick Coghlan <ncoghlan@gmail.com>
parents:
77050
diff
changeset
|
459 return line.strip() |
|
6f7afe25d681
Close #13857: Added textwrap.indent() function (initial patch by Ezra
Nick Coghlan <ncoghlan@gmail.com>
parents:
77050
diff
changeset
|
460 |
|
6f7afe25d681
Close #13857: Added textwrap.indent() function (initial patch by Ezra
Nick Coghlan <ncoghlan@gmail.com>
parents:
77050
diff
changeset
|
461 def prefixed_lines(): |
|
6f7afe25d681
Close #13857: Added textwrap.indent() function (initial patch by Ezra
Nick Coghlan <ncoghlan@gmail.com>
parents:
77050
diff
changeset
|
462 for line in text.splitlines(True): |
|
6f7afe25d681
Close #13857: Added textwrap.indent() function (initial patch by Ezra
Nick Coghlan <ncoghlan@gmail.com>
parents:
77050
diff
changeset
|
463 yield (prefix + line if predicate(line) else line) |
|
6f7afe25d681
Close #13857: Added textwrap.indent() function (initial patch by Ezra
Nick Coghlan <ncoghlan@gmail.com>
parents:
77050
diff
changeset
|
464 return ''.join(prefixed_lines()) |
|
6f7afe25d681
Close #13857: Added textwrap.indent() function (initial patch by Ezra
Nick Coghlan <ncoghlan@gmail.com>
parents:
77050
diff
changeset
|
465 |
|
6f7afe25d681
Close #13857: Added textwrap.indent() function (initial patch by Ezra
Nick Coghlan <ncoghlan@gmail.com>
parents:
77050
diff
changeset
|
466 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
467 if __name__ == "__main__": |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
468 #print dedent("\tfoo\n\tbar") |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
35235
diff
changeset
|
469 #print dedent(" \thello there\n \t how are you?") |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
470 print(dedent("Hello there.\n This is indented.")) |
