diff options
Diffstat (limited to 'Lib/textwrap.py')
-rw-r--r-- | Lib/textwrap.py | 36 |
1 files changed, 30 insertions, 6 deletions
diff --git a/Lib/textwrap.py b/Lib/textwrap.py index dfb4005..7024d4d 100644 --- a/Lib/textwrap.py +++ b/Lib/textwrap.py @@ -5,9 +5,9 @@ # Copyright (C) 2002, 2003 Python Software Foundation. # Written by Greg Ward <gward@python.net> -import string, re +import re -__all__ = ['TextWrapper', 'wrap', 'fill', 'dedent'] +__all__ = ['TextWrapper', 'wrap', 'fill', 'dedent', 'indent'] # Hardcode the recognized whitespace characters to the US-ASCII # whitespace characters. The main reason for doing this is that in @@ -39,8 +39,11 @@ class TextWrapper: of wrapped output; also counts towards each line's width. expand_tabs (default: true) Expand tabs in input text to spaces before further processing. - Each tab will become 1 .. 8 spaces, depending on its position in - its line. If false, each tab is treated as a single character. + Each tab will become 0 .. 'tabsize' spaces, depending on its position + in its line. If false, each tab is treated as a single character. + tabsize (default: 8) + Expand tabs in input text to 0 .. 'tabsize' spaces, unless + 'expand_tabs' is false. replace_whitespace (default: true) Replace all whitespace characters in the input text by spaces after tab expansion. Note that if expand_tabs is false and @@ -100,7 +103,8 @@ class TextWrapper: fix_sentence_endings=False, break_long_words=True, drop_whitespace=True, - break_on_hyphens=True): + break_on_hyphens=True, + tabsize=8): self.width = width self.initial_indent = initial_indent self.subsequent_indent = subsequent_indent @@ -110,6 +114,7 @@ class TextWrapper: self.break_long_words = break_long_words self.drop_whitespace = drop_whitespace self.break_on_hyphens = break_on_hyphens + self.tabsize = tabsize # -- Private methods ----------------------------------------------- @@ -123,7 +128,7 @@ class TextWrapper: becomes " foo bar baz". """ if self.expand_tabs: - text = text.expandtabs() + text = text.expandtabs(self.tabsize) if self.replace_whitespace: text = text.translate(self.unicode_whitespace_trans) return text @@ -381,6 +386,25 @@ def dedent(text): text = re.sub(r'(?m)^' + margin, '', text) return text + +def indent(text, prefix, predicate=None): + """Adds 'prefix' to the beginning of selected lines in 'text'. + + If 'predicate' is provided, 'prefix' will only be added to the lines + where 'predicate(line)' is True. If 'predicate' is not provided, + it will default to adding 'prefix' to all non-empty lines that do not + consist solely of whitespace characters. + """ + if predicate is None: + def predicate(line): + return line.strip() + + def prefixed_lines(): + for line in text.splitlines(True): + yield (prefix + line if predicate(line) else line) + return ''.join(prefixed_lines()) + + if __name__ == "__main__": #print dedent("\tfoo\n\tbar") #print dedent(" \thello there\n \t how are you?") |