class Lexer
from django.template.base import Lexer
Ancestors (MRO)
Attribute | Type | Defined in |
---|---|---|
__dict__ |
getset_descriptor
|
django.template.base.Lexer |
__weakref__ |
getset_descriptor
|
django.template.base.Lexer |
def __init__(self, template_string)
django.template.base.Lexer
Initialize self. See help(type(self)) for accurate signature.
def __init__(self, template_string):
self.template_string = template_string
self.verbatim = False
def __repr__(self)
django.template.base.Lexer
Return repr(self).
def __repr__(self):
return '<%s template_string="%s...", verbatim=%s>' % (
self.__class__.__qualname__,
self.template_string[:20].replace("\n", ""),
self.verbatim,
)
def create_token(self, token_string, position, lineno, in_tag)
django.template.base.Lexer
Convert the given token string into a new Token object and return it. If in_tag is True, we are processing something that matched a tag, otherwise it should be treated as a literal string.
def create_token(self, token_string, position, lineno, in_tag):
"""
Convert the given token string into a new Token object and return it.
If in_tag is True, we are processing something that matched a tag,
otherwise it should be treated as a literal string.
"""
if in_tag:
# The [0:2] and [2:-2] ranges below strip off *_TAG_START and
# *_TAG_END. The 2's are hard-coded for performance. Using
# len(BLOCK_TAG_START) would permit BLOCK_TAG_START to be
# different, but it's not likely that the TAG_START values will
# change anytime soon.
token_start = token_string[0:2]
if token_start == BLOCK_TAG_START:
content = token_string[2:-2].strip()
if self.verbatim:
# Then a verbatim block is being processed.
if content != self.verbatim:
return Token(TokenType.TEXT, token_string, position, lineno)
# Otherwise, the current verbatim block is ending.
self.verbatim = False
elif content[:9] in ("verbatim", "verbatim "):
# Then a verbatim block is starting.
self.verbatim = "end%s" % content
return Token(TokenType.BLOCK, content, position, lineno)
if not self.verbatim:
content = token_string[2:-2].strip()
if token_start == VARIABLE_TAG_START:
return Token(TokenType.VAR, content, position, lineno)
# BLOCK_TAG_START was handled above.
assert token_start == COMMENT_TAG_START
return Token(TokenType.COMMENT, content, position, lineno)
return Token(TokenType.TEXT, token_string, position, lineno)
def tokenize(self)
django.template.base.Lexer
Return a list of tokens from a given template_string.
def tokenize(self):
"""
Return a list of tokens from a given template_string.
"""
in_tag = False
lineno = 1
result = []
for token_string in tag_re.split(self.template_string):
if token_string:
result.append(self.create_token(token_string, None, lineno, in_tag))
lineno += token_string.count("\n")
in_tag = not in_tag
return result