1 """Text wrapping and filling"""
2 from __future__
import absolute_import
, division
, print_function
, unicode_literals
5 from .compat
import ustr
7 # Copyright (C) 1999-2001 Gregory P. Ward.
8 # Copyright (C) 2002, 2003 Python Software Foundation.
9 # Copyright (C) 2013, David Aguilar
10 # Written by Greg Ward <gward@python.net>
11 # Simplified for git-cola by David Aguilar <davvid@gmail.com>
14 class TextWrapper(object):
16 Object for wrapping/filling text. The public interface consists of
17 the wrap() and fill() methods; the other methods are just there for
18 subclasses to override in order to tweak the default behaviour.
19 If you want to completely replace the main wrapping algorithm,
20 you'll probably have to override _wrap_chunks().
22 Several instance attributes control various aspects of wrapping:
24 The preferred width of wrapped lines.
26 The width of a tab used when calculating line length.
27 break_on_hyphens (default: false)
28 Allow breaking hyphenated words. If true, wrapping will occur
29 preferably on whitespaces and right after hyphens part of
31 drop_whitespace (default: true)
32 Drop leading and trailing whitespace from lines.
35 # This funky little regex is just the trick for splitting
36 # text up into word-wrappable chunks. E.g.
37 # "Hello there -- you goof-ball, use the -b option!"
39 # Hello/ /there/ /--/ /you/ /goof-/ball,/ /use/ /the/ /-b/ /option!
40 # (after stripping out empty strings).
41 wordsep_re
= re
.compile(
42 r
'(\s+|' # any whitespace
43 r
'[^\s\w]*\w+[^0-9\W]-(?=\w+[^0-9\W])|' # hyphenated words
44 r
'(?<=[\w\!\"\'\
&\
.\
,\?])-{2,}(?
=\w
))'
47 # This less funky little regex just split on recognized spaces. E.g.
48 # "Hello there -- you goof-ball, use the -b option!"
50 # Hello/ /there/ /--/ /you/ /goof-ball,/ /use/ /the/ /-b/ /option!/
51 wordsep_simple_re = re.compile(r'(\s
+)')
54 self, width=70, tabwidth=8, break_on_hyphens=False, drop_whitespace=True
57 self.tabwidth = tabwidth
58 self.break_on_hyphens = break_on_hyphens
59 self.drop_whitespace = drop_whitespace
61 # recompile the regexes for Unicode mode -- done in this clumsy way for
62 # backwards compatibility because it's rather common to monkey
-patch
63 # the TextWrapper class' wordsep_re attribute.
64 self
.wordsep_re_uni
= re
.compile(self
.wordsep_re
.pattern
, re
.U
)
65 self
.wordsep_simple_re_uni
= re
.compile(self
.wordsep_simple_re
.pattern
, re
.U
)
67 def _split(self
, text
):
68 """_split(text : string) -> [string]
70 Split the text to wrap into indivisible chunks. Chunks are
71 not quite the same as words; see _wrap_chunks() for full
72 details. As an example, the text
73 Look, goof-ball -- use the -b option!
74 breaks into the following chunks:
75 'Look,', ' ', 'goof-', 'ball', ' ', '--', ' ',
76 'use', ' ', 'the', ' ', '-b', ' ', 'option!'
77 if break_on_hyphens is True, or in:
78 'Look,', ' ', 'goof-ball', ' ', '--', ' ',
79 'use', ' ', 'the', ' ', '-b', ' ', option!'
82 if isinstance(text
, ustr
):
83 if self
.break_on_hyphens
:
84 pat
= self
.wordsep_re_uni
86 pat
= self
.wordsep_simple_re_uni
88 if self
.break_on_hyphens
:
91 pat
= self
.wordsep_simple_re
92 chunks
= pat
.split(text
)
93 chunks
= list(filter(None, chunks
)) # remove empty chunks
96 def _wrap_chunks(self
, chunks
):
97 """_wrap_chunks(chunks : [string]) -> [string]
99 Wrap a sequence of text chunks and return a list of lines of length
100 'self.width' or less. Some lines may be longer than this. Chunks
101 correspond roughly to words and the whitespace between them: each
102 chunk is indivisible, but a line break can come between any two
103 chunks. Chunks should not have internal whitespace; ie. a chunk is
104 either all whitespace or a "word". Whitespace chunks will be removed
105 from the beginning and end of lines, but apart from that whitespace is
110 # Arrange in reverse order so items can be efficiently popped
111 # from a stack of chucks.
112 chunks
= list(reversed(chunks
))
116 # Start the list of chunks that will make up the current line.
117 # cur_len is just the length of all the chunks in cur_line.
121 # Maximum width for this line.
124 # First chunk on line is a space -- drop it, unless this
125 # is the very beginning of the text (ie. no lines started yet).
126 if self
.drop_whitespace
and is_blank(chunks
[-1]) and lines
:
131 length
= self
.chunklen(chunks
[-1])
133 # Can at least squeeze this chunk onto the current line.
134 if cur_len
+ length
<= width
:
135 cur_line
.append(chunks
.pop())
137 # Nope, this line is full.
142 # The current line is full, and the next chunk is too big to
143 # fit on *any* line (not just this one).
144 if chunks
and self
.chunklen(chunks
[-1]) > width
:
146 cur_line
.append(chunks
.pop())
148 # Avoid whitespace at the beginining of split lines
151 and self
.drop_whitespace
153 and is_blank(cur_line
[0])
157 # If the last chunk on this line is all a space, drop it.
158 if self
.drop_whitespace
and cur_line
and is_blank(cur_line
[-1]):
161 # Convert current line back to a string and store it in list
162 # of all lines (return value).
164 lines
.append(''.join(cur_line
))
168 def chunklen(self
, word
):
169 """Return length of a word taking tabs into account
171 >>> w = TextWrapper(tabwidth=8)
172 >>> w.chunklen("\\t\\t\\t\\tX")
176 return len(word
.replace('\t', '')) + word
.count('\t') * self
.tabwidth
178 # -- Public interface ----------------------------------------------
180 def wrap(self
, text
):
181 """wrap(text : string) -> [string]
183 Reformat the single paragraph in 'text' so it fits in lines of
184 no more than 'self.width' columns, and return a list of wrapped
185 lines. Tabs in 'text' are expanded with string.expandtabs(),
186 and all other whitespace characters (including newline) are
189 chunks
= self
._split
(text
)
190 return self
._wrap
_chunks
(chunks
)
192 def fill(self
, text
):
193 """fill(text : string) -> string
195 Reformat the single paragraph in 'text' to fit in lines of no
196 more than 'self.width' columns, and return a new string
197 containing the entire wrapped paragraph.
199 return "\n".join(self
.wrap(text
))
202 def word_wrap(text
, tabwidth
, limit
, break_on_hyphens
=False):
203 """Wrap long lines to the specified limit"""
207 # Acked-by:, Signed-off-by:, Helped-by:, etc.
208 special_tag_rgx
= re
.compile(
213 r
'Based-on-patch-by|'
230 r
'Original-patch-by|'
246 r
'Tested-on-([a-zA-Z-_]+)-by|'
247 r
'With-suggestions-by'
249 r
'|([Cc]\.\s*[Ff]\.\s+)'
256 break_on_hyphens
=break_on_hyphens
,
257 drop_whitespace
=True,
260 for line
in text
.split('\n'):
261 if special_tag_rgx
.match(line
):
264 lines
.append(w
.fill(line
))
266 return '\n'.join(lines
)
269 def is_blank(string
):
270 return string
and not string
.strip(' ')