Ticket #1522: smart_token_splitting.diff
File smart_token_splitting.diff, 12.1 KB (added by , 19 years ago) |
---|
-
django/contrib/admin/templatetags/admin_modify.py
200 200 filter_interface_script_maybe = register.simple_tag(filter_interface_script_maybe) 201 201 202 202 def do_one_arg_tag(node_factory, parser,token): 203 tokens = token. contents.split()203 tokens = token.split_contents() 204 204 if len(tokens) != 2: 205 205 raise template.TemplateSyntaxError("%s takes 1 argument" % tokens[0]) 206 206 return node_factory(tokens[1]) -
django/contrib/admin/templatetags/adminapplist.py
49 49 """ 50 50 {% get_admin_app_list as app_list %} 51 51 """ 52 tokens = token. contents.split()52 tokens = token.split_contents() 53 53 if len(tokens) < 3: 54 54 raise template.TemplateSyntaxError, "'%s' tag requires two arguments" % tokens[0] 55 55 if tokens[1] != 'as': -
django/contrib/admin/templatetags/log.py
38 38 self.tag_name = tag_name 39 39 40 40 def __call__(self, parser, token): 41 tokens = token. contents.split()41 tokens = token.split_contents() 42 42 if len(tokens) < 4: 43 43 raise template.TemplateSyntaxError, "'%s' statements require two arguments" % self.tag_name 44 44 if not tokens[1].isdigit(): -
django/contrib/comments/templatetags/comments.py
147 147 self.free = free 148 148 149 149 def __call__(self, parser, token): 150 tokens = token. contents.split()150 tokens = token.split_contents() 151 151 if len(tokens) < 4: 152 152 raise template.TemplateSyntaxError, "%r tag requires at least 3 arguments" % tokens[0] 153 153 if tokens[1] != 'for': … … 225 225 self.free = free 226 226 227 227 def __call__(self, parser, token): 228 tokens = token. contents.split()228 tokens = token.split_contents() 229 229 # Now tokens is a list like this: 230 230 # ['get_comment_list', 'for', 'lcom.eventtimes', 'event.id', 'as', 'comment_list'] 231 231 if len(tokens) != 6: … … 280 280 self.free = free 281 281 282 282 def __call__(self, parser, token): 283 tokens = token. contents.split()283 tokens = token.split_contents() 284 284 # Now tokens is a list like this: 285 285 # ['get_comment_list', 'for', 'lcom.eventtimes', 'event.id', 'as', 'comment_list'] 286 286 if not len(tokens) in (6, 7): -
django/core/template/__init__.py
84 84 tag_re = re.compile('(%s.*?%s|%s.*?%s)' % (re.escape(BLOCK_TAG_START), re.escape(BLOCK_TAG_END), 85 85 re.escape(VARIABLE_TAG_START), re.escape(VARIABLE_TAG_END))) 86 86 87 # split a token up seperating by spaces (allowing for quoted bits) 88 split_token_re = re.compile(r'(?:\'[^\']+\'|"[^"]+"|[^ ]+)(?= )*') 89 check_for_quotes = re.compile(r'[\'"]') 90 87 91 # global dictionary of libraries that have been loaded using get_library 88 92 libraries = {} 89 93 # global list of libraries to load by default for a new parser … … 220 224 self.contents[:].replace('\n', '') 221 225 ) 222 226 227 def split_contents(self): 228 if check_for_quotes.search(self.contents): 229 return split_token_re.findall(self.contents) 230 else: 231 return self.contents.split() 232 233 223 234 class Lexer(object): 224 235 def __init__(self, template_string, origin): 225 236 self.template_string = template_string … … 291 302 self.prepend_token(token) 292 303 return nodelist 293 304 try: 294 command = token. contents.split()[0]305 command = token.split_contents()[0] 295 306 except IndexError: 296 307 self.empty_block_tag(token) 297 308 # execute callback function for this tag and append resulting node … … 784 795 785 796 def generic_tag_compiler(params, defaults, name, node_class, parser, token): 786 797 "Returns a template.Node subclass." 787 bits = token. contents.split()[1:]798 bits = token.split_contents()[1:] 788 799 bmax = len(params) 789 800 def_len = defaults and len(defaults) or 0 790 801 bmin = bmax - def_len -
django/core/template/defaulttags.py
328 328 # a global variable, which would make cycle names have to be unique across 329 329 # *all* templates. 330 330 331 args = token. contents.split()331 args = token.split_contents() 332 332 if len(args) < 2: 333 333 raise TemplateSyntaxError("'Cycle' statement requires at least two arguments") 334 334 … … 410 410 411 411 but obviously much cleaner! 412 412 """ 413 bits = token. contents.split()[1:]413 bits = token.split_contents()[1:] 414 414 if len(bits) < 1: 415 415 raise TemplateSyntaxError, "'firstof' statement requires at least one argument" 416 416 return FirstOfNode(bits) … … 450 450 ========================== ================================================ 451 451 452 452 """ 453 bits = token. contents.split()453 bits = token.split_contents() 454 454 if len(bits) == 5 and bits[4] != 'reversed': 455 455 raise TemplateSyntaxError, "'for' statements with five words should end in 'reversed': %s" % token.contents 456 456 if len(bits) not in (4, 5): … … 481 481 ... 482 482 {% endifnotequal %} 483 483 """ 484 bits = token. contents.split()484 bits = token.split_contents() 485 485 if len(bits) != 3: 486 486 raise TemplateSyntaxError, "%r takes two arguments" % bits[0] 487 487 end_tag = 'end' + bits[0] … … 550 550 {% endif %} 551 551 {% endif %} 552 552 """ 553 bits = token. contents.split()553 bits = token.split_contents() 554 554 del bits[0] 555 555 if not bits: 556 556 raise TemplateSyntaxError, "'if' statement requires at least one argument" … … 591 591 <a href="{{ date|date:"M/d"|lower }}/">{{ date|date:"j" }}</a> 592 592 {% endfor %} 593 593 """ 594 bits = token. contents.split()594 bits = token.split_contents() 595 595 if len(bits) != 1: 596 596 raise TemplateSyntaxError, "'ifchanged' tag takes no arguments" 597 597 nodelist = parser.parse(('endifchanged',)) … … 615 615 616 616 {% ssi /home/html/ljworld.com/includes/right_generic.html parsed %} 617 617 """ 618 bits = token. contents.split()618 bits = token.split_contents() 619 619 parsed = False 620 620 if len(bits) not in (2, 3): 621 621 raise TemplateSyntaxError, "'ssi' tag takes one argument: the path to the file to be included" … … 636 636 637 637 {% load news.photos %} 638 638 """ 639 bits = token. contents.split()639 bits = token.split_contents() 640 640 for taglib in bits[1:]: 641 641 # add the library to the parser 642 642 try: … … 779 779 ``closevariable`` ``}}`` 780 780 ================== ======= 781 781 """ 782 bits = token. contents.split()782 bits = token.split_contents() 783 783 if len(bits) != 2: 784 784 raise TemplateSyntaxError, "'templatetag' statement takes one argument" 785 785 tag = bits[1] … … 803 803 the above example will be 88 pixels wide (because 175/200 = .875; .875 * 804 804 100 = 87.5 which is rounded up to 88). 805 805 """ 806 bits = token. contents.split()806 bits = token.split_contents() 807 807 if len(bits) != 4: 808 808 raise TemplateSyntaxError("widthratio takes three arguments") 809 809 tag, this_value_expr, max_value_expr, max_width = bits -
django/core/template/loader_tags.py
112 112 """ 113 113 Define a block that can be overridden by child templates. 114 114 """ 115 bits = token. contents.split()115 bits = token.split_contents() 116 116 if len(bits) != 2: 117 117 raise TemplateSyntaxError, "'%s' tag takes only one argument" % bits[0] 118 118 block_name = bits[1] … … 137 137 or ``{% extends variable %}`` uses the value of ``variable`` as the name 138 138 of the parent template to extend. 139 139 """ 140 bits = token. contents.split()140 bits = token.split_contents() 141 141 if len(bits) != 2: 142 142 raise TemplateSyntaxError, "'%s' takes one argument" % bits[0] 143 143 parent_name, parent_name_expr = None, None … … 158 158 159 159 {% include "foo/some_include" %} 160 160 """ 161 bits = token. contents.split()161 bits = token.split_contents() 162 162 if len(bits) != 2: 163 163 raise TemplateSyntaxError, "%r tag takes one argument: the name of the template to be included" % bits[0] 164 164 path = bits[1] -
django/templatetags/i18n.py
83 83 your setting file (or the default settings) and 84 84 put it into the named variable. 85 85 """ 86 args = token. contents.split()86 args = token.split_contents() 87 87 if len(args) != 3 or args[1] != 'as': 88 88 raise TemplateSyntaxError, "'get_available_languages' requires 'as variable' (got %r)" % args 89 89 return GetAvailableLanguagesNode(args[2]) … … 100 100 put it's value into the ``language`` context 101 101 variable. 102 102 """ 103 args = token. contents.split()103 args = token.split_contents() 104 104 if len(args) != 3 or args[1] != 'as': 105 105 raise TemplateSyntaxError, "'get_available_languages' requires 'as variable' (got %r)" % args 106 106 return GetCurrentLanguageNode(args[2]) -
tests/othertests/templates.py
8 8 from django.utils.translation import activate, deactivate, install 9 9 import traceback 10 10 11 ################################# 12 # Custom template tag for tests #13 ################################# 11 ################################## 12 # Custom template tags for tests # 13 ################################## 14 14 15 15 register = template.Library() 16 16 … … 22 22 return " ".join(self.contents) 23 23 24 24 def do_echo(parser, token): 25 return EchoNode(token. contents.split()[1:])25 return EchoNode(token.split_contents()[1:]) 26 26 27 27 register.tag("echo", do_echo) 28 28 29 30 class TokenBitsNode(template.Node): 31 def __init__(self, token): 32 self.token = token 33 34 def render(self, context): 35 bits = self.token.split_contents()[1:] 36 return str(bits) 37 38 def do_token_bits(parser, token): 39 return TokenBitsNode(token) 40 41 register.tag("tokenbits", do_token_bits) 42 43 29 44 template.libraries['django.templatetags.testtags'] = register 30 45 31 46 ##################################### … … 134 149 135 150 # Default argument testing 136 151 'basic-syntax32' : (r'{{ var|yesno:"yup,nup,mup" }} {{ var|yesno }}', {"var": True}, 'yup yes'), 152 153 # Smart token content splitting 154 'basic-syntax33': (r'{% load testtags %}{% tokenbits one two three %}', {}, "['one', 'two', 'three']"), 155 'basic-syntax34': (r'{% load testtags %}{% tokenbits one "two three" %}', {}, "['one', '\"two three\"']"), 156 'basic-syntax35': (r"{% load testtags %}{% tokenbits 'one two' three %}", {}, "[\"'one two'\", 'three']"), 137 157 138 158 ### IF TAG ################################################################ 139 159 'if-tag01': ("{% if foo %}yes{% else %}no{% endif %}", {"foo": True}, "yes"),