From 7a4e03657e229ca60ec2fabd5eca9fc2eaa9271e Mon Sep 17 00:00:00 2001 From: Drew Lipiecki Date: Wed, 8 Feb 2017 13:25:41 -0800 Subject: [PATCH] [gyb] added feature to gyb.py to search for an process a new template tag, when encountered in AST it is processed in line --- utils/gyb.py | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/utils/gyb.py b/utils/gyb.py index 8f26694484c78..0acfce33168fb 100755 --- a/utils/gyb.py +++ b/utils/gyb.py @@ -50,9 +50,8 @@ def split_lines(s): """ return [l + '\n' for l in s.split('\n')] - -# text on a line up to the first '$$', '${', or '%%' -literalText = r'(?: [^$\n%] | \$(?![${]) | %(?!%) )*' +# text on a line up to the first '$$', '${', <{, or '%%' +literalText = r'(?: [^$\n%] | \$(?![${]) | \<(?![<{]) | %(?!%) )*' # The part of an '%end' line that follows the '%' sign linesClose = r'[\ \t]* end [\ \t]* (?: \# .* )? $' @@ -85,6 +84,10 @@ def split_lines(s): | (?P \$\{ ) [^}]* \} # Absorb +# Embedded Templates +| (?P \<\{ ) + [^}]* \} + # %% and $$ are literal % and $ respectively | (?P[$%]) (?P=symbol) @@ -503,8 +506,11 @@ def token_generator(self, base_tokens): if not m2: raise ValueError("Invalid block closure") next_pos = m2.end(0) + elif (kind == 'substitutionOpen'): + # skip past the closing '}' + next_pos = close_pos + 1 else: - assert kind == 'substitutionOpen' + assert kind == 'templateOpen' # skip past the closing '}' next_pos = close_pos + 1 @@ -622,6 +628,8 @@ def __init__(self, context): while context.token_kind and not context.close_lines: if context.token_kind == 'literal': node = Literal + elif context.token_kind == 'templateOpen': + node = Template else: node = Code self.children.append(node(context)) @@ -745,6 +753,25 @@ def __str__(self, indent=''): ) + '\n' + indent + '}' return s + self.format_children(indent) +class Template(ASTNode): + + """An AST node that is evaluated as an embedded Template""" + + children = () + filename = None + + def __init__(self, context): + self.filename = context.filename + self.template_filename = os.path.dirname(context.filename) + '/' + context.code_text + self.children += (Block(ParseContext(self.template_filename)),) + context.next_token() + + def execute(self, context): + for x in self.children: + x.execute(context) + + def __str__(self, indent=''): + return self.filename def expand(filename, line_directive=_default_line_directive, **local_bindings): r"""Return the contents of the givepn template file, executed with the given @@ -803,7 +830,6 @@ def expand(filename, line_directive=_default_line_directive, **local_bindings): finally: os.chdir(d) - def parse_template(filename, text=None): r"""Return an AST corresponding to the given template file. @@ -1225,6 +1251,5 @@ def succ(a): args.target.write(execute_template(ast, args.line_directive, **bindings)) - if __name__ == '__main__': main()