Skip to content

Commit 5ca4309

Browse files
committed
Fixed a bug in wordwrap() where an URL (or any word) longer than width could prevent line breaking in following text.
- Legacy-Id: 13541
1 parent e19290e commit 5ca4309

1 file changed

Lines changed: 20 additions & 17 deletions

File tree

ietf/utils/text.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,20 @@ def wordwrap(text, width=80):
6565
wrapped = False
6666
prev_indent = None
6767
for line in lines:
68-
line = line.expandtabs()
68+
line = line.expandtabs().rstrip()
6969
indent = " " * (len(line) - len(line.lstrip()))
70+
ind = len(indent)
7071
if wrapped and line.strip() != "" and indent == prev_indent:
7172
line = filled[-1] + " " + line.lstrip()
7273
filled = filled[:-1]
7374
else:
7475
wrapped = False
75-
while (len(line) > width) and (" " in line[:width]):
76+
while (len(line) > width) and (" " in line[ind:]):
7677
linelength = len(line)
7778
wrapped = True
78-
breakpoint = line.rfind(" ",0,width)
79+
breakpoint = line.rfind(" ",ind,width)
80+
if breakpoint == -1:
81+
breakpoint = line.find(" ", ind)
7982
filled += [ line[:breakpoint] ]
8083
line = indent + line[breakpoint+1:]
8184
if len(line) >= linelength:
@@ -85,20 +88,20 @@ def wordwrap(text, width=80):
8588
return "\n".join(filled)
8689

8790

88-
# def alternative_wrap(text, width=80):
89-
# # From http://blog.belgoat.com/python-textwrap-wrap-your-text-to-terminal-size/
90-
# textLines = text.split('\n')
91-
# wrapped_lines = []
92-
# # Preserve any indent (after the general indent)
93-
# for line in textLines:
94-
# preservedIndent = ''
95-
# existIndent = re.search(r'^(\W+)', line)
96-
# # Change the existing wrap indent to the original one
97-
# if (existIndent):
98-
# preservedIndent = existIndent.groups()[0]
99-
# wrapped_lines.append(textwrap.fill(line, width=width, subsequent_indent=preservedIndent))
100-
# text = '\n'.join(wrapped_lines)
101-
# return text
91+
def alternative_wrap(text, width=80):
92+
# From http://blog.belgoat.com/python-textwrap-wrap-your-text-to-terminal-size/
93+
textLines = text.split('\n')
94+
wrapped_lines = []
95+
# Preserve any indent (after the general indent)
96+
for line in textLines:
97+
preservedIndent = ''
98+
existIndent = re.search(r'^(\W+)', line)
99+
# Change the existing wrap indent to the original one
100+
if (existIndent):
101+
preservedIndent = existIndent.groups()[0]
102+
wrapped_lines.append(textwrap.fill(line, width=width, subsequent_indent=preservedIndent))
103+
text = '\n'.join(wrapped_lines)
104+
return text
102105

103106
def wrap_text_if_unwrapped(text, width=80, max_tolerated_line_length=100):
104107
text = re.sub(" *\r\n", "\n", text) # get rid of DOS line endings

0 commit comments

Comments
 (0)