Esempio n. 1
0
    def parse(self, parser):
        lineno = next(parser.stream).lineno

        # parase key, path and mount
        key = parser.parse_expression()
        parser.stream.skip_if('comma')
        if parser.stream.skip_if('name:path'):
            parser.stream.skip(1)
            path = parser.parse_expression()
        else:
            path = ""
        parser.stream.skip_if('comma')
        if parser.stream.skip_if('name:mount'):
            parser.stream.skip(1)
            mount = parser.parse_expression()
        else:
            mount = "secret"
        parser.stream.skip_if('comma')
        if parser.stream.skip_if('name:to_file'):
            parser.stream.skip(1)
            to_file = parser.parse_expression()
        else:
            to_file = None

        args = [path, key, mount]
        if to_file:
            args.append(to_file)
        return nodes.Output([
            nodes.MarkSafeIfAutoescape(self.call_method('get_secret', args))
        ]).set_lineno(lineno)
Esempio n. 2
0
    def _make_node(self, singular, plural, variables, plural_expr,
                   vars_referenced, num_called_num):
        """Generates a useful node from the data provided."""
        if not vars_referenced and not self.environment.newstyle_gettext:
            singular = singular.replace('%%', '%')
            if plural:
                plural = plural.replace('%%', '%')
        if plural_expr is None:
            gettext = nodes.Name('gettext', 'load')
            node = nodes.Call(gettext, [nodes.Const(singular)], [], None, None)
        else:
            ngettext = nodes.Name('ngettext', 'load')
            node = nodes.Call(
                ngettext,
                [nodes.Const(singular),
                 nodes.Const(plural), plural_expr], [], None, None)
        if self.environment.newstyle_gettext:
            for key, value in variables.iteritems():
                if num_called_num and key == 'num':
                    continue
                node.kwargs.append(nodes.Keyword(key, value))

        else:
            node = nodes.MarkSafeIfAutoescape(node)
            if variables:
                node = nodes.Mod(
                    node,
                    nodes.Dict([
                        nodes.Pair(nodes.Const(key), value)
                        for key, value in variables.items()
                    ]))
        return nodes.Output([node])
Esempio n. 3
0
    def parse(self, parser):
        line_no = next(parser.stream).lineno
        tag_args = [parser.parse_expression()]

        # If there is a comma, the user provided a text substitutions dict
        if parser.stream.skip_if('comma'):
            tag_args.append(parser.parse_expression())

        return nodes.Output([
            nodes.MarkSafeIfAutoescape(self.call_method('to_html', tag_args))
        ]).set_lineno(line_no)
Esempio n. 4
0
    def _make_node(
        self, singular, plural, variables, plural_expr, vars_referenced, num_called_num
    ):
        """Generates a useful node from the data provided."""
        # no variables referenced?  no need to escape for old style
        # gettext invocations only if there are vars.
        if not vars_referenced and not self.environment.newstyle_gettext:
            singular = singular.replace("%%", "%")
            if plural:
                plural = plural.replace("%%", "%")

        # singular only:
        if plural_expr is None:
            gettext = nodes.Name("gettext", "load")
            node = nodes.Call(gettext, [nodes.Const(singular)], [], None, None)

        # singular and plural
        else:
            ngettext = nodes.Name("ngettext", "load")
            node = nodes.Call(
                ngettext,
                [nodes.Const(singular), nodes.Const(plural), plural_expr],
                [],
                None,
                None,
            )

        # in case newstyle gettext is used, the method is powerful
        # enough to handle the variable expansion and autoescape
        # handling itself
        if self.environment.newstyle_gettext:
            for key, value in iteritems(variables):
                # the function adds that later anyways in case num was
                # called num, so just skip it.
                if num_called_num and key == "num":
                    continue
                node.kwargs.append(nodes.Keyword(key, value))

        # otherwise do that here
        else:
            # mark the return value as safe if we are in an
            # environment with autoescaping turned on
            node = nodes.MarkSafeIfAutoescape(node)
            if variables:
                node = nodes.Mod(
                    node,
                    nodes.Dict(
                        [
                            nodes.Pair(nodes.Const(key), value)
                            for key, value in variables.items()
                        ]
                    ),
                )
        return nodes.Output([node])
Esempio n. 5
0
    def parse(self, parser):
        """
        Parse Jinja statement tag defined in `self.tags` (default: css, js).

        This accually tries to build corresponding html script tag
        or collect script file name in jinja2 environment variable.
        If you use bundles it is important to call ``get_css_bundle``
        or ``get_js_bundle`` in template after all occurrences of
        script tags (e.g. {% css ... %}, {% js ...%}).
        """
        tag = parser.stream.current.value
        lineno = next(parser.stream).lineno

        default_bundle_name = u"%s" % (self.environment.default_bundle_name)
        default_bundle_name.encode('utf-8')
        bundle_name = nodes.Const(default_bundle_name)

        #parse filename
        if parser.stream.current.type != 'block_end':
            value = parser.parse_expression()
            # get first optional argument: bundle_name
            if parser.stream.skip_if('comma'):
                bundle_name = parser.parse_expression()
                if isinstance(bundle_name, nodes.Name):
                    bundle_name = nodes.Name(bundle_name.name, 'load')
        else:
            value = parser.parse_tuple()

        args = [nodes.Const(tag), value, bundle_name]

        # Return html tag with link to corresponding script file.
        if self.environment.use_bundle is False:
            value = value.value
            if callable(self.environment.collection_templates[tag]):
                node = self.environment.collection_templates[tag](value)
            else:
                node = self.environment.collection_templates[tag] % value
            return nodes.Output(
                [nodes.MarkSafeIfAutoescape(nodes.Const(node))])

        # Call :meth:`_update` to collect names of used scripts.
        return nodes.CallBlock(
            self.call_method('_update', args=args, lineno=lineno), [], [], '')