Django

Code

Ticket #1199: sprint-patch1.diff

File sprint-patch1.diff, 3.9 kB (added by insin, 10 months ago)

A first stab at just getting this working at all

  • django/template/__init__.py

    old new  
    499499 (?:%(filter_sep)s 
    500500     (?P<filter_name>\w+) 
    501501         (?:%(arg_sep)s 
     502           (?P<args> 
    502503             (?: 
    503               %(i18n_open)s"(?P<i18n_arg>%(str)s)"%(i18n_close)s| 
    504               "(?P<constant_arg>%(str)s)"| 
    505               (?P<var_arg>[%(var_chars)s]+) 
     504              %(i18n_open)s"%(str)s"%(i18n_close)s| 
     505              "%(str)s"| 
     506              [%(var_chars)s]+ 
    506507             ) 
     508             (?: 
     509              ,%(i18n_open)s"%(str)s"%(i18n_close)s| 
     510              ,"%(str)s"| 
     511              ,[%(var_chars)s]+ 
     512             )* 
     513           ) 
    507514         )? 
    508515 )""" % { 
    509516    'str': r"""[^"\\]*(?:\\.[^"\\]*)*""", 
     
    517524filter_raw_string = filter_raw_string.replace("\n", "").replace(" ", "") 
    518525filter_re = re.compile(filter_raw_string, re.UNICODE) 
    519526 
     527arg_raw_string = r""" 
     528%(i18n_open)s"(?P<i18n_arg>%(str)s)"%(i18n_close)s| 
     529"(?P<constant_arg>%(str)s)"| 
     530(?P<var_arg>[%(var_chars)s]+)""" % { 
     531    'str': r"""[^"\\]*(?:\\.[^"\\]*)*""", 
     532    'var_chars': "\w\." , 
     533    'i18n_open' : re.escape("_("), 
     534    'i18n_close' : re.escape(")"), 
     535  } 
     536 
     537arg_raw_string = arg_raw_string.replace("\n", "").replace(" ", "") 
     538arg_re = re.compile(arg_raw_string, re.UNICODE) 
     539 
    520540class FilterExpression(object): 
    521541    """ 
    522542    Parses a variable token and its optional filters (all as a single string), 
     
    558578            else: 
    559579                filter_name = match.group("filter_name") 
    560580                args = [] 
    561                 constant_arg, i18n_arg, var_arg = match.group("constant_arg", "i18n_arg", "var_arg") 
    562                 if i18n_arg: 
    563                     args.append((False, _(i18n_arg.replace(r'\"', '"')))) 
    564                 elif constant_arg is not None: 
    565                     args.append((False, constant_arg.replace(r'\"', '"'))) 
    566                 elif var_arg: 
    567                     args.append((True, var_arg)) 
     581                arg_group = match.group("args") 
     582                if arg_group: 
     583                    for arg_match in arg_re.finditer(arg_group): 
     584                        constant_arg, i18n_arg, var_arg = arg_match.group("constant_arg", "i18n_arg", "var_arg") 
     585                        if i18n_arg: 
     586                            args.append((False, _(i18n_arg.replace(r'\"', '"')))) 
     587                        elif constant_arg is not None: 
     588                            args.append((False, constant_arg.replace(r'\"', '"'))) 
     589                        elif var_arg: 
     590                            args.append((True, var_arg)) 
    568591                filter_func = parser.find_filter(filter_name) 
    569592                self.args_check(filter_name,filter_func, args) 
    570593                filters.append( (filter_func,args)) 
  • tests/regressiontests/templates/tests.py

    old new  
    4141 
    4242register.tag("echo", do_echo) 
    4343 
     44def multi_args(arg1, arg2, arg3): 
     45    return " ".join([arg1, arg2, arg3]) 
     46 
     47register.filter("multi_args", multi_args) 
     48 
    4449template.libraries['django.templatetags.testtags'] = register 
    4550 
    4651##################################### 
     
    272277            # Numbers as filter arguments should work 
    273278            'filter-syntax19': ('{{ var|truncatewords:1 }}', {"var": "hello world"}, "hello ..."), 
    274279 
     280            # Filters can accept multiple arguments 
     281            'filter-syntax20': ('{% load testtags %}{{ var|multi_args:two,"three" }}', {"var": "one", "two": "see you"}, "one see you three"), 
     282 
    275283            ### COMMENT SYNTAX ######################################################## 
    276284            'comment-syntax01': ("{# this is hidden #}hello", {}, "hello"), 
    277285            'comment-syntax02': ("{# this is hidden #}hello{# foo #}", {}, "hello"),