Modify

Opened 17 years ago

Closed 17 years ago

#1123 closed enhancement (fixed)

wrap long lines

Reported by: Rainer Sokoll Owned by: Bas van der Vlies
Priority: normal Component: EmailtoTracScript
Severity: normal Keywords: email2trac line breaks
Cc: Trac Release: 0.10

Description

Could you add a feature that wraps long lines in incoming emails for better reading in trac?

Attachments (3)

trac.png (10.7 KB) - added by Rainer Sokoll 17 years ago.
with_replace_whitespace.png (15.8 KB) - added by Rainer Sokoll 17 years ago.
email2trac.py.in.diff (909 bytes) - added by Rainer Sokoll 17 years ago.

Download all attachments as: .zip

Change History (22)

comment:1 Changed 17 years ago by Bas van der Vlies

Status: newassigned

comment:2 Changed 17 years ago by Rainer Sokoll

Resolution: fixed
Status: assignedclosed
  • import textwrap
  • Change line 552 body_text = part.get_payload(decode=1) to body_text = textwrap.fill(part.get_payload(decode=1), 70)

comment:3 Changed 17 years ago by Bas van der Vlies

Resolution: fixed
Status: closedreopened

Thanks a lot ;-). I will make it an option. How many chars can be put on a line. I will close it when i have applied the patch to trunk. Just for me a reminder

comment:4 in reply to:  3 Changed 17 years ago by Rainer Sokoll

Replying to bas:

Thanks a lot ;-). I will make it an option. How many chars can be put on a line. I will close it when i have applied the patch to trunk.

Unfortunataly, I was way too fast. A new problem araised: textwrap.fill replaces every whitespace, including newlines, with a space.

For example:

word word
word


word word

will become

word word word word word

Maybe I can come up with a solution tomorrow.

comment:5 Changed 17 years ago by anonymous

Then this is the solution:

print textwrap.fill(a, width=70, replace_whitespace=False)

Can you test it

comment:6 in reply to:  5 Changed 17 years ago by Rainer Sokoll

Replying to anonymous:

Then this is the solution:

print textwrap.fill(a, width=70, replace_whitespace=False)

Can you test it

textwrap eats empty lines (\n\n). Also, its output is somewhat unpredictable. (I'm a bloody novice to python, so hopefully noone gets offended). At he moment, I have the feeling that textwrap is not the right tool for our problem.

comment:7 Changed 17 years ago by anonymous

It is the right tool. In python 2.4 it does not eat up the newlines. Maybe we have to set an option for python 2.3

comment:8 in reply to:  7 Changed 17 years ago by Rainer Sokoll

Replying to anonymous:

It is the right tool. In python 2.4 it does not eat up the newlines. Maybe we have to set an option for python 2.3

You are correct. In my tests, I had this:

from textwrap import TextWrapper
rainer = TextWrapper()
rainer.replace_whitespace = False
rainer.width = 70
s = 'some long text with line\nbreaks'
print rainer.fill(s)

which seems to work slightly different from your (much easier) solution. BTW: I'm running python 2.4.1

comment:9 Changed 17 years ago by Bas van der Vlies

Rainer i have just applied a patch to trunk that implements the textwrap functionality:

email2trac.conf:

use_textwrap: 72

can you test it? It will wrap line longer then 72 chars or something else if you want.

Changed 17 years ago by Rainer Sokoll

Attachment: trac.png added

comment:10 Changed 17 years ago by Rainer Sokoll

As you can see here, the 2nd long line has extra linebreaks, I do not know why :-(

comment:11 Changed 17 years ago by Bas van der Vlies

Maybe it does not reset the count and is it a bug in textwrap.

comment:12 Changed 17 years ago by Bas van der Vlies

Rainer,

Can you replace:

                    body_text = textwrap.fill( body_text,
                                               width = self.USE_TEXTWRAP, 
                                               replace_whitespace = False )

with

                    body_text = textwrap.fill( body_text,
                                               width = self.USE_TEXTWRAP)

Changed 17 years ago by Rainer Sokoll

Attachment: with_replace_whitespace.png added

comment:13 Changed 17 years ago by r.sokoll@…

With "replace_whitespace = False" see here Without, see here

The original mail was:

  • long line
  • line break
  • long line
  • line break
  • long line

I'm afraid textwrap.py is broken. I have "1.35.4.1 2005/03/05 02:38:32 gward Exp"

comment:14 Changed 17 years ago by Bas van der Vlies

Thanks, just read the docs and it only works on paragraphs ;-( So it see the whole text as one paragraph and it does not reset the counters.

comment:15 Changed 17 years ago by Rainer Sokoll

A friend of mine sent me this snipplet. As mentioned before, I am a newbie to python, and this code is far beyond my skills, but maybe you can get valuable input.

import textwrap                                                                                                                 
                                                                                                                                
LINESEPARATOR = '\n'                                                                                                            
                                                                                                                                
def breakString(str='', width=30):                                                                                              
   return LINESEPARATOR.join(textwrap.fill(s,width) for s in str.split(LINESEPARATOR))                                                                                                       
                                                                                                                                
if __name__ == '__main__':                                                                                                      
   testString = 'asdfghjkl\nasdfghjkl\nasdfghjkl asdfghjkl\n\nasdfghjkl'                                                        
   print breakString(testString)                                                                                                

Changed 17 years ago by Rainer Sokoll

Attachment: email2trac.py.in.diff added

comment:16 Changed 17 years ago by Rainer Sokoll

The attached patch seems to solve the problem. Also fixes http://www.trac-hacks.org/ticket/1295 ;-)

comment:17 Changed 17 years ago by Bas van der Vlies

Thanks i will apply it to trunk and close the ticket if i have actually did it ;-),

comment:18 Changed 17 years ago by Bas van der Vlies

Applied to trunk. Thanks a lot

comment:19 Changed 17 years ago by Bas van der Vlies

Resolution: fixed
Status: reopenedclosed

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Bas van der Vlies.
The resolution will be deleted. Next status will be 'reopened'.

Add Comment


E-mail address and name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.