1 """Text wrapping and filling"""
4 from .compat
import ustr
6 # Copyright (C) 1999-2001 Gregory P. Ward.
7 # Copyright (C) 2002, 2003 Python Software Foundation.
8 # Copyright (C) 2013-2024 David Aguilar
9 # Written by Greg Ward <gward@python.net>
10 # Simplified for git-cola by David Aguilar <davvid@gmail.com>
15 Object for wrapping/filling text. The public interface consists of
16 the wrap() and fill() methods; the other methods are just there for
17 subclasses to override in order to tweak the default behaviour.
18 If you want to completely replace the main wrapping algorithm,
19 you'll probably have to override _wrap_chunks().
21 Several instance attributes control various aspects of wrapping:
23 The preferred width of wrapped lines.
25 The width of a tab used when calculating line length.
26 break_on_hyphens (default: false)
27 Allow breaking hyphenated words. If true, wrapping will occur
28 preferably on whitespace and right after the hyphenated part of
30 drop_whitespace (default: true)
31 Drop leading and trailing whitespace from lines.
34 # This funky little regex is just the trick for splitting
35 # text up into word-wrappable chunks. E.g.
36 # "Hello there -- you goof-ball, use the -b option!"
38 # Hello/ /there/ /--/ /you/ /goof-/ball,/ /use/ /the/ /-b/ /option!
39 # (after stripping out empty strings).
40 wordsep_re
= re
.compile(
41 r
'(\s+|' # any whitespace
42 r
'[^\s\w]*\w+[^0-9\W]-(?=\w+[^0-9\W])|' # hyphenated words
43 r
'(?<=[\w\!\"\'\
&\
.\
,\?])-{2,}(?
=\w
))'
46 # This less funky little regex just split on recognized spaces. E.g.
47 # "Hello there -- you goof-ball, use the -b option!"
49 # Hello/ /there/ /--/ /you/ /goof-ball,/ /use/ /the/ /-b/ /option!/
50 wordsep_simple_re = re.compile(r'(\s
+)')
53 self, width=70, tabwidth=8, break_on_hyphens=False, drop_whitespace=True
56 self.tabwidth = tabwidth
57 self.break_on_hyphens = break_on_hyphens
58 self.drop_whitespace = drop_whitespace
60 # recompile the regexes for Unicode mode -- done in this clumsy way for
61 # backwards compatibility because it's rather common to monkey
-patch
62 # the TextWrapper class' wordsep_re attribute.
63 self
.wordsep_re_uni
= re
.compile(self
.wordsep_re
.pattern
, re
.U
)
64 self
.wordsep_simple_re_uni
= re
.compile(self
.wordsep_simple_re
.pattern
, re
.U
)
66 def _split(self
, text
):
67 """_split(text : string) -> [string]
69 Split the text to wrap into indivisible chunks. Chunks are
70 not quite the same as words; see _wrap_chunks() for full
71 details. As an example, the text
72 Look, goof-ball -- use the -b option!
73 breaks into the following chunks:
74 'Look,', ' ', 'goof-', 'ball', ' ', '--', ' ',
75 'use', ' ', 'the', ' ', '-b', ' ', 'option!'
76 if break_on_hyphens is True, or in:
77 'Look,', ' ', 'goof-ball', ' ', '--', ' ',
78 'use', ' ', 'the', ' ', '-b', ' ', option!'
81 if isinstance(text
, ustr
):
82 if self
.break_on_hyphens
:
83 pat
= self
.wordsep_re_uni
85 pat
= self
.wordsep_simple_re_uni
87 if self
.break_on_hyphens
:
90 pat
= self
.wordsep_simple_re
91 chunks
= pat
.split(text
)
92 chunks
= list(filter(None, chunks
)) # remove empty chunks
95 def _wrap_chunks(self
, chunks
):
96 """_wrap_chunks(chunks : [string]) -> [string]
98 Wrap a sequence of text chunks and return a list of lines of length
99 'self.width' or less. Some lines may be longer than this. Chunks
100 correspond roughly to words and the whitespace between them: each
101 chunk is indivisible, but a line break can come between any two
102 chunks. Chunks should not have internal whitespace; i.e. a chunk is
103 either all whitespace or a "word". Whitespace chunks will be removed
104 from the beginning and end of lines, but apart from that whitespace is
109 # Arrange in reverse order so items can be efficiently popped
110 # from a stack of chucks.
111 chunks
= list(reversed(chunks
))
114 # Start the list of chunks that will make up the current line.
115 # cur_len is just the length of all the chunks in cur_line.
119 # Maximum width for this line.
122 # First chunk on line is a space -- drop it, unless this
123 # is the very beginning of the text (i.e. no lines started yet).
124 if self
.drop_whitespace
and is_blank(chunks
[-1]) and lines
:
129 length
= self
.chunklen(chunks
[-1])
131 # Can at least squeeze this chunk onto the current line.
132 if cur_len
+ length
<= width
:
133 cur_line
.append(chunks
.pop())
135 # Nope, this line is full.
140 # The current line is full, and the next chunk is too big to
141 # fit on *any* line (not just this one).
142 if chunks
and self
.chunklen(chunks
[-1]) > width
:
144 cur_line
.append(chunks
.pop())
146 # Avoid whitespace at the beginning of split lines
149 and self
.drop_whitespace
151 and is_blank(cur_line
[0])
155 # If the last chunk on this line is all a space, drop it.
156 if self
.drop_whitespace
and cur_line
and is_blank(cur_line
[-1]):
159 # Convert current line back to a string and store it in list
160 # of all lines (return value).
162 lines
.append(''.join(cur_line
))
166 def chunklen(self
, word
):
167 """Return length of a word taking tabs into account
169 >>> w = TextWrapper(tabwidth=8)
170 >>> w.chunklen("\\t\\t\\t\\tX")
174 return len(word
.replace('\t', '')) + word
.count('\t') * self
.tabwidth
176 # -- Public interface ----------------------------------------------
178 def wrap(self
, text
):
179 """wrap(text : string) -> [string]
181 Reformat the single paragraph in 'text' so it fits in lines of
182 no more than 'self.width' columns, and return a list of wrapped
183 lines. Tabs in 'text' are expanded with string.expandtabs(),
184 and all other whitespace characters (including newline) are
187 chunks
= self
._split
(text
)
188 return self
._wrap
_chunks
(chunks
)
190 def fill(self
, text
):
191 """fill(text : string) -> string
193 Reformat the single paragraph in 'text' to fit in lines of no
194 more than 'self.width' columns, and return a new string
195 containing the entire wrapped paragraph.
197 return '\n'.join(self
.wrap(text
))
200 def word_wrap(text
, tabwidth
, limit
, break_on_hyphens
=False):
201 """Wrap long lines to the specified limit"""
205 # Acked-by:, Signed-off-by:, Helped-by:, etc.
206 special_tag_rgx
= re
.compile(
211 r
'Based-on-patch-by|'
228 r
'Original-patch-by|'
244 r
'Tested-on-([a-zA-Z-_]+)-by|'
245 r
'With-suggestions-by'
247 r
'|([Cc]\.\s*[Ff]\.\s+)'
251 wrapper
= TextWrapper(
254 break_on_hyphens
=break_on_hyphens
,
255 drop_whitespace
=True,
258 for line
in text
.split('\n'):
259 if special_tag_rgx
.match(line
):
262 lines
.append(wrapper
.fill(line
))
264 return '\n'.join(lines
)
267 def is_blank(string
):
268 return string
and not string
.strip(' ')