CMS 3D CMS Logo

Functions

wordWrappers Namespace Reference

Functions

def wrap_always
def wrap_onspace
def wrap_onspace_strict

Function Documentation

def wordWrappers::wrap_always (   text,
  width 
)
A simple word-wrap function that wraps text on exactly width characters.
It doesn't split the text in words.

Definition at line 5 of file wordWrappers.py.

00006                             :
00007     """
00008     A simple word-wrap function that wraps text on exactly width characters.
00009     It doesn't split the text in words.
00010     """
00011     return '\n'.join([ text[width*i:width*(i+1)] \
00012                        for i in xrange(int(math.ceil(1.*len(text)/width))) ])

def wordWrappers::wrap_onspace (   text,
  width 
)
A word-wrap function that preserves existing line breaks
and most spaces in the text. Expects that existing line
breaks are posix newlines (\n).

Definition at line 13 of file wordWrappers.py.

00014                             :
00015     """
00016     A word-wrap function that preserves existing line breaks
00017     and most spaces in the text. Expects that existing line
00018     breaks are posix newlines (\n).
00019     """
00020     return reduce(lambda line, word, width=width: '%s%s%s' %
00021                   (line,
00022                    ' \n'[(len(line[line.rfind('\n')+1:])
00023                           + len(word.split('\n',1)[0]
00024                                 ) >= width)],
00025                    word),
00026                   text.split(' ')
                  )
def wordWrappers::wrap_onspace_strict (   text,
  width 
)
Similar to wrap_onspace, but enforces the width constraint:
words longer than width are split.

Definition at line 27 of file wordWrappers.py.

00028                                     :
00029     """
00030     Similar to wrap_onspace, but enforces the width constraint:
00031     words longer than width are split.
00032     """
00033     wordRegex = re.compile(r'\S{'+str(width)+r',}')
00034     return wrap_onspace(wordRegex.sub(lambda m: wrap_always(m.group(),width),text),width)