home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / checkbox / lib / text.py < prev    next >
Encoding:
Python Source  |  2009-04-27  |  2.0 KB  |  67 lines

  1. #
  2. # This file is part of Checkbox.
  3. #
  4. # Copyright 2008 Canonical Ltd.
  5. #
  6. # Checkbox is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Checkbox is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. def indent(text, count=1, step=2):
  20.     """Intend each line of text by a given step.
  21.  
  22.     Keyword arguments:
  23.     text -- text containing lines to indent
  24.     count -- number of steps to indent (default 1)
  25.     step -- number of spaces per indent (default 2)
  26.     """
  27.  
  28.     indent = ' ' * step
  29.     return "\n".join([indent * count + t for t in text.split("\n")])
  30.  
  31. def wrap(text, limit=72):
  32.     """Wrap text into lines up to limit characters excluding newline.
  33.  
  34.     Keyword arguments:
  35.     text -- text to wrap
  36.     limit -- maximum number of characters per line (default 72)
  37.     """
  38.  
  39.     lines = ['']
  40.     if text:
  41.         current = -1
  42.         inside = False
  43.         for line in text.split("\n"):
  44.             words = line.split()
  45.             if words:
  46.                 inside = True
  47.                 for word in words:
  48.                     increment = len(word) + 1
  49.                     if current + increment > limit:
  50.                         current = -1
  51.                         lines.append('')
  52.                     current += increment
  53.                     lines[-1] += word + ' '
  54.             else:
  55.                 if inside:
  56.                     inside = False
  57.                     lines.append('')
  58.                 lines.append('')
  59.                 current = -1
  60.  
  61.     return "\n".join(lines)
  62.  
  63. def unwrap(text):
  64.     lines = text.split("\n")
  65.     lines = [l.strip() for l in lines]
  66.     return " ".join(lines)
  67.