Skip to content

Commit

Permalink
new: added a dedent() function.
Browse files Browse the repository at this point in the history
  • Loading branch information
vaab committed Feb 4, 2015
1 parent 2ed6abb commit af7a773
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ You can easily indent text with::
|


dedent
------

You can also dedent text::

>>> print(txt.dedent(
... '''This is a doc
...
... with fancy indentation, that should just work also.
... Without removing too much neither as:
... - more space.'''))
This is a doc
<BLANKLINE>
with fancy indentation, that should just work also.
Without removing too much neither as:
- more space.


paragrap_wrap
-------------

Expand Down
3 changes: 1 addition & 2 deletions src/kids/txt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Package placeholder

from .txt import indent, paragraph_wrap

from .txt import indent, dedent, paragraph_wrap
34 changes: 34 additions & 0 deletions src/kids/txt/txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,40 @@
import re


def dedent(txt):
"""Dedent a txt, tolerating first line not indented.
>>> from __future__ import print_function
Various issues should be tackled:
>>> print(dedent(
... '''This is a doc
...
... with fancy indentation, that should just work also.
... Without removing too much neither as:
... - more space.'''))
This is a doc
<BLANKLINE>
with fancy indentation, that should just work also.
Without removing too much neither as:
- more space.
Note that the first line doesn't have indentation and neither the
second (which is empty).
Of course, ``dedent`` should not fail on empty string neither:
>>> dedent("")
''
"""
if "\n" not in txt:
return txt.lstrip()
first_line, end = txt.split('\n', 1)
return "%s\n%s" % (first_line, textwrap.dedent(end))


## Note that a quite equivalent function was added to textwrap in python 3.3
def indent(text, prefix=" ", first=None):
"""Return text string indented with the given prefix
Expand Down

0 comments on commit af7a773

Please sign in to comment.