How to use the mako.lexer.Lexer function in Mako

To help you get started, we’ve selected a few Mako examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zzzeek / mako / test / test_lexer.py View on Github external
def test_ternary_control(self):
        template = """
        % if x:
            hi
        % elif y+7==10:
            there
        % elif lala:
            lala
        % else:
            hi
        % endif
"""
        nodes = Lexer(template).parse()
        self._compare(
            nodes,
            TemplateNode(
                {},
                [
                    Text("\n", (1, 1)),
                    ControlLine("if", "if x:", False, (2, 1)),
                    Text("            hi\n", (3, 1)),
                    ControlLine("elif", "elif y+7==10:", False, (4, 1)),
                    Text("            there\n", (5, 1)),
                    ControlLine("elif", "elif lala:", False, (6, 1)),
                    Text("            lala\n", (7, 1)),
                    ControlLine("else", "else:", False, (8, 1)),
                    Text("            hi\n", (9, 1)),
                    ControlLine("if", "endif", True, (10, 1)),
                ],
github zzzeek / mako / test / test_lexer.py View on Github external
def test_tricky_code_4(self):
        template = """<% foo = "\\"\\\\" %>"""
        nodes = Lexer(template).parse()
        self._compare(
            nodes,
            TemplateNode({}, [Code("""foo = "\\"\\\\" \n""", False, (1, 1))]),
        )
github zzzeek / mako / test / test_lexer.py View on Github external
def test_long_control_lines(self):
        template = """
    % for file in \\
        requestattr['toc'].filenames:
        x
    % endfor
        """
        nodes = Lexer(template).parse()
        self._compare(
            nodes,
            TemplateNode(
                {},
                [
                    Text("\n", (1, 1)),
                    ControlLine(
                        "for",
                        "for file in \\\n        "
                        "requestattr['toc'].filenames:",
                        False,
                        (2, 1),
                    ),
                    Text("        x\n", (4, 1)),
                    ControlLine("for", "endfor", True, (5, 1)),
                    Text("        ", (6, 1)),
github sqlalchemy / mako / test / test_lexer.py View on Github external
def test_control_lines_2(self):
        template = """% for file in requestattr['toc'].filenames:
    x
% endfor
"""
        nodes = Lexer(template).parse()
        self._compare(
            nodes,
            TemplateNode(
                {},
                [
                    ControlLine(
                        "for",
                        "for file in requestattr['toc'].filenames:",
                        False,
                        (1, 1),
                    ),
                    Text("    x\n", (2, 1)),
                    ControlLine("for", "endfor", True, (3, 1)),
                ],
github sqlalchemy / mako / test / test_lexer.py View on Github external
<%def name="y()">
        this is y
    


<%
    result = []
    data = get_data()
    for x in data:
        result.append(x+7)
%>

    result: <%call expr="foo.x(result)"/>
"""
        nodes = Lexer(template).parse()
        self._compare(
            nodes,
            TemplateNode(
                {},
                [
                    Text("\n", (1, 1)),
                    NamespaceTag(
                        "namespace",
                        {"name": "foo"},
                        (2, 1),
                        [
                            Text("\n    ", (2, 24)),
                            DefTag(
                                "def",
                                {"name": "x()"},
                                (3, 5),
github sqlalchemy / mako / test / test_lexer.py View on Github external
def test_def_syntax_2(self):
        template = """
        <%def name="lala">
            hi
        
    """
        self.assertRaises(exceptions.CompileException, Lexer(template).parse)
github rembo10 / headphones / mako / template.py View on Github external
def _compile(template, text, filename, generate_magic_comment):
    lexer = Lexer(text,
                    filename,
                    disable_unicode=template.disable_unicode,
                    input_encoding=template.input_encoding,
                    preprocessor=template.preprocessor)
    node = lexer.parse()
    source = codegen.compile(node,
                            template.uri,
                            filename,
                            default_filters=template.default_filters,
                            buffer_filters=template.buffer_filters,
                            imports=template.imports,
                            source_encoding=lexer.encoding,
                            generate_magic_comment=generate_magic_comment,
                            disable_unicode=template.disable_unicode,
                            strict_undefined=template.strict_undefined,
                            enable_loop=template.enable_loop,
github Southpaw-TACTIC / TACTIC / 3rd_party / python3 / site-packages / mako / ext / extract.py View on Github external
def process_file(self, fileobj):
        template_node = lexer.Lexer(
            fileobj.read(),
            input_encoding=self.config['encoding']).parse()
        for extracted in self.extract_nodes(template_node.get_children()):
            yield extracted
github cloudera / hue / desktop / core / ext-py / Mako-0.7.2 / mako / template.py View on Github external
def _compile(template, text, filename, generate_magic_comment):
    lexer = Lexer(text,
                    filename,
                    disable_unicode=template.disable_unicode,
                    input_encoding=template.input_encoding,
                    preprocessor=template.preprocessor)
    node = lexer.parse()
    source = codegen.compile(node,
                            template.uri,
                            filename,
                            default_filters=template.default_filters,
                            buffer_filters=template.buffer_filters,
                            imports=template.imports,
                            source_encoding=lexer.encoding,
                            generate_magic_comment=generate_magic_comment,
                            disable_unicode=template.disable_unicode,
                            strict_undefined=template.strict_undefined,
                            enable_loop=template.enable_loop,
github doconix / django-mako-plus / django_mako_plus / template / lexer.py View on Github external
###########################################################
###  DMP hook for expression filters. This allows DMP
###  to filter expressions ${...} after all other filters
###  have been run.
###
###  Currently, the use of this hook is HTML autoescaping.
###  Django autoescapes by default, while Mako does not.
###  DMP injects autoescaping to be consistent with Django.
###

MAKO_ESCAPE_REPLACEMENTS = {
    'h': 'django.utils.html.escape',  # uses Django's escape rather than Mako's, which works better with marks
}

class DMPLexer(Lexer):
    '''
    Subclass of Mako's Lexer, which is used during compilation of
    templates.  This subclass injects ExpressionPostProcessor
    as the final filter on every expression.  Overriding append_node()
    is a hack, but it's the only way I can find to hook into Mako's
    compile process without modifying Mako directly.
    '''
    def append_node(self, nodecls, *args, **kwargs):
        # fyi, this method runs on template compilation (not on template render)
        if nodecls == parsetree.Expression:
            # when an Expression, args[1] is a comma-separated string of filters
            # parse the filters and make any DMP replacements for them
            try:
                # this is Mako's ast, not the python one
                filters = [ MAKO_ESCAPE_REPLACEMENTS.get(f, f) for f in ast.ArgumentList(args[1]).args ]
            except Exception as e: