[reportlab-users] bug - trying to preserve leading whitespace in textLines method

Dave Rogers reportlab-users@reportlab.com
Thu, 31 Oct 2002 16:39:53 -0500


--------------D071BECC3669294B92DC55E2
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hi,

I'm new to this list, but I've been using report lab for 4-5 months.

I think I have found a bug.

Version: Release 1.15
File:  pdfgen/textobject.py
Class: PDFTextObject
Method: textLines

The doc string says:

        """prints multi-line or newlined strings, moving down.  One
        comon use is to quote a multi-line block in your Python code;
        since this may be indented, by default it trims whitespace
        off each line and from the beginning; set trim=0 to preserve
        whitespace."""

My problem is that it is trimming off leading whitespace even when I set trim to 0.

I changed the code to this:

    def textLines(self, stuff, trim=1):
        """prints multi-line or newlined strings, moving down.  One
        comon use is to quote a multi-line block in your Python code;
        since this may be indented, by default it trims whitespace
        off each line and from the beginning; set trim=0 to preserve
        whitespace."""
        if type(stuff) == StringType:
            if trim==1:
              lines = string.split(string.strip(stuff), '\n')
            else:
              lines = string.split(stuff, '\n') # preserve leading white space dr 10-31-02
            if trim==1:
                lines = map(string.strip,lines)
....

Basically I don't call string.strip if trim is not 1 when you split on \n for the lines list.

Maybe I'm missing something here, but it seems like leading whitespace would be included in the preservation of whitespace for a trim of 0.  I need the whitespace for some padding on the first row of ad hoc tables I create.

Regards,
--
Dave Rogers
DaveRogers@StockAlerts.com
http://StockAlerts.com


--------------D071BECC3669294B92DC55E2
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
Hi,
<p>I'm new to this list, but I've been using report lab for 4-5 months.
<p>I think I have found a bug.
<p>Version: Release 1.15
<br>File:&nbsp; pdfgen/textobject.py
<br>Class: PDFTextObject
<br>Method: textLines&nbsp;
<p>The doc string says:
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; """prints multi-line or newlined
strings, moving down.&nbsp; One
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; comon use is to quote a
multi-line block in your Python code;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; since this may be indented,
by default it trims whitespace
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; off each line and from the
beginning; set trim=0 to preserve
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; whitespace."""
<p>My problem is that it is trimming off leading whitespace even when I
set trim to 0.
<p>I changed the code to this:
<p><tt>&nbsp;&nbsp;&nbsp; def textLines(self, stuff, trim=1):</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; """prints multi-line
or newlined strings, moving down.&nbsp; One</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; comon use is to quote
a multi-line block in your Python code;</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; since this may be indented,
by default it trims whitespace</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; off each line and from
the beginning; set trim=0 to preserve</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; whitespace."""</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if type(stuff) == StringType:</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if trim==1:</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
lines = string.split(string.strip(stuff), '\n')</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
else:</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
lines = string.split(stuff, '\n') # preserve leading white space dr 10-31-02</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if trim==1:</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
lines = map(string.strip,lines)</tt>
<br><tt>....</tt>
<p>Basically I don't call string.strip if trim is not 1 when you split
on \n for the lines list.
<p>Maybe I'm missing something here, but it seems like leading whitespace
would be included in the preservation of whitespace for a trim of 0.&nbsp;
I need the whitespace for some padding on the first row of ad hoc tables
I create.
<p>Regards,
<br>--
<br>Dave Rogers
<br>DaveRogers@StockAlerts.com
<br><A HREF="http://StockAlerts.com">http://StockAlerts.com</A>
<br>&nbsp;</html>

--------------D071BECC3669294B92DC55E2--