def build_def_single_markup(builder: Builder, lineno: int, token: str, value: typing.Any) -> bool: assert token == "def " stmt, nodes = value if len(nodes) > 2: return False ln, token, nodes = nodes[0] if token != "out" or len(nodes) > 1: return False ln, token, value = nodes[0] if token != "markup": return False def_name = stmt[4:stmt.index("(", 5)] builder.add(lineno, stmt) builder.start_block() builder.add(ln, "return " + value) builder.end_block() builder.add( ln + 1, def_name.join(( "super_defs['", "'] = ", "; ", " = local_defs.setdefault('", "', ", ")", )), ) return True
def build_module(builder: Builder, lineno: int, token: str, nodes: typing.List[typing.Any]) -> bool: assert token == "module" for lineno, token, value in nodes: if token == "def ": builder.build_token(lineno, token, value) return True
def build_require(builder: Builder, lineno: int, token: str, variables: typing.List[str]) -> bool: assert token == "require" builder.add( lineno, "; ".join([name + " = ctx['" + name + "']" for name in variables]), ) return True
def build_var(builder: Builder, lineno: int, token: str, value: typing.Any) -> bool: assert token == "var" var, var_filters = value if var_filters: for f in var_filters: var = known_var_filters.get(f, f) + "(" + var + ")" builder.add(lineno, "w(" + var + ")") return True
def build_var(builder: Builder, lineno: int, token: str, value: typing.Any) -> bool: assert token == "var" var, var_filters = value if not var_filters: builder.add(lineno, "w(e(str(" + var + ")))") return True if "safe" in var_filters: builder.add(lineno, "w(" + var + ")") return True return False
def build_from( builder: Builder, lineno: int, token: str, value: typing.Tuple[str, str, str], ) -> bool: assert token == "from " name, var, alias = value builder.add(lineno, alias + " = _i(" + name + ").local_defs['" + var + "']") return True
def build_def_syntax_check(builder: Builder, lineno: int, token: str, value: typing.Any) -> bool: assert token == "def " stmt, nodes = value lineno, token, value = nodes[0] if token in compound_tokens: builder.add(lineno, stmt) builder.start_block() token = token.rstrip() error = """\ The compound statement '%s' is not allowed here. \ Add a line before it with @#ignore or leave it empty. %s @#ignore @%s ...""" % ( token, stmt, token, ) builder.add(lineno, "raise SyntaxError(%s)" % repr(error)) builder.end_block() return True elif len(nodes) > 1: # token before @end lineno, token, value = nodes[-2] if token == "#": del nodes[-2] return False
def build_compound(builder: Builder, lineno: int, token: str, value: typing.Any) -> bool: assert token in compound_tokens stmt, nodes = value builder.add(lineno, stmt) builder.start_block() builder.build_block(nodes) builder.end_block() return True
def build_extends(builder: Builder, lineno: int, token: str, nodes: typing.List[typing.Any]) -> bool: assert token == "render" n = len(nodes) if not n: return False lineno, token, value = nodes[-1] if token != "extends": return False extends, nested = value if n > 1: nested = nodes[:-1] + nested for lineno, token, value in nested: if token in extends_tokens: builder.build_token(lineno, token, value) builder.add( builder.lineno + 1, "return _r(" + extends + ", ctx, local_defs, super_defs)", ) return True
def build_def_empty(builder: Builder, lineno: int, token: str, value: typing.Any) -> bool: assert token == "def " stmt, nodes = value if len(nodes) > 1: return False def_name = stmt[4:stmt.index("(", 5)] builder.add(lineno, stmt) builder.start_block() builder.add(lineno, "return ''") builder.end_block() builder.add( lineno + 1, def_name.join(( "super_defs['", "'] = ", "; ", " = local_defs.setdefault('", "', ", ")", )), ) return True
def build_render(builder: Builder, lineno: int, token: str, nodes: typing.Iterable[Token]) -> bool: assert lineno <= 0 assert token == "render" builder.add(lineno, WRITER_DECLARE) builder.build_block(nodes) lineno = builder.lineno builder.add(lineno + 1, WRITER_RETURN) return True
def build_render_single_markup(builder: Builder, lineno: int, token: str, nodes: typing.List[typing.Any]) -> bool: assert lineno <= 0 assert token == "render" if len(nodes) > 1: return False if len(nodes) == 0: builder.add(lineno, "return ''") return True ln, token, nodes = nodes[0] if token != "out" or len(nodes) > 1: return False ln, token, value = nodes[0] if token != "markup": return False if value: builder.add(ln, "return " + value) else: builder.add(ln, "return ''") return True
def build_markup(builder: Builder, lineno: int, token: str, value: str) -> bool: assert token == "markup" if value: builder.add(lineno, "w(" + value + ")") return True
def build_include(builder: Builder, lineno: int, token: str, value: str) -> bool: assert token == "include" builder.add(lineno, "w(_r(" + value + ", ctx, local_defs, super_defs))") return True
def build_import(builder: Builder, lineno: int, token: str, value: typing.Tuple[str, str]) -> bool: assert token == "import " name, var = value builder.add(lineno, var + " = _i(" + name + ")") return True
def build_comment(builder: Builder, lineno: int, token: str, comment: str) -> bool: assert token == "#" builder.add(lineno, comment) return True
def build_end(builder: Builder, lineno: int, token: str, value: typing.Any) -> bool: if builder.lineno != lineno: builder.add(lineno - 1, "") return True
def build_def(builder: Builder, lineno: int, token: str, value: typing.Any) -> bool: assert token == "def " stmt, nodes = value def_name = stmt[4:stmt.index("(", 5)] builder.add(lineno, stmt) builder.start_block() builder.add(lineno + 1, WRITER_DECLARE) builder.build_block(nodes) lineno = builder.lineno builder.add(lineno, WRITER_RETURN) builder.end_block() builder.add( lineno + 1, def_name.join(( "super_defs['", "'] = ", "; ", " = local_defs.setdefault('", "', ", ")", )), ) return True
def build_code(builder: Builder, lineno: int, token: str, lines: typing.List[str]) -> bool: for line in lines: builder.add(lineno, line) lineno += 1 return True
def build_out(builder: Builder, lineno: int, token: str, nodes: typing.Iterable[Token]) -> bool: assert token == "out" builder.build_block(nodes) return True