File: wordwrap.py

package info (click to toggle)
pythoncard 0.8.2-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 8,452 kB
  • sloc: python: 56,787; makefile: 56; sh: 22
file content (40 lines) | stat: -rw-r--r-- 1,051 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# word_wrap.py
#
# this was originally based on some code I found on the net, but I
# couldn't get that to work so I just wrote it afresh.

"""
__version__ = "$Revision: 1.4 $"
__date__ = "$Date: 2002/12/27 17:26:40 $"
"""

def wrap_string(str, max, para = "\n\n"):
    paras = str.split(para)
    outStr = ""
    
    for paragraph in paras:
        paragraph = paragraph.replace("\n", " ")
        words = paragraph.split()
        outLine = ""
        lineCount = wordCount = 0
        
        for i in range(len(words)):
            if (len(outLine) + len(words[i])) > max:
                if lineCount:
                    outStr += "\n"
                outStr += outLine
                outLine = words[i]
                lineCount += 1
                wordCount = 1
            else:
                if wordCount:
                    outLine += " "
                outLine +=  words[i]
                wordCount += 1

        if lineCount:
            outStr += "\n"
        outStr += outLine
        outStr += para
        
    return outStr