Feature Requests item #1702681, was opened at 2007-04-18 01:40
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1702681&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: None
Group: None
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Matt Kraai (kraai)
>Assigned to: Greg Ward (gward)
Summary: Prevent textwrap from breaking words at hyphens

Initial Comment:
I'm using textwrap to wrap text that contains URLs that contain hyphens.  When 
it wraps these URLs after a hyphen, it breaks the URL.  I wish there was a way 
to prevent it from doing so.

----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2007-04-19 01:37

Message:
Logged In: YES 
user_id=80475
Originator: NO

FWIW, one workaround is to "monkey patch" the module:

  textwrap.TextWrapper.wordsep_re =
re.compile(r'(\s+|(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))')

Another workaround is to "hide" the hyphens during wrapping and then
restore them.
  
  def myfill(text, width=70, **kwargs):
     althyphen = chr(127)
     text = text.replace('-', althyphen)
     result = wrap(text, width, **kwargs)
     return result.replace(althyphen, '-')

That being said, I'm +1 on adding a keyword argument treating hyphens as
non-breaking.


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1702681&group_id=5470
_______________________________________________
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to