wordwrap.py
Go to the documentation of this file.
00001 """
00002 WordWrapping
00003 """
00004 
00005 
00006 import os, sys, string, time, getopt
00007 import re
00008 
00009 def WordWrap(text, cols=70, detect_paragraphs = 0, is_header = 0):
00010   text =  string.replace(text,"\r\n", "\n") # remove CRLF
00011   def nlrepl(matchobj):
00012     if matchobj.group(1) != ' ' and matchobj.group(2) != ' ':
00013       repl_with = ' '
00014     else:
00015       repl_with = ''
00016 
00017     return matchobj.group(1) + repl_with + matchobj.group(2)
00018 
00019   if detect_paragraphs:
00020     text = re.sub("([^\n])\n([^\n])",nlrepl,text)
00021 
00022   body = []
00023   i = 0
00024   j = 0
00025   ltext = len(text)
00026 
00027   while i<ltext:
00028     if i+cols < ltext:
00029       r = string.find(text, "\n", i, i+cols)
00030       j = r
00031       if r == -1:
00032         j = string.rfind(text, " ", i, i+cols)
00033         if j == -1:
00034           r = string.find(text, "\n", i+cols)
00035           if r == -1: r = ltext
00036           j = string.find(text, " ", i+cols)
00037           if j == -1: j = ltext
00038           j = min(j, r)
00039     else:
00040       j = ltext
00041 
00042     body.append(string.strip(text[i:j]))
00043     i = j+1
00044 
00045   if is_header:
00046     body = string.join(body, "\n ")
00047   else:
00048     body = string.join(body, "\n")
00049   return body
00050 


pyclearsilver
Author(s): Scott Hassan/hassan@willowgarage.com
autogenerated on Sat Dec 28 2013 17:47:30