Ticket #7027: fix_tag_translation.diff
File fix_tag_translation.diff, 3.1 KB (added by , 16 years ago) |
---|
-
django/utils/text.py
196 196 return str(ustring_re.sub(fix, s)) 197 197 javascript_quote = allow_lazy(javascript_quote, unicode) 198 198 199 smart_split_re = re.compile( '("(?:[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'(?:[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\'|[^\\s]+)')199 smart_split_re = re.compile(r"""((?:_\()?"(?:[^"\\]*(?:\\.[^"\\]*)*)"\)?|(?:_\()?'(?:[^'\\]*(?:\\.[^'\\]*)*)'\)?|[^\s]+)""") 200 200 def smart_split(text): 201 201 r""" 202 202 Generator that splits a string by spaces, leaving quoted phrases together. 203 203 Supports both single and double quotes, and supports escaping quotes with 204 204 backslashes. In the output, strings will keep their initial and trailing 205 quote marks. 205 quote marks. Also, gettext markers '_(', ')' are preserved. 206 206 207 207 >>> list(smart_split(r'This is "a person\'s" test.')) 208 208 [u'This', u'is', u'"a person\\\'s"', u'test.'] … … 210 210 [u'Another', u"'person's'", u'test.'] 211 211 >>> list(smart_split(r'A "\"funky\" style" test.')) 212 212 [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')"] 213 217 """ 214 218 text = force_unicode(text) 215 219 for bit in smart_split_re.finditer(text): 216 220 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) 221 236 else: 222 237 yield bit 223 238 smart_split = allow_lazy(smart_split, unicode) -
tests/regressiontests/text/tests.py
15 15 [u'"a', u"'one"] 16 16 >>> print list(smart_split(r'''all friends' tests'''))[1] 17 17 friends' 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') 18 24 19 25 ### urlquote ############################################################# 20 26 >>> from django.utils.http import urlquote, urlquote_plus