summaryrefslogtreecommitdiff
path: root/babel/util.py
diff options
context:
space:
mode:
authorChristopher Lenz <cmlenz@gmail.com>2008-02-01 15:23:13 +0000
committerChristopher Lenz <cmlenz@gmail.com>2008-02-01 15:23:13 +0000
commit34c5c00801ce067609efc60dfd1920f17865149d (patch)
treee9ce53b04d19c6f51da8563275834c70c5d47141 /babel/util.py
parent6dcb4488778a2bb9b89b5e09f830a430f5541779 (diff)
downloadbabel-34c5c00801ce067609efc60dfd1920f17865149d.tar.gz
Fix for #79 (location lines wrapping at hyphens).
Diffstat (limited to 'babel/util.py')
-rw-r--r--babel/util.py30
1 files changed, 29 insertions, 1 deletions
diff --git a/babel/util.py b/babel/util.py
index 3c00e97..2ef92d6 100644
--- a/babel/util.py
+++ b/babel/util.py
@@ -22,9 +22,11 @@ try:
set
except NameError:
from sets import Set as set
+import textwrap
import time
-__all__ = ['distinct', 'pathmatch', 'relpath', 'odict', 'UTC', 'LOCALTZ']
+__all__ = ['distinct', 'pathmatch', 'relpath', 'wraptext', 'odict', 'UTC',
+ 'LOCALTZ']
__docformat__ = 'restructuredtext en'
def distinct(iterable):
@@ -145,6 +147,32 @@ def pathmatch(pattern, filename):
return match is not None
+class TextWrapper(textwrap.TextWrapper):
+ wordsep_re = re.compile(
+ r'(\s+|' # any whitespace
+ r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))' # em-dash
+ )
+
+
+def wraptext(text, width=70, initial_indent='', subsequent_indent=''):
+ """Simple wrapper around the ``textwrap.wrap`` function in the standard
+ library. This version does not wrap lines on hyphens in words.
+
+ :param text: the text to wrap
+ :param width: the maximum line width
+ :param initial_indent: string that will be prepended to the first line of
+ wrapped output
+ :param subsequent_indent: string that will be prepended to all lines save
+ the first of wrapped output
+ :return: a list of lines
+ :rtype: `list`
+ """
+ wrapper = TextWrapper(width=width, initial_indent=initial_indent,
+ subsequent_indent=subsequent_indent,
+ break_long_words=False)
+ return wrapper.wrap(text)
+
+
class odict(dict):
"""Ordered dict implementation.