Mercurial > cpython-lru_cache-weakref
annotate Lib/textwrap.py @ 27998:e27f58a50737 legacy-trunk
SF bug #690377: Dud footnote reference in distutils docs
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Fri, 21 Feb 2003 02:47:09 +0000 |
| parents | 2094e199fd55 |
| children | 3055c9bdff5f |
| 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. |
|
23743
2e1def417ecb
Record copyright and author.
Greg Ward <gward@python.net>
parents:
23742
diff
changeset
|
5 # Copyright (C) 2002 Python Software Foundation. |
|
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 |
|
25884
34f17fb49c51
Add comment about inability to handle Unicode strings (hopefully a
Greg Ward <gward@python.net>
parents:
25757
diff
changeset
|
8 # XXX currently this module does not work very well with Unicode |
|
34f17fb49c51
Add comment about inability to handle Unicode strings (hopefully a
Greg Ward <gward@python.net>
parents:
25757
diff
changeset
|
9 # strings. See http://www.python.org/sf/622831 for updates. |
|
34f17fb49c51
Add comment about inability to handle Unicode strings (hopefully a
Greg Ward <gward@python.net>
parents:
25757
diff
changeset
|
10 |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
11 __revision__ = "$Id$" |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
12 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
13 import string, re |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
14 |
|
27630
6567f4829e7e
Add __all__ (suggested by Raymond Hettinger).
Greg Ward <gward@python.net>
parents:
26614
diff
changeset
|
15 __all__ = ['TextWrapper', 'wrap', 'fill'] |
|
6567f4829e7e
Add __all__ (suggested by Raymond Hettinger).
Greg Ward <gward@python.net>
parents:
26614
diff
changeset
|
16 |
|
26614
2c7bdbb50e76
Hardcode the recognized whitespace characters to the US-ASCII whitespace
Greg Ward <gward@python.net>
parents:
26600
diff
changeset
|
17 # 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
|
18 # 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
|
19 # 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
|
20 # 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
|
21 # 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
|
22 # 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
|
23 # *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
|
24 # since 0xa0 is not in range(128). |
|
27630
6567f4829e7e
Add __all__ (suggested by Raymond Hettinger).
Greg Ward <gward@python.net>
parents:
26614
diff
changeset
|
25 _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
|
26 |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
27 class TextWrapper: |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
28 """ |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
29 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
|
30 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
|
31 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
|
32 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
|
33 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
|
34 |
|
23766
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
35 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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 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
|
42 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
|
43 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
|
44 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
|
45 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
|
46 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
|
47 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
|
48 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
|
49 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
|
50 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
|
51 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
|
52 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
|
53 single space! |
|
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
54 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
|
55 Ensure that sentence-ending punctuation is always followed |
| 27884 | 56 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
|
57 (unavoidably) imperfect. |
|
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
58 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
|
59 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
|
60 be broken, and some lines might be longer than 'width'. |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
61 """ |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
62 |
|
27630
6567f4829e7e
Add __all__ (suggested by Raymond Hettinger).
Greg Ward <gward@python.net>
parents:
26614
diff
changeset
|
63 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
|
64 |
|
26579
e8940d4cd8ca
Fix SF bug #622831 (I think): add unicode_whitespace_trans class
Greg Ward <gward@python.net>
parents:
25884
diff
changeset
|
65 unicode_whitespace_trans = {} |
|
26600
75bfc24fd748
Tweak generation of unicode_whitespace_trans -- clearer, more
Greg Ward <gward@python.net>
parents:
26580
diff
changeset
|
66 uspace = ord(u' ') |
|
27630
6567f4829e7e
Add __all__ (suggested by Raymond Hettinger).
Greg Ward <gward@python.net>
parents:
26614
diff
changeset
|
67 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
|
68 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
|
69 |
|
24263
e6033254df21
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24111
diff
changeset
|
70 # 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
|
71 # 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
|
72 # "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
|
73 # splits into |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
74 # 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
|
75 # (after stripping out empty strings). |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
76 wordsep_re = re.compile(r'(\s+|' # any whitespace |
|
24965
1b4ef9cd1897
Fix SF bug #596434: tweak wordsep_re so "--foo-bar" now splits
Greg Ward <gward@python.net>
parents:
24263
diff
changeset
|
77 r'-*\w{2,}-(?=\w{2,})|' # hyphenated words |
|
25757
0838c4247917
Fix copyright date: the core of this code actually dates back to 1999
Greg Ward <gward@python.net>
parents:
25534
diff
changeset
|
78 r'(?<=\S)-{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
|
79 |
|
23739
27f48314ba9d
Convert _fix_sentence_endings() to use a regex, and augment it to
Greg Ward <gward@python.net>
parents:
23738
diff
changeset
|
80 # XXX will there be a locale-or-charset-aware version of |
|
27f48314ba9d
Convert _fix_sentence_endings() to use a regex, and augment it to
Greg Ward <gward@python.net>
parents:
23738
diff
changeset
|
81 # string.lowercase in 2.3? |
|
27f48314ba9d
Convert _fix_sentence_endings() to use a regex, and augment it to
Greg Ward <gward@python.net>
parents:
23738
diff
changeset
|
82 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
|
83 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
|
84 r'[\"\']?' # optional end-of-quote |
|
27f48314ba9d
Convert _fix_sentence_endings() to use a regex, and augment it to
Greg Ward <gward@python.net>
parents:
23738
diff
changeset
|
85 % string.lowercase) |
|
23738
a68d2466d94b
Add fix_sentence_endings option to control whether we ensure that
Greg Ward <gward@python.net>
parents:
23737
diff
changeset
|
86 |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
87 |
|
23744
ea035490f624
Make all of TextWrapper's options keyword args to the constructor.
Greg Ward <gward@python.net>
parents:
23743
diff
changeset
|
88 def __init__ (self, |
|
23766
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
89 width=70, |
|
23770
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
90 initial_indent="", |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
91 subsequent_indent="", |
|
23744
ea035490f624
Make all of TextWrapper's options keyword args to the constructor.
Greg Ward <gward@python.net>
parents:
23743
diff
changeset
|
92 expand_tabs=True, |
|
ea035490f624
Make all of TextWrapper's options keyword args to the constructor.
Greg Ward <gward@python.net>
parents:
23743
diff
changeset
|
93 replace_whitespace=True, |
|
ea035490f624
Make all of TextWrapper's options keyword args to the constructor.
Greg Ward <gward@python.net>
parents:
23743
diff
changeset
|
94 fix_sentence_endings=False, |
|
ea035490f624
Make all of TextWrapper's options keyword args to the constructor.
Greg Ward <gward@python.net>
parents:
23743
diff
changeset
|
95 break_long_words=True): |
|
23766
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
96 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
|
97 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
|
98 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
|
99 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
|
100 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
|
101 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
|
102 self.break_long_words = break_long_words |
|
24263
e6033254df21
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24111
diff
changeset
|
103 |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
104 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
105 # -- Private methods ----------------------------------------------- |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
106 # (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
|
107 |
|
23740
a2136ae9d4f7
Conform to the bloody coding standards: "def foo()" not "def foo ()".
Greg Ward <gward@python.net>
parents:
23739
diff
changeset
|
108 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
|
109 """_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
|
110 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
111 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
|
112 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
|
113 becomes " foo bar baz". |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
114 """ |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
115 if self.expand_tabs: |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
116 text = text.expandtabs() |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
117 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
|
118 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
|
119 text = text.translate(self.whitespace_trans) |
|
e8940d4cd8ca
Fix SF bug #622831 (I think): add unicode_whitespace_trans class
Greg Ward <gward@python.net>
parents:
25884
diff
changeset
|
120 elif isinstance(text, unicode): |
|
e8940d4cd8ca
Fix SF bug #622831 (I think): add unicode_whitespace_trans class
Greg Ward <gward@python.net>
parents:
25884
diff
changeset
|
121 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
|
122 return text |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
123 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
124 |
|
23740
a2136ae9d4f7
Conform to the bloody coding standards: "def foo()" not "def foo ()".
Greg Ward <gward@python.net>
parents:
23739
diff
changeset
|
125 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
|
126 """_split(text : string) -> [string] |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
127 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
128 Split the text to wrap into indivisible chunks. Chunks are |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
129 not quite the same as words; see wrap_chunks() for full |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
130 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
|
131 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
|
132 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
|
133 'Look,', ' ', 'goof-', 'ball', ' ', '--', ' ', |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
134 'use', ' ', 'the', ' ', '-b', ' ', 'option!' |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
135 """ |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
136 chunks = self.wordsep_re.split(text) |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
137 chunks = filter(None, chunks) |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
138 return chunks |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
139 |
|
23740
a2136ae9d4f7
Conform to the bloody coding standards: "def foo()" not "def foo ()".
Greg Ward <gward@python.net>
parents:
23739
diff
changeset
|
140 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
|
141 """_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
|
142 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
143 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
|
144 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
|
145 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
|
146 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
|
147 space to two. |
|
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 i = 0 |
|
23739
27f48314ba9d
Convert _fix_sentence_endings() to use a regex, and augment it to
Greg Ward <gward@python.net>
parents:
23738
diff
changeset
|
150 pat = self.sentence_end_re |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
151 while i < len(chunks)-1: |
|
23739
27f48314ba9d
Convert _fix_sentence_endings() to use a regex, and augment it to
Greg Ward <gward@python.net>
parents:
23738
diff
changeset
|
152 if chunks[i+1] == " " and pat.search(chunks[i]): |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
153 chunks[i+1] = " " |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
154 i += 2 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
155 else: |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
156 i += 1 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
157 |
|
23770
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
158 def _handle_long_word(self, 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
|
159 """_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
|
160 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
|
161 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
|
162 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
163 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
|
164 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
|
165 """ |
|
23770
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
166 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
|
167 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
168 # 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
|
169 # 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
|
170 if self.break_long_words: |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
171 cur_line.append(chunks[0][0:space_left]) |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
172 chunks[0] = chunks[0][space_left:] |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
173 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
174 # 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
|
175 # 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
|
176 # 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
|
177 elif not cur_line: |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
178 cur_line.append(chunks.pop(0)) |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
179 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
180 # 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
|
181 # 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
|
182 # 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
|
183 # 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
|
184 # 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
|
185 |
|
23766
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
186 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
|
187 """_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
|
188 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
189 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
|
190 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
|
191 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
|
192 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
|
193 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
|
194 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
|
195 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
|
196 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
|
197 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
|
198 """ |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
199 lines = [] |
|
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 while chunks: |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
202 |
|
23770
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
203 # 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
|
204 # 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
|
205 cur_line = [] |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
206 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
|
207 |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
208 # 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
|
209 if lines: |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
210 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
|
211 else: |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
212 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
|
213 |
|
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
214 # 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
|
215 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
|
216 |
|
26580
d4b76817c9e6
Fix SF bug #622849: in _wrap_chunks(), ensure that leading whitespace in
Greg Ward <gward@python.net>
parents:
26579
diff
changeset
|
217 # 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
|
218 # is the very beginning of the text (ie. no lines started yet). |
|
d4b76817c9e6
Fix SF bug #622849: in _wrap_chunks(), ensure that leading whitespace in
Greg Ward <gward@python.net>
parents:
26579
diff
changeset
|
219 if chunks[0].strip() == '' and lines: |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
220 del chunks[0] |
|
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 while chunks: |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
223 l = len(chunks[0]) |
|
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 # 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
|
226 if cur_len + l <= width: |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
227 cur_line.append(chunks.pop(0)) |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
228 cur_len += l |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
229 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
230 # 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
|
231 else: |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
232 break |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
233 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
234 # 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
|
235 # fit on *any* line (not just this one). |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
236 if chunks and len(chunks[0]) > width: |
|
23770
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
237 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
|
238 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
239 # If the last chunk on this line is all whitespace, drop it. |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
240 if cur_line and cur_line[-1].strip() == '': |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
241 del cur_line[-1] |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
242 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
243 # 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
|
244 # 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
|
245 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
|
246 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
|
247 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
248 return lines |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
249 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
250 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
251 # -- Public interface ---------------------------------------------- |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
252 |
|
23766
1cbcddca33b0
Make 'width' an instance attribute rather than an argument to the wrap()
Greg Ward <gward@python.net>
parents:
23744
diff
changeset
|
253 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
|
254 """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
|
255 |
|
24111
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
256 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
|
257 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
|
258 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
|
259 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
|
260 converted to space. |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
261 """ |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
262 text = self._munge_whitespace(text) |
|
25534
08148e916d38
Fix an endcase bug: initial_indent was ignored when the text was short
Guido van Rossum <guido@python.org>
parents:
24970
diff
changeset
|
263 indent = self.initial_indent |
|
08148e916d38
Fix an endcase bug: initial_indent was ignored when the text was short
Guido van Rossum <guido@python.org>
parents:
24970
diff
changeset
|
264 if len(text) + len(indent) <= self.width: |
|
08148e916d38
Fix an endcase bug: initial_indent was ignored when the text was short
Guido van Rossum <guido@python.org>
parents:
24970
diff
changeset
|
265 return [indent + text] |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
266 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
|
267 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
|
268 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
|
269 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
|
270 |
|
23770
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
271 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
|
272 """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
|
273 |
|
24111
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
274 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
|
275 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
|
276 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
|
277 """ |
|
23770
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
278 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
|
279 |
|
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
280 |
|
24111
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
281 # -- Convenience interface --------------------------------------------- |
|
23737
df1a275ff4f2
Initial revision. Currently biased towards English in a fixed-width font,
Greg Ward <gward@python.net>
parents:
diff
changeset
|
282 |
|
23767
5fb08d832eac
Allow the standalone wrap() and fill() functions to take arbitrary
Greg Ward <gward@python.net>
parents:
23766
diff
changeset
|
283 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
|
284 """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
|
285 |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
286 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
|
287 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
|
288 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
|
289 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
|
290 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
|
291 wrapping behaviour. |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
292 """ |
|
23767
5fb08d832eac
Allow the standalone wrap() and fill() functions to take arbitrary
Greg Ward <gward@python.net>
parents:
23766
diff
changeset
|
293 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
|
294 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
|
295 |
|
23770
f6b2719fbf11
Took initial_tab and subsequent_tab away from the fill() method and
Greg Ward <gward@python.net>
parents:
23767
diff
changeset
|
296 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
|
297 """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
|
298 |
|
34d7137098a7
Docstring improvements. In particular, added docstrings for the
Greg Ward <gward@python.net>
parents:
23770
diff
changeset
|
299 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
|
300 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
|
301 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
|
302 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
|
303 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
|
304 """ |
|
23767
5fb08d832eac
Allow the standalone wrap() and fill() functions to take arbitrary
Greg Ward <gward@python.net>
parents:
23766
diff
changeset
|
305 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
|
306 return w.fill(text) |
