Write 'text' word-wrapped at self.width characters.
(self, text, indent=0)
| 135 | return dollar_count |
| 136 | |
| 137 | def _line(self, text, indent=0): |
| 138 | """Write 'text' word-wrapped at self.width characters.""" |
| 139 | leading_space = ' ' * indent |
| 140 | if self.width > 0: |
| 141 | while len(leading_space) + len(text) > self.width: |
| 142 | # The text is too wide; wrap if possible. |
| 143 | |
| 144 | # Find the rightmost space that would obey our width constraint and |
| 145 | # that's not an escaped space. |
| 146 | available_space = self.width - len(leading_space) - len(' $') |
| 147 | space = available_space |
| 148 | while True: |
| 149 | space = text.rfind(' ', 0, space) |
| 150 | if (space < 0 or |
| 151 | self._count_dollars_before_index(text, space) % 2 == 0): |
| 152 | break |
| 153 | |
| 154 | if space < 0: |
| 155 | # No such space; just use the first unescaped space we can find. |
| 156 | space = available_space - 1 |
| 157 | while True: |
| 158 | space = text.find(' ', space + 1) |
| 159 | if (space < 0 or |
| 160 | self._count_dollars_before_index(text, space) % 2 == 0): |
| 161 | break |
| 162 | if space < 0: |
| 163 | # Give up on breaking. |
| 164 | break |
| 165 | |
| 166 | self.output.write(leading_space + text[0:space] + ' $\n') |
| 167 | text = text[space+1:] |
| 168 | |
| 169 | # Subsequent lines are continuations, so indent them. |
| 170 | leading_space = ' ' * (indent+2) |
| 171 | |
| 172 | self.output.write(leading_space + text + '\n') |
| 173 | |
| 174 | def close(self): |
| 175 | self.output.close() |