Return data reformatted to specified width (limit).
(data, limit)
| 118 | |
| 119 | # This should perhaps be replaced with textwrap.wrap |
| 120 | def reformat_paragraph(data, limit): |
| 121 | """Return data reformatted to specified width (limit).""" |
| 122 | lines = data.split("\n") |
| 123 | i = 0 |
| 124 | n = len(lines) |
| 125 | while i < n and is_all_white(lines[i]): |
| 126 | i = i+1 |
| 127 | if i >= n: |
| 128 | return data |
| 129 | indent1 = get_indent(lines[i]) |
| 130 | if i+1 < n and not is_all_white(lines[i+1]): |
| 131 | indent2 = get_indent(lines[i+1]) |
| 132 | else: |
| 133 | indent2 = indent1 |
| 134 | new = lines[:i] |
| 135 | partial = indent1 |
| 136 | while i < n and not is_all_white(lines[i]): |
| 137 | # XXX Should take double space after period (etc.) into account |
| 138 | words = re.split(r"(\s+)", lines[i]) |
| 139 | for j in range(0, len(words), 2): |
| 140 | word = words[j] |
| 141 | if not word: |
| 142 | continue # Can happen when line ends in whitespace |
| 143 | if len((partial + word).expandtabs()) > limit and \ |
| 144 | partial != indent1: |
| 145 | new.append(partial.rstrip()) |
| 146 | partial = indent2 |
| 147 | partial = partial + word + " " |
| 148 | if j+1 < len(words) and words[j+1] != " ": |
| 149 | partial = partial + " " |
| 150 | i = i+1 |
| 151 | new.append(partial.rstrip()) |
| 152 | # XXX Should reformat remaining paragraphs as well |
| 153 | new.extend(lines[i:]) |
| 154 | return "\n".join(new) |
| 155 | |
| 156 | def reformat_comment(data, limit, comment_header): |
| 157 | """Return data reformatted to specified width with comment header.""" |
no test coverage detected
searching dependent graphs…