Ticket #7027: fix_tag_translation.diff

File fix_tag_translation.diff, 3.1 KB (added by mrts, 16 years ago)
  • django/utils/text.py

     
    196196    return str(ustring_re.sub(fix, s))
    197197javascript_quote = allow_lazy(javascript_quote, unicode)
    198198
    199 smart_split_re = re.compile('("(?:[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'(?:[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\'|[^\\s]+)')
     199smart_split_re = re.compile(r"""((?:_\()?"(?:[^"\\]*(?:\\.[^"\\]*)*)"\)?|(?:_\()?'(?:[^'\\]*(?:\\.[^'\\]*)*)'\)?|[^\s]+)""")
    200200def smart_split(text):
    201201    r"""
    202202    Generator that splits a string by spaces, leaving quoted phrases together.
    203203    Supports both single and double quotes, and supports escaping quotes with
    204204    backslashes. In the output, strings will keep their initial and trailing
    205     quote marks.
     205    quote marks. Also, gettext markers '_(', ')' are preserved.
    206206
    207207    >>> list(smart_split(r'This is "a person\'s" test.'))
    208208    [u'This', u'is', u'"a person\\\'s"', u'test.']
     
    210210        [u'Another', u"'person's'", u'test.']
    211211        >>> list(smart_split(r'A "\"funky\" style" test.'))
    212212        [u'A', u'""funky" style"', u'test.']
     213    >>> list(smart_split(' _("my quoted string") '))
     214    [u'_("my quoted string")']
     215    >>> list(smart_split(" _('my quoted string') "))
     216    [u"_('my quoted string')"]
    213217    """
    214218    text = force_unicode(text)
    215219    for bit in smart_split_re.finditer(text):
    216220        bit = bit.group(0)
    217         if bit[0] == '"' and bit[-1] == '"':
    218             yield '"' + bit[1:-1].replace('\\"', '"').replace('\\\\', '\\') + '"'
    219         elif bit[0] == "'" and bit[-1] == "'":
    220             yield "'" + bit[1:-1].replace("\\'", "'").replace("\\\\", "\\") + "'"
     221        prefix, suffix = '', ''
     222        start, end = 1, -1
     223        if bit[0:2] == '_(' and bit[-1] == ')':
     224            prefix, suffix = '_(', ')'
     225            start, end = 3, -2
     226        if (bit[0] == '"' and bit[-1] == '"'
     227                or bit[0:3] == '_("' and bit[-2:] == '")'):
     228            yield '%s"%s"%s' % (prefix,
     229                    bit[start:end].replace(r'\"', '"').replace(r'\\', '\\'),
     230                    suffix)
     231        elif (bit[0] == "'" and bit[-1] == "'"
     232                or bit[0:3] == "_('" and bit[-2:] == "')"):
     233            yield "%s'%s'%s" % (prefix,
     234                    bit[start:end].replace(r"\'", "'").replace(r'\\', '\\'),
     235                    suffix)
    221236        else:
    222237            yield bit
    223238smart_split = allow_lazy(smart_split, unicode)
  • tests/regressiontests/text/tests.py

     
    1515[u'"a', u"'one"]
    1616>>> print list(smart_split(r'''all friends' tests'''))[1]
    1717friends'
     18>>> list(smart_split(' _("my quoted string") '))
     19[u'_("my quoted string")']
     20>>> list(smart_split(" _('my quoted string') "))
     21[u"_('my quoted string')"]
     22>>> print list(smart_split(" _('my \"quoted\" string') "))[0]
     23_('my "quoted" string')
    1824
    1925### urlquote #############################################################
    2026>>> from django.utils.http import urlquote, urlquote_plus
Back to Top