diff options
author | Christopher Lenz <cmlenz@gmail.com> | 2008-02-01 15:23:13 +0000 |
---|---|---|
committer | Christopher Lenz <cmlenz@gmail.com> | 2008-02-01 15:23:13 +0000 |
commit | 34c5c00801ce067609efc60dfd1920f17865149d (patch) | |
tree | e9ce53b04d19c6f51da8563275834c70c5d47141 /babel | |
parent | 6dcb4488778a2bb9b89b5e09f830a430f5541779 (diff) | |
download | babel-34c5c00801ce067609efc60dfd1920f17865149d.tar.gz |
Fix for #79 (location lines wrapping at hyphens).
Diffstat (limited to 'babel')
-rwxr-xr-x | babel/messages/frontend.py | 1 | ||||
-rw-r--r-- | babel/messages/pofile.py | 9 | ||||
-rw-r--r-- | babel/messages/tests/pofile.py | 15 | ||||
-rw-r--r-- | babel/util.py | 30 |
4 files changed, 48 insertions, 7 deletions
diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py index 9ebb64a..d01fdef 100755 --- a/babel/messages/frontend.py +++ b/babel/messages/frontend.py @@ -28,7 +28,6 @@ import shutil from StringIO import StringIO import sys import tempfile -import textwrap from babel import __version__ as VERSION from babel import Locale, localedata diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py index 6702ace..2b041a5 100644 --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -25,11 +25,10 @@ try: set except NameError: from sets import Set as set -from textwrap import wrap from babel import __version__ as VERSION from babel.messages.catalog import Catalog, Message -from babel.util import LOCALTZ +from babel.util import wraptext, LOCALTZ __all__ = ['read_po', 'write_po'] __docformat__ = 'restructuredtext en' @@ -370,7 +369,7 @@ def write_po(fileobj, catalog, width=76, no_location=False, omit_header=False, def _write_comment(comment, prefix=''): lines = comment if width and width > 0: - lines = wrap(comment, width, break_long_words=False) + lines = wraptext(comment, width) for line in lines: _write('#%s %s\n' % (prefix, line.strip())) @@ -404,8 +403,8 @@ def write_po(fileobj, catalog, width=76, no_location=False, omit_header=False, if width and width > 0: lines = [] for line in comment_header.splitlines(): - lines += wrap(line, width=width, subsequent_indent='# ', - break_long_words=False) + lines += wraptext(line, width=width, + subsequent_indent='# ') comment_header = u'\n'.join(lines) + u'\n' _write(comment_header) diff --git a/babel/messages/tests/pofile.py b/babel/messages/tests/pofile.py index 822a946..11007e2 100644 --- a/babel/messages/tests/pofile.py +++ b/babel/messages/tests/pofile.py @@ -220,6 +220,21 @@ msgstr ""''', buf.getvalue().strip()) # #, fuzzy''', '\n'.join(buf.getvalue().splitlines()[:7])) + def test_wrap_locations_with_hyphens(self): + catalog = Catalog() + catalog.add(u'foo', locations=[ + ('doupy/templates/base/navmenu.inc.html.py', 60) + ]) + catalog.add(u'foo', locations=[ + ('doupy/templates/job-offers/helpers.html', 22) + ]) + buf = StringIO() + pofile.write_po(buf, catalog, omit_header=True) + self.assertEqual('''#: doupy/templates/base/navmenu.inc.html.py:60 +#: doupy/templates/job-offers/helpers.html:22 +msgid "foo" +msgstr ""''', buf.getvalue().strip()) + def test_pot_with_translator_comments(self): catalog = Catalog() catalog.add(u'foo', locations=[('main.py', 1)], 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. |