def parse_tropts(self, parser, lineno): """ {% tropts source="index" %} {% tr %} {% endtr %} {% tr %} {% endtr %} {% tr %} {% endtr %} {% endtropts %} """ node = nodes.Scope(lineno=lineno) assignments = [] while parser.stream.current.type != 'block_end': lineno = parser.stream.current.lineno if assignments: parser.stream.expect('comma') key = parser.parse_assign_target() # a=b (a is key) parser.stream.expect('assign') value = parser.parse_expression() # a=b (b is expression) assignments.append(nodes.Keyword(key.name, value)) node.body = parser.parse_statements(('name:endtropts',), drop_needle=True) for item in node.body: if isinstance(item, (nodes.Call, nodes.CallBlock)) and item.call.node.name in ('_translate_trs', '_translate_tr'): used_keys = set(arg.key for arg in item.call.args) allowed_assignments = [assign for assign in assignments if not assign.key in used_keys] item.call.args += allowed_assignments return node
def parse(self, parser): parser_token = next(parser.stream) parser_lineno = parser_token.lineno node = nodes.Scope(lineno=parser_lineno) assignments = [] widget_args = [] while parser.stream.current.type != 'block_end': lineno = parser.stream.current.lineno if assignments: parser.stream.expect('comma') widget_target = parser.parse_assign_target() parser.stream.expect('assign') widget_config = parser.parse_expression() widget_name = widget_target.name widget_target, widget_node = self._widget_uuid( widget_target, widget_config) widget_args = [ nodes.Name('widgets', 'load'), nodes.Const(widget_name), widget_config, widget_node ] assignments.append( nodes.Assign(widget_target, widget_node, lineno=lineno)) node.body = assignments + list( parser.parse_statements(('name:endwidget', ), drop_needle=True)) output = [ nodes.CallBlock( self.call_method('_widget_onload', args=widget_args), [], [], '').set_lineno(parser_lineno), node ] return output
def parse(self, parser): node = nodes.ScopedEvalContextModifier( lineno=next(parser.stream).lineno) node.options = [nodes.Keyword('autoescape', parser.parse_expression())] node.body = parser.parse_statements(('name:endautoescape', ), drop_needle=True) return nodes.Scope([node])
def parse_formrow(self, parser, tag): lineno = tag.lineno field = parser.parse_expression() template_name = None if not parser.stream.current.test('block_end'): template_name = parser.parse_expression() else: template_name = nodes.Call( nodes.Name('get_formrow_template', 'load'), [], [ nodes.Keyword('caller_template', nodes.Const(parser.name)), nodes.Keyword('form', nodes.Name('form_utils_form', 'load')) ], None, None, ) if not parser.stream.current.test('block_end'): raise TemplateSyntaxError("Too many arguments", lineno) node = nodes.Scope(lineno=lineno) assignments = [nodes.Assign(nodes.Name('field', 'store'), field)] node.body = assignments + [ nodes.Include(template_name, True, False) ] return node.set_lineno(lineno)
def parse(self, parser): node = nodes.Scope(lineno=next(parser.stream).lineno) node.body = parser.parse_statements(('name:endcodeblock', ), drop_needle=True) codeblock_string = CODEBLOCK_STRING % node.body[0].nodes[0].data node.body[0].nodes[0].data = codeblock_string return node
def parse(self, parser): lineno = parser.stream.next().lineno value = parser.parse_expression() parser.stream.expect('name:as') name = parser.stream.expect('name') body = parser.parse_statements(['name:endwith'], drop_needle=True) body.insert(0, nodes.Assign(nodes.Name(name.value, 'store'), value)) return nodes.Scope(body)
def parse(self, parser): stream = parser.stream lineno = stream.next().lineno print(lineno) name = parser.parse_expression() objvar = nodes.Name('obj', 'load', lineno=lineno) print name assignments = [] has_property_details_kwargs = {} property_stack = self.get_property_stack(name) if property_stack: print('property_stack', property_stack) property_var = nodes.Name('property', 'store', lineno=lineno) property_node = nodes.Const(property_stack[-1]) property_details_node = self.call_method( 'get_property_details', args=[objvar, property_node]) assignments.append( nodes.Assign(property_var, property_details_node, lineno=lineno)) has_property_details_kwargs['property'] = property_var get_template_args = [objvar] properties_args = [name] if stream.next_if('name:as'): as_type = parser.parse_expression() get_template_args.append(as_type) if stream.next_if('name:with'): with_node = parser.parse_expression() print("with_node", with_node) properties_args.append(with_node) properties_var = nodes.Name('properties', 'store', lineno=lineno) properties_node = self.call_method('get_properties', args=properties_args, kwargs=has_property_details_kwargs) assignments.append( nodes.Assign(properties_var, properties_node, lineno=lineno)) call_node = self.call_method('get_template_list', args=get_template_args, kwargs=has_property_details_kwargs) scope = nodes.Scope(lineno=lineno) assign_obj = nodes.Assign(objvar, name, lineno=lineno) assignments.append(assign_obj) scope.body = assignments + [ nodes.Include(call_node, True, False, lineno=lineno) ] print(scope) return scope
def parse(self, parser): node = nodes.Scope(lineno=next(parser.stream).lineno) assignments = [] while parser.stream.current.type != 'block_end': lineno = parser.stream.current.lineno if assignments: parser.stream.expect('comma') target = parser.parse_assign_target() parser.stream.expect('assign') expr = parser.parse_expression() assignments.append(nodes.Assign(target, expr, lineno=lineno)) node.body = assignments + \ list(parser.parse_statements(('name:endwith',), drop_needle=True)) return node
def parse_tr(self, parser, lineno): node = nodes.Scope(lineno=lineno) assignments = [] while parser.stream.current.type != 'block_end': lineno = parser.stream.current.lineno if assignments: parser.stream.expect('comma') target = parser.parse_assign_target(name_only=True) parser.stream.expect('assign') expr = parser.parse_expression() assignments.append(nodes.Keyword(target.name, expr, lineno=lineno)) body = parser.parse_statements(('name:endtr', ), drop_needle=True) return nodes.CallBlock( nodes.Call(nodes.Name('translate_tr', 'load'), [], assignments, None, None), [], [], body).set_lineno(lineno)
def parse(self, parser): node = nodes.Scope(lineno=next(parser.stream).lineno) # Set the node body node.body = parser.parse_statements(('name:markdown', ), drop_needle=True) # Loop through the body, ignoring CodeBlocks # Transform the body into Markdown for body_object in node.body: # Conform body to markdown try: body_object.nodes[0].data = markdown.markdown( body_object.nodes[0].data) # Other objects, like CodeBlock, will trigger this exception except AttributeError: pass return node
def parse_form(self, parser, tag): lineno = tag.lineno form_instance = parser.parse_expression() template_name = nodes.Call( nodes.Name('get_formlayout_template', 'load'), [], [ nodes.Keyword('caller_template', nodes.Const(parser.name)), nodes.Keyword('form', form_instance), ], None, None, ) has_body = False if not parser.stream.current.test('block_end'): parser.stream.expect('name:using') if parser.stream.current.test('block_end'): has_body = True if not parser.stream.current.test('block_end'): template_name = parser.parse_expression() if not parser.stream.current.test('block_end'): raise TemplateSyntaxError("Too many arguments", lineno) body = None if has_body: body = parser.parse_statements(['name:endform'], drop_needle=True) else: body = nodes.Include(template_name, True, False) body = [body] node = nodes.Scope(lineno=lineno) assignments = [ nodes.Assign(nodes.Name('form_utils_form', 'store'), form_instance) ] node.body = assignments + body return node.set_lineno(lineno)
def parse(self, parser): # Get the component for the tag name that we matched on tag_name = parser.stream.current[2] component_class = self.environment.components[tag_name] field_names = [f.name for f in dataclasses.fields(component_class)] has_children = CHILDREN_FIELD_NAME in field_names lineno = next(parser.stream).lineno node = nodes.Scope(lineno=lineno) # list of `Pair` nodes for tag properties to update "component" dictionary component_dict_update_items = [] while parser.stream.current.type != 'block_end': lineno = parser.stream.current.lineno if component_dict_update_items: parser.stream.expect('comma') name = parser.stream.expect('name') parser.stream.expect('assign') value = parser.parse_expression() component_dict_update_items.append( nodes.Pair(nodes.Const(name.value), value)) # dictionary initialization in the "component" name prepare_component_dict = [ self._initialize_component_dict(component_class, lineno) ] if component_dict_update_items: component_dict_delta = nodes.Dict(component_dict_update_items) # `Getattr` for "update" function of the dictionary "component" update_component_dict_fun = nodes.Getattr( nodes.Name(TMP_COMPONENT_DICT_NAME, 'load'), 'update', 'load') # `Call` for `component.update(<prop name>, <prop value>)` call_component_dict_update = nodes.Call(update_component_dict_fun, [component_dict_delta], [], None, None) prepare_component_dict.append( nodes.ExprStmt(call_component_dict_update)) # assign `component = __component` and `__component = None` prepare_component_dict.extend([ nodes.Assign(nodes.Name(COMPONENT_DICT_NAME, 'store', lineno=lineno), nodes.Name(TMP_COMPONENT_DICT_NAME, 'load', lineno=lineno), lineno=lineno), nodes.Assign(nodes.Name(TMP_COMPONENT_DICT_NAME, 'store', lineno=lineno), nodes.Const(None, lineno=lineno), lineno=lineno) ]) if has_children: inner_block = list( parser.parse_statements(('name:end' + tag_name, ), drop_needle=True)) # create children() macro children_macro = nodes.Macro() children_macro.name = CHILDREN_MACRO_NAME children_macro.args = [] children_macro.defaults = [] children_macro.body = inner_block children_macro_nodes = [children_macro] else: children_macro_nodes = [] # include tag template include_tag = nodes.Include() # use `template` item of the "component" dictionary for template path include_tag.template = nodes.Getitem(nodes.Name(COMPONENT_DICT_NAME, 'load', lineno=lineno), nodes.Const(TEMPLATE_FIELD_NAME, lineno=lineno), 'load', lineno=lineno) include_tag.ignore_missing = False include_tag.with_context = True node.body = prepare_component_dict + children_macro_nodes + [ include_tag, ] return node
def parse_autoescape(self): node = nodes.ScopedEvalContextModifier(lineno=next(self.stream).lineno) node.options = [nodes.Keyword("autoescape", self.parse_expression())] node.body = self.parse_statements(("name:endautoescape", ), drop_needle=True) return nodes.Scope([node])