How do I wrap text?

Jeff Bauer jbauer at rubic.com
Sat Feb 5 21:19:25 EST 2000


Doug Sauder wrote:
> It seems like there should be some very easy way 
> to wrap long lines of text.

Doug,

Use the standard formatter.py module.  Below is an
example of how to use it.

Jeff Bauer
Rubicon Research
---
#!/usr/bin/env python
# SimpleWriter.py

import os, sys, string
from formatter import NullWriter

class SimpleWriter(NullWriter):
    """
    SimpleWriter is functionally equivalent to DumbWriter.  It
    stores the data as a list of self.text lines rather than
    writing the text to a file.
    """
    def __init__(self, maxcol=72):
        self.text = []
        self.__current_line = []
        self.maxcol = maxcol
        NullWriter.__init__(self)
        self.reset()

    def reset(self):
        self.col = 0
        self.atbreak = 0

    def send_paragraph(self, blankline):
        self.text.append(string.join(self.__current_line, ''))
        self.__current_line = []
        for i in range(blankline):
            self.text.append('')
        self.col = 0
        self.atbreak = 0

    def send_line_break(self):
        self.text.append(string.join(self.__current_line, ''))
        self.__current_line = []
        self.col = 0
        self.atbreak = 0

    def send_hor_rule(self, *args, **kw):
        self.text.append(string.join(self.__current_line, ''))
        self.__current_line = []
        self.text.append('-'*self.maxcol)
        self.col = 0
        self.atbreak = 0

    def send_literal_data(self, data):
        self.__current_line.append(data)
        i = string.rfind(data, '\n')
        if i >= 0:
            self.col = 0
            data = data[i+1:]
        data = string.expandtabs(data)
        self.col = self.col + len(data)
        self.atbreak = 0

    def send_flowing_data(self, data):
        if not data: 
            return
        atbreak = self.atbreak or data[0] in string.whitespace
        col = self.col
        maxcol = self.maxcol
        for word in string.split(data):
            if atbreak:
                if col + len(word) >= maxcol:
                    self.text.append(string.join(self.__current_line,
''))
                    self.__current_line = []
                    col = 0
                else:
                    self.__current_line.append(' ')
                    col = col + 1
            self.__current_line.append(word)
            col = col + len(word)
            atbreak = 1
        self.col = col
        self.atbreak = data[-1] in string.whitespace


if __name__ == '__main__':
    spam = \
"""
Python is an interpreted, interactive, object-oriented programming \
language. It is often compared to Tcl, Perl, Scheme or Java. \

Python combines remarkable power with very clear syntax. It has \
modules, classes, exceptions, very high level dynamic data types, and \
dynamic typing. There are interfaces to many system calls and libraries,
\
as well as to various windowing systems (X11, Motif, Tk, Mac, MFC). \
New built-in modules are easily written in in C or C++. Python is also \
usable as an extension language for applications that need a \
programmable interface. 
"""

    from formatter import AbstractFormatter
    w = SimpleWriter(40)
    f = AbstractFormatter(w)

    for s in string.split(spam, "\n"):
        f.add_flowing_data(s)
        f.end_paragraph(1)
    f.end_paragraph(0)

    print string.join(w.text, '\n')




More information about the Python-list mailing list