Exemple #1
0
    def parse(self, parser):
        """Parse the {% error %} tag, returning an AST node."""
        tag = next(parser.stream)
        message = parser.parse_expression()

        node = CallBlock(
            self.call_method('_exec_error',
                             [message, Const(tag.lineno)]), [], [], [])
        node.set_lineno(tag.lineno)
        return node
Exemple #2
0
    def parse(self, parser):
        tag = parser.stream.current.value
        parser.stream.next()

        subtags = self._parse_subtags(tag, parser)
        methodname = 'tag_' + tag
        if subtags:
            methodname += '_' + '_'.join(subtags)

        lineno = parser.stream.current.lineno
        content = parser.parse_statements(
          ['name:end' + tag], drop_needle=True)
        node = CallBlock(self.call_method(methodname), [], [], content)
        node.set_lineno(lineno)
        return node
Exemple #3
0
    def parse(self, parser):
        lineno = next(parser.stream).lineno
        message = parser.parse_expression()

        return CallBlock(
            self.call_method('_raise_error', [message], lineno=lineno),
            [], [], []).set_lineno(lineno)
Exemple #4
0
    def parse(self, parser):
        lineno = next(parser.stream).lineno

        # Extract the language name.
        args = [parser.parse_expression()]

        # Extract optional arguments.
        kwarg_names = {
            'line_numbers': 0,
            'use_classes': 0,
            'class': 1,
            'id': 1
        }
        kwargs = {}
        while not parser.stream.current.test('block_end'):
            name = parser.stream.expect('name')
            if name.value not in kwarg_names:
                raise Exception("'%s' is not a valid argument for the code "
                                "highlighting tag." % name.value)
            if kwarg_names[name.value] == 0:
                kwargs[name.value] = Const(True)
            elif parser.stream.skip_if('assign'):
                kwargs[name.value] = parser.parse_expression()

        # body of the block
        body = parser.parse_statements(['name:endhighlight', 'name:endgeshi'],
                                       drop_needle=True)

        return CallBlock(self.call_method('_highlight', args, kwargs), [], [],
                         body).set_lineno(lineno)
Exemple #5
0
 def parse(self, parser):
     next(parser.stream)
     token = parser.stream.expect("integer")
     return CallBlock(self.call_method(
         "_mul", [Const(token.value, lineno=token.lineno)],
         lineno=token.lineno), [], [], [],
                      lineno=token.lineno)
Exemple #6
0
 def parse(self, parser):
     lineno = next(parser.stream).lineno
     name = parser.parse_expression()
     body = parser.parse_statements(['name:endaddtoblock'],
                                    drop_needle=True)
     args = [name]
     return CallBlock(self.call_method('_render_tag', args), [], [],
                      body).set_lineno(lineno)
Exemple #7
0
 def parse(self, parser):
     token = next(parser.stream)
     lineno = token.lineno
     tag = token.value
     name = next(parser.stream).value
     body = parser.parse_statements(["name:end%s" % tag], drop_needle=True)
     return CallBlock(self.call_method('_render_output', args=[ContextReference(), Const(name)]),
                      [], [], body).set_lineno(lineno)
Exemple #8
0
 def parse(self, parser):
     if self._settings is None:
         self._settings = self.environment.globals.get(
             "config").settings_for_tag("markdown")
     lineno = next(parser.stream).lineno
     body = parser.parse_statements([self._end_name], drop_needle=True)
     return CallBlock(self.call_method('_markdown_support'), [], [],
                      body).set_lineno(lineno)
Exemple #9
0
 def parse(self, parser):
     """Parse the {% error %} tag, returning an AST node."""
     lineno = next(parser.stream).lineno
     message = parser.parse_expression()
     node = CallBlock(
         self.call_method('_raise', [message], lineno=lineno),
         [], [], [], lineno=lineno)
     return node
Exemple #10
0
    def parse(self, parser: Any) -> Node:
        lineno = next(parser.stream).lineno

        args = [DerivedContextReference(), parser.parse_expression()]
        body = parser.parse_statements(["name:endfunction"], drop_needle=True)

        handler = self.call_method("_function_handler", args)
        return CallBlock(handler, [], [], body).set_lineno(lineno)
Exemple #11
0
 def parse(self, parser):
     next(parser.stream)
     name = parser.stream.expect("name")
     parser.stream.expect("assign")
     return CallBlock(self.call_method(
         "_set",
         [Const(name.value, lineno=name.lineno),
          parser.parse_expression()],
         lineno=name.lineno), [], [], [],
                      lineno=name.lineno)
Exemple #12
0
    def parse(self, parser):
        tag = parser.stream.current.value
        lineno = next(parser.stream).lineno
        args, kwargs = self.parse_args(parser)
        default_cache_key = (None if parser.filename is None
                             else '%s:%d' % (parser.filename, lineno))
        kwargs.append(Keyword('default_cache_key', Const(default_cache_key),
                              lineno=lineno))
        body = parser.parse_statements(['name:end' + tag], drop_needle=True)

        return CallBlock(self.call_method('cache', args, kwargs),
                         [], [], body).set_lineno(lineno)
Exemple #13
0
    def parse(self, parser):
        # the first token is the token that started the tag.  In our case
        # we only listen to ``'pccache'`` so this will be a name token with
        # `pccache` as value.  We get the line number so that we can give
        # that line number to the nodes we create by hand.
        lineno = next(parser.stream).lineno

        # now we parse a single expression that is used as cache key.
        args = [parser.parse_expression()]

        # now we parse the body of the cache block up to `endpccache` and
        # drop the needle (which would always be `endpccache` in that case)
        body = parser.parse_statements(['name:endpccache', 'name:endcache'],
                                       drop_needle=True)

        # now return a `CallBlock` node that calls our _cache_support
        # helper method on this extension.
        return CallBlock(self.call_method('_cache_support', args), [], [],
                         body).set_lineno(lineno)
Exemple #14
0
    def parse(self, parser: Any) -> Node:
        lineno = next(parser.stream).lineno
        args: List[Any] = [parser.parse_expression()]

        if parser.stream.current.test("name:append"):
            args.append(TemplateData("append"))
            parser.stream.skip()
        elif parser.stream.current.test("name:prepend"):
            args.append(TemplateData("prepend"))
            parser.stream.skip()
        else:
            args.append(TemplateData("replace"))

        body = parser.parse_statements(["name:endfunction"], drop_needle=True)

        return CallBlock(
            self.call_method("_function_handler", args, lineno=lineno),
            [],
            [],
            body,
            lineno=lineno,
        )
Exemple #15
0
 def parse(self, parser):
     lineno = next(parser.stream).lineno
     body = parser.parse_statements(['name:endmarkdown'], drop_needle=True)
     return CallBlock(self.call_method('_markdown_support'), [], [],
                      body).set_lineno(lineno)
Exemple #16
0
 def parse(self, parser):
     lineno = next(parser.stream).lineno
     body = parser.parse_statements(['name:endpre'], drop_needle=True)
     return CallBlock(self.call_method('_format_content'), [], [],
                      body).set_lineno(lineno)
Exemple #17
0
 def parse(self, parser):
     lineno = next(parser.stream).lineno
     args = [parser.parse_expression()]
     body = parser.parse_statements(['name:endpcformat'], drop_needle=True)
     return CallBlock(self.call_method('_format', args), [], [],
                      body).set_lineno(lineno)
Exemple #18
0
 def parse(self, parser):
     lineno = next(parser.stream).lineno
     body = parser.parse_statements(['name:endtable'], drop_needle=True)
     return CallBlock(self.call_method('_process_table'), [], [],
                      body).set_lineno(lineno)
Exemple #19
0
 def parse(self, parser):
     lineno = next(parser.stream).lineno
     name = parser.parse_expression()
     args = [name]
     return CallBlock(self.call_method('_render_tag', args), [], [],
                      []).set_lineno(lineno)