LessDumbWriter

ben at co.and.co ben at co.and.co
Fri Mar 24 21:44:14 CET 2000


I've been having fun with my first python-program ever.
As a side-effect, I hacked "formatter.DumbWriter" till I got something
that should be able to render html-lists.

Have fun.

P.S. Can somebody please change the tutorial.
     The first line should read: "You can't mix spaces and tabs in
     python", in Times 20 pt bold, and the next line should read
     "This will most likely happen if you cut and paste in your editor"
     in a bigger font. Would have saved me 2 hours.


#!/usr/bin/env python

from formatter import *

class LessDumbWriter(NullWriter):

    def __init__(self, file=None, maxcol=72, margin_width=4, margin_char=' '):
        self.file = file or sys.stdout
        self.maxcol = maxcol
        self.m_width = margin_width
        self.m_char = margin_char
        NullWriter.__init__(self)
        self.reset()

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

    def new_line(self, no_lines=1):
        self.file.write('\n' * no_lines)
        self.col = 0
        self.atbreak = 0
        self.atmargin = 1

    def normal_margin(self):
        if self.m_level:
            self.file.write(self.m_char * (self.m_width * self.m_level))
        self.col = self.m_width * self.m_level
        self.atmargin = 0

    def send_paragraph(self, blankline):
        self.new_line(blankline)

    def send_line_break(self):
        self.new_line()

    def send_hor_rule(self, *args, **kw):
        self.new_line()
        self.normal_margin()
        self.file.write('-' * self.maxcol)
        self.new_line()

    def send_literal_data(self, data):
        if not data: return
        if self.atmargin: self.normal_margin()
        lines = string.split(string.expandtabs(data), '\n')
        for line in lines[:-2]:
            self.file.write(line)
            self.new_line()
            self.normal_margin()
        self.file.write(lines[-2])
        if data[-1] == '\n':
            self.new_line()
        else:
            self.col = self.col + len(lines[-2])
        
    def send_flowing_data(self, data):
        if not data: return
        self.atbreak = self.atbreak or data[0] in string.whitespace
        for word in string.split(data):
            if self.atbreak:
                if self.col + len(word) >= self.maxcol:
                    self.new_line()
                else:
                    self.file.write(' ')
                    self.col = self.col + 1
            if self.atmargin:
                self.normal_margin()
            self.file.write(word)
            self.col = self.col + len(word)
            self.atbreak = 1
        self.atbreak = data[-1] in string.whitespace

    def new_margin(self, which_margin, level):
        self.m_level = level
        if self.m_level < 0: self.m_level = 0

    def send_label_data(self, data):
        if not self.atmargin:
            self.new_line()
        if self.m_level > 1:
            level = self.m_level - 1
            self.file.write(self.m_char * (self.m_width * level))
            self.col = level * self.m_width
        l = len(data)
        if l > self.m_width:
            self.file.write(data)
            self.col = self.col + l
        else:
            if l < self.m_width:
                self.file.write(string.rjust(data, self.m_width - 1))
                self.file.write(' ')
            else:
                self.file.write(data)
            self.col = self.col + self.m_width
        self.atmargin = 0

Greetings,
-- 
ben . de . rydt at pandora . be ------------------ your comments
http://users.pandora.be/bdr/ ------- inl. IPv6, Linux en Pandora




More information about the Python-list mailing list