Ticket #9315: patch-9315.diff

File patch-9315.diff, 4.5 KB (added by Natalia Bidart, 15 years ago)
  • django/utils/text.py

     
    197197    return str(ustring_re.sub(fix, s))
    198198javascript_quote = allow_lazy(javascript_quote, unicode)
    199199
    200 smart_split_re = re.compile('("(?:[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'(?:[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\'|[^\\s]+)')
     200smart_split_re = re.compile(r"""(\S*?"(?:[^"\\]*(?:\\.[^"\\]*)*)"\S*| # matches '"value with spaces"' and 'keyword="value with spaces"'
     201                                 \S*?'(?:[^'\\]*(?:\\.[^'\\]*)*)'\S*| # same as above but with quotes swapped
     202                                 \S+)                                 # matches not whitespaces""",
     203                            re.VERBOSE)
    201204def smart_split(text):
    202205    r"""
    203206    Generator that splits a string by spaces, leaving quoted phrases together.
     
    206209    quote marks and escaped quotes will remain escaped (the results can then
    207210    be further processed with unescape_string_literal()).
    208211
     212    (See more doctests at tests/regressiontests/text/tests.py)
     213
    209214    >>> list(smart_split(r'This is "a person\'s" test.'))
    210215    [u'This', u'is', u'"a person\\\'s"', u'test.']
    211216    >>> list(smart_split(r"Another 'person\'s' test."))
  • django/template/__init__.py

     
    199199            self.contents[:20].replace('\n', ''))
    200200
    201201    def split_contents(self):
    202         split = []
    203         bits = iter(smart_split(self.contents))
    204         for bit in bits:
    205             # Handle translation-marked template pieces
    206             if bit.startswith('_("') or bit.startswith("_('"):
    207                 sentinal = bit[2] + ')'
    208                 trans_bit = [bit]
    209                 while not bit.endswith(sentinal):
    210                     bit = bits.next()
    211                     trans_bit.append(bit)
    212                 bit = ' '.join(trans_bit)
    213             split.append(bit)
    214         return split
     202        return list(smart_split(self.contents))
    215203
    216204class Lexer(object):
    217205    def __init__(self, template_string, origin):
     
    478466    'arg_sep': re.escape(FILTER_ARGUMENT_SEPARATOR),
    479467  }
    480468
    481 filter_raw_string = filter_raw_string.replace("\n", "").replace(" ", "")
    482 filter_re = re.compile(filter_raw_string, re.UNICODE)
     469filter_re = re.compile(filter_raw_string, re.UNICODE | re.VERBOSE)
    483470
    484471class FilterExpression(object):
    485472    r"""
  • django/template/defaulttags.py

     
    10921092
    10931093    The URL will look like ``/clients/client/123/``.
    10941094    """
    1095     bits = token.contents.split(' ')
     1095    bits = token.split_contents()
    10961096    if len(bits) < 2:
    10971097        raise TemplateSyntaxError("'%s' takes at least one argument"
    10981098                                  " (path to a view)" % bits[0])
  • 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(u'url search_page words="something else"'))
     19[u'url', u'search_page', u'words="something else"']
     20>>> list(smart_split(u"url search_page words='something else'"))
     21[u'url', u'search_page', u"words='something else'"]
     22>>> list(smart_split(u'url search_page words "something else"'))
     23[u'url', u'search_page', u'words', u'"something else"']
     24>>> list(smart_split(u'url search_page words-"something else"'))
     25[u'url', u'search_page', u'words-"something else"']
     26>>> list(smart_split(u'url search_page words=hello'))
     27[u'url', u'search_page', u'words=hello']
     28>>> list(smart_split(u'url search_page words="something else'))
     29[u'url', u'search_page', u'words="something', u'else']
     30>>> list(smart_split(u"cycle 'a' 'b' 'c' as abc"))
     31[u'cycle', u"'a'", u"'b'", u"'c'", u'as', u'abc']
     32>>> list(smart_split(u'cycle "a" "b" "c" as abc'))
     33[u'cycle', u'"a"', u'"b"', u'"c"', u'as', u'abc']
     34>>> list(smart_split(u'cycle "a" "b" "c" "d" as abc'))
     35[u'cycle', u'"a"', u'"b"', u'"c"', u'"d"', u'as', u'abc']
    1836
    1937### urlquote #############################################################
    2038>>> from django.utils.http import urlquote, urlquote_plus
Back to Top