Object for wrapping/filling text. The public interface consists of the wrap() and fill() methods; the other methods are just there for subclasses to override in order to tweak the default behaviour. If you want to completely replace the main wrapping algorithm, you'll probably
| 15 | _whitespace = '\t\n\x0b\x0c\r ' |
| 16 | |
| 17 | class TextWrapper: |
| 18 | """ |
| 19 | Object for wrapping/filling text. The public interface consists of |
| 20 | the wrap() and fill() methods; the other methods are just there for |
| 21 | subclasses to override in order to tweak the default behaviour. |
| 22 | If you want to completely replace the main wrapping algorithm, |
| 23 | you'll probably have to override _wrap_chunks(). |
| 24 | |
| 25 | Several instance attributes control various aspects of wrapping: |
| 26 | width (default: 70) |
| 27 | the maximum width of wrapped lines (unless break_long_words |
| 28 | is false) |
| 29 | initial_indent (default: "") |
| 30 | string that will be prepended to the first line of wrapped |
| 31 | output. Counts towards the line's width. |
| 32 | subsequent_indent (default: "") |
| 33 | string that will be prepended to all lines save the first |
| 34 | of wrapped output; also counts towards each line's width. |
| 35 | expand_tabs (default: true) |
| 36 | Expand tabs in input text to spaces before further processing. |
| 37 | Each tab will become 0 .. 'tabsize' spaces, depending on its position |
| 38 | in its line. If false, each tab is treated as a single character. |
| 39 | tabsize (default: 8) |
| 40 | Expand tabs in input text to 0 .. 'tabsize' spaces, unless |
| 41 | 'expand_tabs' is false. |
| 42 | replace_whitespace (default: true) |
| 43 | Replace all whitespace characters in the input text by spaces |
| 44 | after tab expansion. Note that if expand_tabs is false and |
| 45 | replace_whitespace is true, every tab will be converted to a |
| 46 | single space! |
| 47 | fix_sentence_endings (default: false) |
| 48 | Ensure that sentence-ending punctuation is always followed |
| 49 | by two spaces. Off by default because the algorithm is |
| 50 | (unavoidably) imperfect. |
| 51 | break_long_words (default: true) |
| 52 | Break words longer than 'width'. If false, those words will not |
| 53 | be broken, and some lines might be longer than 'width'. |
| 54 | break_on_hyphens (default: true) |
| 55 | Allow breaking hyphenated words. If true, wrapping will occur |
| 56 | preferably on whitespaces and right after hyphens part of |
| 57 | compound words. |
| 58 | drop_whitespace (default: true) |
| 59 | Drop leading and trailing whitespace from lines. |
| 60 | max_lines (default: None) |
| 61 | Truncate wrapped lines. |
| 62 | placeholder (default: ' [...]') |
| 63 | Append to the last line of truncated text. |
| 64 | """ |
| 65 | |
| 66 | unicode_whitespace_trans = dict.fromkeys(map(ord, _whitespace), ord(' ')) |
| 67 | |
| 68 | # This funky little regex is just the trick for splitting |
| 69 | # text up into word-wrappable chunks. E.g. |
| 70 | # "Hello there -- you goof-ball, use the -b option!" |
| 71 | # splits into |
| 72 | # Hello/ /there/ /--/ /you/ /goof-/ball,/ /use/ /the/ /-b/ /option! |
| 73 | # (after stripping out empty strings). |
| 74 | word_punct = r'[\w!"\'&.,?]' |
searching dependent graphs…