Modify

Opened 8 years ago

Closed 7 years ago

#1123 closed enhancement (fixed)

wrap long lines

Reported by: r.sokoll@… Owned by: bas
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 r.sokoll@… 7 years ago.
with_replace_whitespace.png (15.8 KB) - added by r.sokoll@… 7 years ago.
email2trac.py.in.diff (909 bytes) - added by r.sokoll@… 7 years ago.

Download all attachments as: .zip

Change History (22)

comment:1 Changed 8 years ago by bas

  • Status changed from new to assigned

comment:2 Changed 7 years ago by r.sokoll@…

  • Resolution set to fixed
  • Status changed from assigned to closed
  • 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 follow-up: Changed 7 years ago by bas

  • Resolution fixed deleted
  • Status changed from closed to reopened

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 7 years ago by r.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 follow-up: Changed 7 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 7 years ago by r.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 follow-up: Changed 7 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 7 years ago by r.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 7 years ago by bas

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 7 years ago by r.sokoll@…

comment:10 Changed 7 years ago by r.sokoll@…

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

comment:11 Changed 7 years ago by bas

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

comment:12 Changed 7 years ago by bas

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 7 years ago by r.sokoll@…

comment:13 Changed 7 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 7 years ago by bas

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 7 years ago by r.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 7 years ago by r.sokoll@…

comment:16 Changed 7 years ago by r.sokoll@…

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

comment:17 Changed 7 years ago by bas

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

comment:18 Changed 7 years ago by bas

Applied to trunk. Thanks a lot

comment:19 Changed 7 years ago by bas

  • Resolution set to fixed
  • Status changed from reopened to closed

Add Comment

Modify Ticket

Action
as closed .
as The resolution will be set. Next status will be 'closed'.
to The owner will be changed from bas. Next status will be 'closed'.
The resolution will be deleted. Next status will be 'reopened'.
Author


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

 
Note: See TracTickets for help on using tickets.