Ticket #2594: template_whitespace_ticket_2594.diff

File template_whitespace_ticket_2594.diff, 3.3 KB (added by jshedd, 14 years ago)

Causes template tag blocks to collapse the whitespace around them when they are on their own line.

  • template/__init__.py

     
    9090UNKNOWN_SOURCE="<unknown source>"
    9191
    9292# match a variable or block tag and capture the entire tag, including start/end delimiters
    93 tag_re = re.compile('(%s.*?%s|%s.*?%s|%s.*?%s)' % (re.escape(BLOCK_TAG_START), re.escape(BLOCK_TAG_END),
    94                                           re.escape(VARIABLE_TAG_START), re.escape(VARIABLE_TAG_END),
    95                                           re.escape(COMMENT_TAG_START), re.escape(COMMENT_TAG_END)))
     93tag_re = re.compile('([\n\r\v\f]+[ \t]*?%s[^\n\r\v\f%s%s]*?%s(?=[ \t]*?[\n\r\v\f]+)|%s.*?%s|%s.*?%s|%s.*?%s)' % (
     94                        re.escape(BLOCK_TAG_START), re.escape(BLOCK_TAG_START), re.escape(BLOCK_TAG_END), re.escape(BLOCK_TAG_END),
     95                        re.escape(BLOCK_TAG_START), re.escape(BLOCK_TAG_END),                       
     96                        re.escape(VARIABLE_TAG_START), re.escape(VARIABLE_TAG_END),
     97                        re.escape(COMMENT_TAG_START), re.escape(COMMENT_TAG_END)))
     98# find the command within a block tag
     99block_command_re = re.compile('%s(?P<token_string>.*?)%s' % (re.escape(BLOCK_TAG_START), re.escape(BLOCK_TAG_END)))
    96100
    97101# global dictionary of libraries that have been loaded using get_library
    98102libraries = {}
     
    207211            self.contents[:20].replace('\n', ''))
    208212
    209213    def split_contents(self):
    210         split = []
     214        split = []             
    211215        bits = iter(smart_split(self.contents))
    212216        for bit in bits:
    213217            # Handle translation-marked template pieces
     
    230234        "Return a list of tokens from a given template_string."
    231235        in_tag = False
    232236        result = []
    233         for bit in tag_re.split(self.template_string):
     237        for bit in tag_re.split(self.template_string): 
    234238            if bit:
    235239                result.append(self.create_token(bit, in_tag))
    236240            in_tag = not in_tag
     
    241245        Convert the given token string into a new Token object and return it.
    242246        If in_tag is True, we are processing something that matched a tag,
    243247        otherwise it should be treated as a literal string.
    244         """
     248        """               
    245249        if in_tag:
    246250            if token_string.startswith(VARIABLE_TAG_START):
    247251                token = Token(TOKEN_VAR, token_string[len(VARIABLE_TAG_START):-len(VARIABLE_TAG_END)].strip())
    248             elif token_string.startswith(BLOCK_TAG_START):
    249                 token = Token(TOKEN_BLOCK, token_string[len(BLOCK_TAG_START):-len(BLOCK_TAG_END)].strip())
     252            elif token_string.lstrip().startswith(BLOCK_TAG_START):
     253                token_struct = block_command_re.search(token_string)
     254                token_string = token_struct.group('token_string')
     255                token = Token(TOKEN_BLOCK, token_string.strip())
    250256            elif token_string.startswith(COMMENT_TAG_START):
    251257                token = Token(TOKEN_COMMENT, '')
    252258        else:
     
    984990    builtins.append(get_library(module_name))
    985991
    986992add_to_builtins('django.template.defaulttags')
    987 add_to_builtins('django.template.defaultfilters')
     993add_to_builtins('django.template.defaultfilters')
     994 No newline at end of file
Back to Top