Greg Ward | e807e57 | 2002-07-04 14:51:49 +0000 | [diff] [blame] | 1 | """Text wrapping and filling. |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 2 | """ |
| 3 | |
Greg Ward | 78cc051 | 2002-10-13 19:23:18 +0000 | [diff] [blame] | 4 | # Copyright (C) 1999-2001 Gregory P. Ward. |
Greg Ward | 698d9f0 | 2002-06-07 22:40:23 +0000 | [diff] [blame] | 5 | # Copyright (C) 2002 Python Software Foundation. |
| 6 | # Written by Greg Ward <gward@python.net> |
| 7 | |
Greg Ward | 4c486bc | 2002-10-22 18:31:50 +0000 | [diff] [blame] | 8 | # XXX currently this module does not work very well with Unicode |
| 9 | # strings. See http://www.python.org/sf/622831 for updates. |
| 10 | |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 11 | __revision__ = "$Id$" |
| 12 | |
| 13 | import string, re |
| 14 | |
Greg Ward | 4c6c9c4 | 2003-02-03 14:46:57 +0000 | [diff] [blame] | 15 | __all__ = ['TextWrapper', 'wrap', 'fill'] |
| 16 | |
Greg Ward | afd44de | 2002-12-12 17:24:35 +0000 | [diff] [blame] | 17 | # Hardcode the recognized whitespace characters to the US-ASCII |
| 18 | # whitespace characters. The main reason for doing this is that in |
| 19 | # ISO-8859-1, 0xa0 is non-breaking whitespace, so in certain locales |
| 20 | # that character winds up in string.whitespace. Respecting |
| 21 | # string.whitespace in those cases would 1) make textwrap treat 0xa0 the |
| 22 | # same as any other whitespace char, which is clearly wrong (it's a |
| 23 | # *non-breaking* space), 2) possibly cause problems with Unicode, |
| 24 | # since 0xa0 is not in range(128). |
Greg Ward | 4c6c9c4 | 2003-02-03 14:46:57 +0000 | [diff] [blame] | 25 | _whitespace = '\t\n\x0b\x0c\r ' |
Greg Ward | afd44de | 2002-12-12 17:24:35 +0000 | [diff] [blame] | 26 | |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 27 | class TextWrapper: |
| 28 | """ |
| 29 | Object for wrapping/filling text. The public interface consists of |
| 30 | the wrap() and fill() methods; the other methods are just there for |
| 31 | subclasses to override in order to tweak the default behaviour. |
| 32 | If you want to completely replace the main wrapping algorithm, |
| 33 | you'll probably have to override _wrap_chunks(). |
| 34 | |
Greg Ward | d34c959 | 2002-06-10 20:26:02 +0000 | [diff] [blame] | 35 | Several instance attributes control various aspects of wrapping: |
| 36 | width (default: 70) |
| 37 | the maximum width of wrapped lines (unless break_long_words |
| 38 | is false) |
Greg Ward | 62080be | 2002-06-10 21:37:12 +0000 | [diff] [blame] | 39 | initial_indent (default: "") |
| 40 | string that will be prepended to the first line of wrapped |
| 41 | output. Counts towards the line's width. |
| 42 | subsequent_indent (default: "") |
| 43 | string that will be prepended to all lines save the first |
| 44 | of wrapped output; also counts towards each line's width. |
Greg Ward | 62e4f3b | 2002-06-07 21:56:16 +0000 | [diff] [blame] | 45 | expand_tabs (default: true) |
| 46 | Expand tabs in input text to spaces before further processing. |
| 47 | Each tab will become 1 .. 8 spaces, depending on its position in |
| 48 | its line. If false, each tab is treated as a single character. |
| 49 | replace_whitespace (default: true) |
| 50 | Replace all whitespace characters in the input text by spaces |
| 51 | after tab expansion. Note that if expand_tabs is false and |
| 52 | replace_whitespace is true, every tab will be converted to a |
| 53 | single space! |
| 54 | fix_sentence_endings (default: false) |
| 55 | Ensure that sentence-ending punctuation is always followed |
Andrew M. Kuchling | a2ecabe | 2003-02-14 01:14:15 +0000 | [diff] [blame] | 56 | by two spaces. Off by default because the algorithm is |
Greg Ward | 62e4f3b | 2002-06-07 21:56:16 +0000 | [diff] [blame] | 57 | (unavoidably) imperfect. |
| 58 | break_long_words (default: true) |
Greg Ward | d34c959 | 2002-06-10 20:26:02 +0000 | [diff] [blame] | 59 | Break words longer than 'width'. If false, those words will not |
| 60 | be broken, and some lines might be longer than 'width'. |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 61 | """ |
| 62 | |
Greg Ward | 4c6c9c4 | 2003-02-03 14:46:57 +0000 | [diff] [blame] | 63 | whitespace_trans = string.maketrans(_whitespace, ' ' * len(_whitespace)) |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 64 | |
Greg Ward | 2e74541 | 2002-12-09 16:23:08 +0000 | [diff] [blame] | 65 | unicode_whitespace_trans = {} |
Greg Ward | 0e88c9f | 2002-12-11 13:54:20 +0000 | [diff] [blame] | 66 | uspace = ord(u' ') |
Greg Ward | 4c6c9c4 | 2003-02-03 14:46:57 +0000 | [diff] [blame] | 67 | for x in map(ord, _whitespace): |
Greg Ward | 0e88c9f | 2002-12-11 13:54:20 +0000 | [diff] [blame] | 68 | unicode_whitespace_trans[x] = uspace |
Greg Ward | 2e74541 | 2002-12-09 16:23:08 +0000 | [diff] [blame] | 69 | |
Tim Peters | c411dba | 2002-07-16 21:35:23 +0000 | [diff] [blame] | 70 | # This funky little regex is just the trick for splitting |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 71 | # text up into word-wrappable chunks. E.g. |
| 72 | # "Hello there -- you goof-ball, use the -b option!" |
| 73 | # splits into |
| 74 | # Hello/ /there/ /--/ /you/ /goof-/ball,/ /use/ /the/ /-b/ /option! |
| 75 | # (after stripping out empty strings). |
| 76 | wordsep_re = re.compile(r'(\s+|' # any whitespace |
Greg Ward | cce4d67 | 2002-08-22 21:04:21 +0000 | [diff] [blame] | 77 | r'-*\w{2,}-(?=\w{2,})|' # hyphenated words |
Greg Ward | a409f7c | 2003-05-07 01:20:58 +0000 | [diff] [blame] | 78 | r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))') # em-dash |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 79 | |
Greg Ward | 9b4864e | 2002-06-07 22:04:15 +0000 | [diff] [blame] | 80 | # XXX will there be a locale-or-charset-aware version of |
| 81 | # string.lowercase in 2.3? |
| 82 | sentence_end_re = re.compile(r'[%s]' # lowercase letter |
| 83 | r'[\.\!\?]' # sentence-ending punct. |
| 84 | r'[\"\']?' # optional end-of-quote |
| 85 | % string.lowercase) |
Greg Ward | 62e4f3b | 2002-06-07 21:56:16 +0000 | [diff] [blame] | 86 | |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 87 | |
Greg Ward | 47df99d | 2002-06-09 00:22:07 +0000 | [diff] [blame] | 88 | def __init__ (self, |
Greg Ward | d34c959 | 2002-06-10 20:26:02 +0000 | [diff] [blame] | 89 | width=70, |
Greg Ward | 62080be | 2002-06-10 21:37:12 +0000 | [diff] [blame] | 90 | initial_indent="", |
| 91 | subsequent_indent="", |
Greg Ward | 47df99d | 2002-06-09 00:22:07 +0000 | [diff] [blame] | 92 | expand_tabs=True, |
| 93 | replace_whitespace=True, |
| 94 | fix_sentence_endings=False, |
| 95 | break_long_words=True): |
Greg Ward | d34c959 | 2002-06-10 20:26:02 +0000 | [diff] [blame] | 96 | self.width = width |
Greg Ward | 62080be | 2002-06-10 21:37:12 +0000 | [diff] [blame] | 97 | self.initial_indent = initial_indent |
| 98 | self.subsequent_indent = subsequent_indent |
Greg Ward | 47df99d | 2002-06-09 00:22:07 +0000 | [diff] [blame] | 99 | self.expand_tabs = expand_tabs |
| 100 | self.replace_whitespace = replace_whitespace |
| 101 | self.fix_sentence_endings = fix_sentence_endings |
| 102 | self.break_long_words = break_long_words |
Tim Peters | c411dba | 2002-07-16 21:35:23 +0000 | [diff] [blame] | 103 | |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 104 | |
| 105 | # -- Private methods ----------------------------------------------- |
| 106 | # (possibly useful for subclasses to override) |
| 107 | |
Greg Ward | cb320eb | 2002-06-07 22:32:15 +0000 | [diff] [blame] | 108 | def _munge_whitespace(self, text): |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 109 | """_munge_whitespace(text : string) -> string |
| 110 | |
| 111 | Munge whitespace in text: expand tabs and convert all other |
| 112 | whitespace characters to spaces. Eg. " foo\tbar\n\nbaz" |
| 113 | becomes " foo bar baz". |
| 114 | """ |
| 115 | if self.expand_tabs: |
| 116 | text = text.expandtabs() |
| 117 | if self.replace_whitespace: |
Greg Ward | 2e74541 | 2002-12-09 16:23:08 +0000 | [diff] [blame] | 118 | if isinstance(text, str): |
| 119 | text = text.translate(self.whitespace_trans) |
| 120 | elif isinstance(text, unicode): |
| 121 | text = text.translate(self.unicode_whitespace_trans) |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 122 | return text |
| 123 | |
| 124 | |
Greg Ward | cb320eb | 2002-06-07 22:32:15 +0000 | [diff] [blame] | 125 | def _split(self, text): |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 126 | """_split(text : string) -> [string] |
| 127 | |
| 128 | Split the text to wrap into indivisible chunks. Chunks are |
| 129 | not quite the same as words; see wrap_chunks() for full |
| 130 | details. As an example, the text |
| 131 | Look, goof-ball -- use the -b option! |
| 132 | breaks into the following chunks: |
| 133 | 'Look,', ' ', 'goof-', 'ball', ' ', '--', ' ', |
| 134 | 'use', ' ', 'the', ' ', '-b', ' ', 'option!' |
| 135 | """ |
| 136 | chunks = self.wordsep_re.split(text) |
| 137 | chunks = filter(None, chunks) |
| 138 | return chunks |
| 139 | |
Greg Ward | cb320eb | 2002-06-07 22:32:15 +0000 | [diff] [blame] | 140 | def _fix_sentence_endings(self, chunks): |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 141 | """_fix_sentence_endings(chunks : [string]) |
| 142 | |
| 143 | Correct for sentence endings buried in 'chunks'. Eg. when the |
| 144 | original text contains "... foo.\nBar ...", munge_whitespace() |
| 145 | and split() will convert that to [..., "foo.", " ", "Bar", ...] |
| 146 | which has one too few spaces; this method simply changes the one |
| 147 | space to two. |
| 148 | """ |
| 149 | i = 0 |
Greg Ward | 9b4864e | 2002-06-07 22:04:15 +0000 | [diff] [blame] | 150 | pat = self.sentence_end_re |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 151 | while i < len(chunks)-1: |
Greg Ward | 9b4864e | 2002-06-07 22:04:15 +0000 | [diff] [blame] | 152 | if chunks[i+1] == " " and pat.search(chunks[i]): |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 153 | chunks[i+1] = " " |
| 154 | i += 2 |
| 155 | else: |
| 156 | i += 1 |
| 157 | |
Greg Ward | 62080be | 2002-06-10 21:37:12 +0000 | [diff] [blame] | 158 | def _handle_long_word(self, chunks, cur_line, cur_len, width): |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 159 | """_handle_long_word(chunks : [string], |
| 160 | cur_line : [string], |
Greg Ward | 62080be | 2002-06-10 21:37:12 +0000 | [diff] [blame] | 161 | cur_len : int, width : int) |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 162 | |
| 163 | Handle a chunk of text (most likely a word, not whitespace) that |
| 164 | is too long to fit in any line. |
| 165 | """ |
Greg Ward | 62080be | 2002-06-10 21:37:12 +0000 | [diff] [blame] | 166 | space_left = width - cur_len |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 167 | |
| 168 | # If we're allowed to break long words, then do so: put as much |
| 169 | # of the next chunk onto the current line as will fit. |
| 170 | if self.break_long_words: |
| 171 | cur_line.append(chunks[0][0:space_left]) |
| 172 | chunks[0] = chunks[0][space_left:] |
| 173 | |
| 174 | # Otherwise, we have to preserve the long word intact. Only add |
| 175 | # it to the current line if there's nothing already there -- |
| 176 | # that minimizes how much we violate the width constraint. |
| 177 | elif not cur_line: |
| 178 | cur_line.append(chunks.pop(0)) |
| 179 | |
| 180 | # If we're not allowed to break long words, and there's already |
| 181 | # text on the current line, do nothing. Next time through the |
| 182 | # main loop of _wrap_chunks(), we'll wind up here again, but |
| 183 | # cur_len will be zero, so the next line will be entirely |
| 184 | # devoted to the long word that we can't handle right now. |
| 185 | |
Greg Ward | d34c959 | 2002-06-10 20:26:02 +0000 | [diff] [blame] | 186 | def _wrap_chunks(self, chunks): |
| 187 | """_wrap_chunks(chunks : [string]) -> [string] |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 188 | |
| 189 | Wrap a sequence of text chunks and return a list of lines of |
Greg Ward | d34c959 | 2002-06-10 20:26:02 +0000 | [diff] [blame] | 190 | length 'self.width' or less. (If 'break_long_words' is false, |
| 191 | some lines may be longer than this.) Chunks correspond roughly |
| 192 | to words and the whitespace between them: each chunk is |
| 193 | indivisible (modulo 'break_long_words'), but a line break can |
| 194 | come between any two chunks. Chunks should not have internal |
| 195 | whitespace; ie. a chunk is either all whitespace or a "word". |
| 196 | Whitespace chunks will be removed from the beginning and end of |
| 197 | lines, but apart from that whitespace is preserved. |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 198 | """ |
| 199 | lines = [] |
Greg Ward | 21820cd | 2003-05-07 00:55:35 +0000 | [diff] [blame] | 200 | if self.width <= 0: |
| 201 | raise ValueError("invalid width %r (must be > 0)" % self.width) |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 202 | |
| 203 | while chunks: |
| 204 | |
Greg Ward | 62080be | 2002-06-10 21:37:12 +0000 | [diff] [blame] | 205 | # Start the list of chunks that will make up the current line. |
| 206 | # cur_len is just the length of all the chunks in cur_line. |
| 207 | cur_line = [] |
| 208 | cur_len = 0 |
| 209 | |
| 210 | # Figure out which static string will prefix this line. |
| 211 | if lines: |
| 212 | indent = self.subsequent_indent |
| 213 | else: |
| 214 | indent = self.initial_indent |
| 215 | |
| 216 | # Maximum width for this line. |
| 217 | width = self.width - len(indent) |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 218 | |
Greg Ward | ab73d46 | 2002-12-09 16:26:05 +0000 | [diff] [blame] | 219 | # First chunk on line is whitespace -- drop it, unless this |
| 220 | # is the very beginning of the text (ie. no lines started yet). |
| 221 | if chunks[0].strip() == '' and lines: |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 222 | del chunks[0] |
| 223 | |
| 224 | while chunks: |
| 225 | l = len(chunks[0]) |
| 226 | |
| 227 | # Can at least squeeze this chunk onto the current line. |
| 228 | if cur_len + l <= width: |
| 229 | cur_line.append(chunks.pop(0)) |
| 230 | cur_len += l |
| 231 | |
| 232 | # Nope, this line is full. |
| 233 | else: |
| 234 | break |
| 235 | |
| 236 | # The current line is full, and the next chunk is too big to |
Tim Peters | c411dba | 2002-07-16 21:35:23 +0000 | [diff] [blame] | 237 | # fit on *any* line (not just this one). |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 238 | if chunks and len(chunks[0]) > width: |
Greg Ward | 62080be | 2002-06-10 21:37:12 +0000 | [diff] [blame] | 239 | self._handle_long_word(chunks, cur_line, cur_len, width) |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 240 | |
| 241 | # If the last chunk on this line is all whitespace, drop it. |
| 242 | if cur_line and cur_line[-1].strip() == '': |
| 243 | del cur_line[-1] |
| 244 | |
| 245 | # Convert current line back to a string and store it in list |
| 246 | # of all lines (return value). |
| 247 | if cur_line: |
Greg Ward | 62080be | 2002-06-10 21:37:12 +0000 | [diff] [blame] | 248 | lines.append(indent + ''.join(cur_line)) |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 249 | |
| 250 | return lines |
| 251 | |
| 252 | |
| 253 | # -- Public interface ---------------------------------------------- |
| 254 | |
Greg Ward | d34c959 | 2002-06-10 20:26:02 +0000 | [diff] [blame] | 255 | def wrap(self, text): |
| 256 | """wrap(text : string) -> [string] |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 257 | |
Greg Ward | e807e57 | 2002-07-04 14:51:49 +0000 | [diff] [blame] | 258 | Reformat the single paragraph in 'text' so it fits in lines of |
| 259 | no more than 'self.width' columns, and return a list of wrapped |
| 260 | lines. Tabs in 'text' are expanded with string.expandtabs(), |
| 261 | and all other whitespace characters (including newline) are |
| 262 | converted to space. |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 263 | """ |
| 264 | text = self._munge_whitespace(text) |
Guido van Rossum | eb287a2 | 2002-10-02 15:47:32 +0000 | [diff] [blame] | 265 | indent = self.initial_indent |
| 266 | if len(text) + len(indent) <= self.width: |
| 267 | return [indent + text] |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 268 | chunks = self._split(text) |
Greg Ward | 62e4f3b | 2002-06-07 21:56:16 +0000 | [diff] [blame] | 269 | if self.fix_sentence_endings: |
| 270 | self._fix_sentence_endings(chunks) |
Greg Ward | d34c959 | 2002-06-10 20:26:02 +0000 | [diff] [blame] | 271 | return self._wrap_chunks(chunks) |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 272 | |
Greg Ward | 62080be | 2002-06-10 21:37:12 +0000 | [diff] [blame] | 273 | def fill(self, text): |
| 274 | """fill(text : string) -> string |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 275 | |
Greg Ward | e807e57 | 2002-07-04 14:51:49 +0000 | [diff] [blame] | 276 | Reformat the single paragraph in 'text' to fit in lines of no |
| 277 | more than 'self.width' columns, and return a new string |
| 278 | containing the entire wrapped paragraph. |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 279 | """ |
Greg Ward | 62080be | 2002-06-10 21:37:12 +0000 | [diff] [blame] | 280 | return "\n".join(self.wrap(text)) |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 281 | |
| 282 | |
Greg Ward | e807e57 | 2002-07-04 14:51:49 +0000 | [diff] [blame] | 283 | # -- Convenience interface --------------------------------------------- |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 284 | |
Greg Ward | cf02ac6 | 2002-06-10 20:36:07 +0000 | [diff] [blame] | 285 | def wrap(text, width=70, **kwargs): |
Greg Ward | e807e57 | 2002-07-04 14:51:49 +0000 | [diff] [blame] | 286 | """Wrap a single paragraph of text, returning a list of wrapped lines. |
| 287 | |
| 288 | Reformat the single paragraph in 'text' so it fits in lines of no |
| 289 | more than 'width' columns, and return a list of wrapped lines. By |
| 290 | default, tabs in 'text' are expanded with string.expandtabs(), and |
| 291 | all other whitespace characters (including newline) are converted to |
| 292 | space. See TextWrapper class for available keyword args to customize |
| 293 | wrapping behaviour. |
| 294 | """ |
Greg Ward | cf02ac6 | 2002-06-10 20:36:07 +0000 | [diff] [blame] | 295 | w = TextWrapper(width=width, **kwargs) |
| 296 | return w.wrap(text) |
Greg Ward | 0093582 | 2002-06-07 21:43:37 +0000 | [diff] [blame] | 297 | |
Greg Ward | 62080be | 2002-06-10 21:37:12 +0000 | [diff] [blame] | 298 | def fill(text, width=70, **kwargs): |
Greg Ward | e807e57 | 2002-07-04 14:51:49 +0000 | [diff] [blame] | 299 | """Fill a single paragraph of text, returning a new string. |
| 300 | |
| 301 | Reformat the single paragraph in 'text' to fit in lines of no more |
| 302 | than 'width' columns, and return a new string containing the entire |
| 303 | wrapped paragraph. As with wrap(), tabs are expanded and other |
| 304 | whitespace characters converted to space. See TextWrapper class for |
| 305 | available keyword args to customize wrapping behaviour. |
| 306 | """ |
Greg Ward | cf02ac6 | 2002-06-10 20:36:07 +0000 | [diff] [blame] | 307 | w = TextWrapper(width=width, **kwargs) |
Greg Ward | 62080be | 2002-06-10 21:37:12 +0000 | [diff] [blame] | 308 | return w.fill(text) |
Greg Ward | 478cd48 | 2003-05-08 01:58:05 +0000 | [diff] [blame] | 309 | |
| 310 | |
| 311 | # -- Loosely related functionality ------------------------------------- |
| 312 | |
| 313 | def dedent(text): |
| 314 | """dedent(text : string) -> string |
| 315 | |
| 316 | Remove any whitespace than can be uniformly removed from the left |
| 317 | of every line in `text`. |
| 318 | |
| 319 | This can be used e.g. to make triple-quoted strings line up with |
| 320 | the left edge of screen/whatever, while still presenting it in the |
| 321 | source code in indented form. |
| 322 | |
| 323 | For example: |
| 324 | |
| 325 | def test(): |
| 326 | # end first line with \ to avoid the empty line! |
| 327 | s = '''\ |
Greg Ward | 86e1790 | 2003-05-08 02:12:35 +0000 | [diff] [blame] | 328 | hello |
| 329 | world |
Greg Ward | 478cd48 | 2003-05-08 01:58:05 +0000 | [diff] [blame] | 330 | ''' |
Greg Ward | 86e1790 | 2003-05-08 02:12:35 +0000 | [diff] [blame] | 331 | print repr(s) # prints ' hello\n world\n ' |
| 332 | print repr(dedent(s)) # prints 'hello\n world\n' |
Greg Ward | 478cd48 | 2003-05-08 01:58:05 +0000 | [diff] [blame] | 333 | """ |
| 334 | lines = text.expandtabs().split('\n') |
| 335 | margin = None |
| 336 | for line in lines: |
Greg Ward | 2557100 | 2003-05-08 02:02:50 +0000 | [diff] [blame] | 337 | content = line.lstrip() |
Greg Ward | 478cd48 | 2003-05-08 01:58:05 +0000 | [diff] [blame] | 338 | if not content: |
| 339 | continue |
Greg Ward | 2557100 | 2003-05-08 02:02:50 +0000 | [diff] [blame] | 340 | indent = len(line) - len(content) |
Greg Ward | 478cd48 | 2003-05-08 01:58:05 +0000 | [diff] [blame] | 341 | if margin is None: |
| 342 | margin = indent |
| 343 | else: |
| 344 | margin = min(margin, indent) |
| 345 | |
Greg Ward | 2557100 | 2003-05-08 02:02:50 +0000 | [diff] [blame] | 346 | if margin is not None and margin > 0: |
Greg Ward | 478cd48 | 2003-05-08 01:58:05 +0000 | [diff] [blame] | 347 | for i in range(len(lines)): |
| 348 | lines[i] = lines[i][margin:] |
| 349 | |
| 350 | return '\n'.join(lines) |