Exemplo n.º 1
0
    def parse(self, parser):
        _db = self.environment.edits

        # Skip begining node
        parser.stream.__next__()

        # Get section key
        key = parser.parse_expression().name

        # Read editable section
        section = parser.parse_statements(['name:endeditable'], drop_needle=True)

        # Render original section contents
        compiled = self.environment.compile(Template(section), '', '')
        original = self.environment.template_class.from_code(self.environment, compiled, {}, True).render()

        _db.setdefault(parser.name, OrderedDict())
        _db[parser.name].setdefault(key, OrderedDict())
        _db[parser.name][key].setdefault('original', original.strip())
        _db[parser.name][key].setdefault('edited', None)

        if _db[parser.name][key].get('edited', None):
            if self.environment.edits_preview:
                if self.environment.globals['request'].args.get('preview'):
                    return Output([TemplateData(_db[parser.name][key]['edited'])])
                else:
                    return section
            else:
                return Output([TemplateData(_db[parser.name][key]['edited'])])

        return section
Exemplo n.º 2
0
    def parse(self, parser):
        """
        Parse a placeholder!

        :param parser: Template parser
        :type parser: jinja2.parser.Parser
        :return: Output node for rendering a placeholder.
        :rtype: jinja2.nodes.Output
        """
        lineno = next(parser.stream).lineno
        global_type = bool(parser.stream.look().value == "global")
        if global_type:
            # Do some special-case parsing for global placeholders
            placeholder_name = six.text_type(parser.stream.current.value)
            next(parser.stream)
            next(parser.stream)
        else:
            placeholder_name = six.text_type(
                parse_constantlike(self.environment, parser))
        self._new_layout(parser, placeholder_name)
        parser.parse_statements(['name:endplaceholder'], drop_needle=True)
        # Body parsing will have, as a side effect, populated the current layout
        layout = self._end_layout(parser)
        args = [
            Const(placeholder_name),
            Const(layout),
            Const(parser.name),
            Const(global_type),
        ]
        return Output([self.call_method('_render_placeholder',
                                        args)]).set_lineno(lineno)
Exemplo n.º 3
0
    def parse(self, parser):
        def parse_arguments():
            args = [parser.parse_expression()]
            # append task filters if any
            if parser.stream.skip_if('comma'):
                args.append(parser.parse_expression())
            return args

        lineno = next(parser.stream).lineno
        tag_args = parse_arguments()
        call = self.call_method(self._include_tasks.__name__, tag_args)
        return Output([call], lineno=lineno)
Exemplo n.º 4
0
    def parse(self, parser):
        """
        Parse a placeholder!

        :param parser: Template parser
        :type parser: jinja2.parser.Parser
        :return: Output node for rendering a placeholder.
        :rtype: jinja2.nodes.Output
        """
        lineno = next(parser.stream).lineno
        placeholder_name = six.text_type(
            parse_constantlike(self.environment, parser))
        self._new_layout(parser, placeholder_name)
        parser.parse_statements(['name:endplaceholder'], drop_needle=True)
        # Body parsing will have, as a side effect, populated the current layout
        layout = self._end_layout(parser)
        args = [Const(placeholder_name), Const(layout), Const(parser.name)]
        return Output([self.call_method('_render_placeholder',
                                        args)]).set_lineno(lineno)
Exemplo n.º 5
0
Arquivo: dbg.py Projeto: mcbeet/beet
    def parse(self, parser: Any) -> Node:
        renderer = self.ctx.inject(DbgRenderer)

        lineno = next(parser.stream).lineno

        level = next(parser.stream)
        if level.test_any(*[f"name:{level}"
                            for level in renderer.level_ranks]):
            mode = next(parser.stream)
        else:
            mode = level
            level = None

        name = parser.parse_expression()
        target = None

        if mode.test_any("name:score", "name:storage", "name:entity",
                         "name:block"):
            parser.stream.expect("comma")
            target = parser.parse_expression()
        elif not mode.test("name:print"):
            parser.fail(f"Invalid mode {mode.value!r}.", mode.lineno)

        return Output(
            [
                self.call_method(
                    "_dbg_handler",
                    [
                        TemplateData(level.value if level else ""),
                        TemplateData(mode.value),
                        name,
                        target if target else TemplateData(""),
                        TemplateData(lineno),
                    ],
                    lineno=lineno,
                )
            ],
            lineno=lineno,
        )
Exemplo n.º 6
0
 def parse(self, parser):
     lineno = next(parser.stream).lineno
     args = [parser.parse_expression()]
     cm = self.call_method('_markdown', args)
     return Output([cm], lineno=lineno)
Exemplo n.º 7
0
    def parse(self, parser):
        token = next(parser.stream)

        if token.value == 'paste_dump':
            content_type = parser.stream.expect('name').value
            return Output([
                MarkSafe(
                    self.call_method('_render_paste_dump_queue',
                                     args=(Const(content_type), ),
                                     lineno=token.lineno))
            ],
                          lineno=token.lineno)
        elif token.value == "paste_jam":
            args = self._extract_stream_args(parser)
            content_types = (
                k for k in args.keys()
                if content_type_helper.filename_to_content_type('.' + k))
            node_output = []
            agnostic_arglist = self._parse_jam_args(args)
            agnostic_raw_arglist = self._parse_jam_args(args, output_raw=True)
            for ctype in content_types:
                ctype_arglist = self._parse_jam_args(args, content_type=ctype)
                ctype_raw_arglist = self._parse_jam_args(args,
                                                         content_type=ctype,
                                                         output_raw=True)

                if args.get(ctype + ':preserve', args.get('preserve', False)):
                    node_output.append(
                        TemplateData(''.join(
                            self._create_paste_jam(
                                self._get_paste_jammed_modules(ctype),
                                args.get(ctype), ctype,
                                **self._coalesce_ast_jam_args(
                                    dict(agnostic_raw_arglist,
                                         **ctype_raw_arglist),
                                    content_type=ctype)))))
                else:
                    node_output.append(
                        self.call_method(
                            "_append_to_paste_dump_queue",
                            args=(Dict([
                                Pair(Const('content_type'), Const(ctype)),
                                Pair(Const('dependencies'),
                                     Const(args.get(ctype))),
                                Pair(Const('args'),
                                     Dict(agnostic_arglist + ctype_arglist))
                            ]), ),
                            lineno=token.lineno))
                    node_output.append(Const(''))

            return Output(node_output, lineno=token.lineno)

        # NOTE: the following is deprecated
        elif token.value == 'paste':
            # {% paste %} tag that brings in all configured modules
            log.warning(
                '{% paste %} is deprecated. upgrade to: {% paste_dump <content_type> %}'
            )
            content_type = 'js'
            return Output([
                MarkSafe(
                    self.call_method('_render_paste_dump_queue',
                                     args=(Const(content_type), ),
                                     lineno=token.lineno))
            ],
                          lineno=token.lineno)

        elif token.value == 'paste_require':
            log.warning(
                '{% paste_require %} is deprecated. upgrade to: {% paste_jam <content_type>="<dependencies>" %}'
            )
            module_names = parser.parse_expression().value
            args = self._extract_stream_args(parser)
            content_type = 'js'
            agnostic_arglist = self._parse_jam_args(args)
            agnostic_raw_arglist = self._parse_jam_args(args, output_raw=True)
            ctype_arglist = self._parse_jam_args(args,
                                                 content_type=content_type)
            ctype_raw_arglist = self._parse_jam_args(args,
                                                     content_type=content_type,
                                                     output_raw=True)

            if args.get('preserve', False):
                return Output([
                    TemplateData(''.join(
                        self._create_paste_jam(
                            self._get_paste_jammed_modules(content_type),
                            module_names, content_type,
                            **self._coalesce_ast_jam_args(
                                dict(agnostic_raw_arglist, **
                                     ctype_raw_arglist), content_type))))
                ],
                              lineno=token.lineno)
            else:
                return Output([
                    self.call_method(
                        "_append_to_paste_dump_queue",
                        args=(Dict([
                            Pair(Const('content_type'), Const(content_type)),
                            Pair(Const('dependencies'), Const(module_names)),
                            Pair(Const('args'),
                                 Dict(agnostic_arglist + ctype_arglist))
                        ]), ),
                        lineno=token.lineno),
                    Const('')
                ],
                              lineno=token.lineno)