def parse_block(self): node = nodes.Block(lineno=next(self.stream).lineno) node.name = self.stream.expect('name').value node.scoped = self.stream.skip_if('name:scoped') # common problem people encounter when switching from django # to jinja. we do not support hyphens in block names, so let's # raise a nicer error message in that case. if self.stream.current.type == 'sub': self.fail('Block names in Jinja have to be valid Python ' 'identifiers and may not contain hyphens, use an ' 'underscore instead.') node.body = self.parse_statements(('name:endblock', ), drop_needle=True) self.stream.skip_if('name:' + node.name) return node
def parseExpr(self): t = self.peek().type if 'yield' == t: self.advance() block = nodes.Block() block._yield = True return block elif t in ('id', 'class'): tok = self.advance() self.lexer.defer(self.lexer.tok('tag', 'div')) self.lexer.defer(tok) return self.parseExpr() funcName = 'parse%s' % t.capitalize() if hasattr(self, funcName): return getattr(self, funcName)() else: raise Exception('unexpected token "%s"' % t)
def parse(self): block = nodes.Block() parser = None block.line = self.line() while 'eos' != self.peek().type: if 'newline' == self.peek().type: self.advance() else: block.append(self.parseExpr()) parser = self.extending if parser: self.context(parser) ast = parser.parse() self.context() return ast return block
def parse_block(self): lineno = self.token_stream.expect('name:block').lineno name = self.token_stream.expect('name').value body = self.parse_statements(end_tokens=('name:endblock', )) self.token_stream.skip_if('name:' + name) return nodes.Block(name, body, lineno=lineno)
def parseBlockExpansion(self): if ':' == self.peek().type: self.advance() return nodes.Block(self.parseExpr()) else: return self.block()
def parseTag(self): i = 2 if 'attrs' == self.lookahead(i).type: i += 1 if ':' == self.lookahead(i).type: if 'indent' == self.lookahead(i + 1).type: return self.parseASTFilter name = self.advance().val tag = nodes.Tag(name) dot = None tag.line = self.line() while True: t = self.peek().type if t in ('id', 'class'): tok = self.advance() tag.setAttribute(tok.type, "'%s'" % tok.val) continue elif 'attrs' == t: obj = self.advance().attrs for n, v in obj.iteritems(): tag.setAttribute(n, v) continue else: break if '.' == self.peek().val: dot = tag.textOnly = True self.advance() t = self.peek().type if 'text' == t: tag.text = self.parseText() elif 'code' == t: tag.code = self.parseCode() elif ':' == t: self.advance() tag.block = nodes.Block() tag.block.append(self.parseExpr()) while 'newline' == self.peek().type: self.advance() tag.textOnly = tag.textOnly or tag.name in textOnly if 'script' == tag.name: type = tag.getAttribute('type') if not dot and type and 'text/javascript' != type.strip('"\''): tag.textOnly = False if 'indent' == self.peek().type: if tag.textOnly: self.lexer.pipeless = True tag.block = self.parseTextBlock() self.lexer.pipeless = False else: block = self.block() if tag.block: for node in block.nodes: tag.block.append(node) else: tag.block = block return tag