Ticket #2721: commentable_block_tags.patch

File commentable_block_tags.patch, 2.9 KB (added by Chris Beaven, 18 years ago)

patch, including tests

  • django/template/__init__.py

     
    6666TOKEN_TEXT = 0
    6767TOKEN_VAR = 1
    6868TOKEN_BLOCK = 2
     69TOKEN_COMMENT = 3
    6970
    7071# template syntax constants
     72COMMENT_IDENTIFIER = '#'
    7173FILTER_SEPARATOR = '|'
    7274FILTER_ARGUMENT_SEPARATOR = ':'
    7375VARIABLE_ATTRIBUTE_SEPARATOR = '.'
     
    163165
    164166class Token(object):
    165167    def __init__(self, token_type, contents):
    166         "The token_type must be TOKEN_TEXT, TOKEN_VAR or TOKEN_BLOCK"
     168        "The token_type must be TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK, or TOKEN_COMMENT"
    167169        self.token_type, self.contents = token_type, contents
    168170
    169171    def __str__(self):
    170172        return '<%s token: "%s...">' % \
    171             ({TOKEN_TEXT: 'Text', TOKEN_VAR: 'Var', TOKEN_BLOCK: 'Block'}[self.token_type],
     173            ({TOKEN_TEXT: 'Text', TOKEN_VAR: 'Var', TOKEN_BLOCK: 'Block', TOKEN_COMMENT: 'Comment'}[self.token_type],
    172174            self.contents[:20].replace('\n', ''))
    173175
    174176    def split_contents(self):
     
    190192        if token_string.startswith(VARIABLE_TAG_START):
    191193            token = Token(TOKEN_VAR, token_string[len(VARIABLE_TAG_START):-len(VARIABLE_TAG_END)].strip())
    192194        elif token_string.startswith(BLOCK_TAG_START):
    193             token = Token(TOKEN_BLOCK, token_string[len(BLOCK_TAG_START):-len(BLOCK_TAG_END)].strip())
     195            token_string = token_string[len(BLOCK_TAG_START):-len(BLOCK_TAG_END)].strip()
     196            if token_string.startswith(COMMENT_IDENTIFIER):
     197                token = Token(TOKEN_COMMENT, token_string)
     198            else:
     199                token = Token(TOKEN_BLOCK, token_string)
    194200        else:
    195201            token = Token(TOKEN_TEXT, token_string)
    196202        return token
  • tests/regressiontests/templates/tests.py

     
    170170            # Escaped backslash using known escape char
    171171            'basic-syntax35': (r'{{ var|default_if_none:"foo\now" }}', {"var": None}, r'foo\now'),
    172172
     173            ### COMMENTED BLOCK TAGS ####################################################
     174            'commented-block-tag01': ("{% #invalidtag %}commented invalid tag", {}, "commented invalid tag"),
     175            'commented-block-tag02': ("commented valid tag{% # if %}", {}, "commented valid tag"),
     176            'commented-block-tag03': ("{% comment %}hide{% #endcomment %}still hide{% endcomment %}show", {}, "show"),
     177
    173178            ### COMMENT TAG ###########################################################
    174179            'comment-tag01': ("{% comment %}this is hidden{% endcomment %}hello", {}, "hello"),
    175180            'comment-tag02': ("{% comment %}this is hidden{% endcomment %}hello{% comment %}foo{% endcomment %}", {}, "hello"),
Back to Top