[Zope] wrapping email at 72 characters ?

Geir Bækholt Geir Bækholt
Thu, 8 Aug 2002 20:57:33 +0200


Hello Casey, 
Thursday, August 8, 2002, 2:43:57 PM, you wrote:
CD> Try this code from my MSWordMunger product (which predates Python 2.0 btw):

thanks .. with a few minor fixes it works perfectly for my needs.
:-)

Most notably : the following line doesn't make any sense:
CD>             if cut == -1: cut = find(remaining, ' ', cut-1)
as cut-1 always will be -2 ...


here is an updated version with support for existing newlines as well:

from string import find, rfind, join

def wordWrap(text, width):
    remaining = text
    wrapped = []

    while len(remaining) > width:
        cut = width
        newline = find(remaining, '\n', 0, cut)
        
        if newline != -1:
            cut = newline
        elif remaining[cut] != ' ':            
            temp = rfind(remaining, ' ', 0, cut-1)
            if temp == -1:temp = find(remaining, ' ', cut-1, len(remaining))
            if temp == -1: temp = len(remaining)
            cut = temp
        wrapped.append(remaining[:cut])
        remaining = remaining[cut+1:]

    if remaining:
        wrapped.append(remaining)

    return join(wrapped, '\n')


thanks a lot , Casey

:-)

posted it to ZopeLabs :
http://www.zopelabs.com/cookbook/1028832719


CD> On Thursday 08 August 2002 07:40 am, Geir Bækholt wrote:
>> Does anyone have a python snippet or something that helps format
>> outgoing emails, so that lines wrap nicely at 72 characters?

-- 
Geir Bækholt                       web-developer
geirh@funcom.com             funcom oslo | webdev-team