Search in sources :

Example 1 with PrintDirectiveNode

use of com.google.template.soy.soytree.PrintDirectiveNode in project closure-templates by google.

the class SoyNodeCompiler method visitLoggingFunction.

private Statement visitLoggingFunction(PrintNode node, FunctionNode fn, LoggingFunction loggingFunction) {
    List<Expression> printDirectives = new ArrayList<>(node.numChildren());
    for (PrintDirectiveNode child : node.getChildren()) {
        // sanity
        checkState(child.getArgs().isEmpty());
        printDirectives.add(parameterLookup.getRenderContext().getEscapingDirectiveAsFunction(child.getName()));
    }
    Label reattachPoint = new Label();
    return appendableExpression.appendLoggingFunctionInvocation(loggingFunction.getName(), loggingFunction.getPlaceholder(), exprCompiler.asBasicCompiler(reattachPoint).compileToList(fn.getChildren()), printDirectives).labelStart(reattachPoint).toStatement();
}
Also used : PrintDirectiveNode(com.google.template.soy.soytree.PrintDirectiveNode) SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Expression(com.google.template.soy.jbcsrc.restricted.Expression) ArrayList(java.util.ArrayList) Label(org.objectweb.asm.Label)

Example 2 with PrintDirectiveNode

use of com.google.template.soy.soytree.PrintDirectiveNode in project closure-templates by google.

the class SoyNodeCompiler method compilePrintNodeAsExpression.

private SoyExpression compilePrintNodeAsExpression(PrintNode node, Label reattachPoint) {
    BasicExpressionCompiler basic = exprCompiler.asBasicCompiler(reattachPoint);
    SoyExpression value = basic.compile(node.getExpr());
    // because instead of wrapping the soy value, we would just wrap the appendable.
    for (PrintDirectiveNode printDirective : node.getChildren()) {
        value = parameterLookup.getRenderContext().applyPrintDirective(printDirective.getPrintDirective(), value, basic.compileToList(printDirective.getArgs()));
    }
    return value;
}
Also used : SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) PrintDirectiveNode(com.google.template.soy.soytree.PrintDirectiveNode) BasicExpressionCompiler(com.google.template.soy.jbcsrc.ExpressionCompiler.BasicExpressionCompiler)

Example 3 with PrintDirectiveNode

use of com.google.template.soy.soytree.PrintDirectiveNode in project closure-templates by google.

the class GenPyExprsVisitor method visitPrintNode.

/**
 * Visiting a print node accomplishes 3 basic tasks. It loads data, it performs any operations
 * needed, and it executes the appropriate print directives.
 *
 * <p>TODO(dcphillips): Add support for local variables once LetNode are supported.
 *
 * <p>Example:
 *
 * <pre>
 *   {$boo |changeNewlineToBr}
 *   {$goo + 5}
 * </pre>
 *
 * might generate
 *
 * <pre>
 *   sanitize.change_newline_to_br(data.get('boo'))
 *   data.get('goo') + 5
 * </pre>
 */
@Override
protected void visitPrintNode(PrintNode node) {
    TranslateToPyExprVisitor translator = new TranslateToPyExprVisitor(localVarExprs, errorReporter);
    PyExpr pyExpr = translator.exec(node.getExpr());
    // Process directives.
    for (PrintDirectiveNode directiveNode : node.getChildren()) {
        // Get directive.
        SoyPrintDirective directive = directiveNode.getPrintDirective();
        if (!(directive instanceof SoyPySrcPrintDirective)) {
            errorReporter.report(directiveNode.getSourceLocation(), UNKNOWN_SOY_PY_SRC_PRINT_DIRECTIVE, directiveNode.getName());
            continue;
        }
        // Get directive args.
        List<ExprRootNode> args = directiveNode.getArgs();
        // Translate directive args.
        List<PyExpr> argsPyExprs = new ArrayList<>(args.size());
        for (ExprRootNode arg : args) {
            argsPyExprs.add(translator.exec(arg));
        }
        // Apply directive.
        pyExpr = ((SoyPySrcPrintDirective) directive).applyForPySrc(pyExpr, argsPyExprs);
    }
    pyExprs.add(pyExpr);
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) SoyPySrcPrintDirective(com.google.template.soy.pysrc.restricted.SoyPySrcPrintDirective) PrintDirectiveNode(com.google.template.soy.soytree.PrintDirectiveNode) SoyPrintDirective(com.google.template.soy.shared.restricted.SoyPrintDirective) ArrayList(java.util.ArrayList) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Example 4 with PrintDirectiveNode

use of com.google.template.soy.soytree.PrintDirectiveNode in project closure-templates by google.

the class TemplateParserTest method testParsePrintStmt.

@Test
public void testParsePrintStmt() throws Exception {
    String templateBody = "{@param boo : ?}{@param goo : ?}\n" + "  {$boo.foo}{$boo.foo}\n" + "  {$goo + 1 |noAutoescape}\n" + "  {print 'blah    blahblahblah' |escapeHtml|insertWordBreaks:8}\n";
    List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
    assertEquals(4, nodes.size());
    PrintNode pn0 = (PrintNode) nodes.get(0);
    assertEquals("$boo.foo", pn0.getExpr().toSourceString());
    assertEquals(0, pn0.getChildren().size());
    assertEquals("FOO", pn0.genBasePhName());
    assertEquals("{$boo.foo}", pn0.toSourceString());
    assertTrue(pn0.getExpr().getRoot() instanceof FieldAccessNode);
    PrintNode pn1 = (PrintNode) nodes.get(1);
    assertTrue(pn0.genSamenessKey().equals(pn1.genSamenessKey()));
    assertTrue(pn1.getExpr().getRoot() instanceof FieldAccessNode);
    PrintNode pn2 = (PrintNode) nodes.get(2);
    assertEquals("$goo + 1", pn2.getExpr().toSourceString());
    assertEquals(1, pn2.getChildren().size());
    PrintDirectiveNode pn2d0 = pn2.getChild(0);
    assertEquals("|noAutoescape", pn2d0.getName());
    assertEquals("XXX", pn2.genBasePhName());
    assertTrue(pn2.getExpr().getRoot() instanceof PlusOpNode);
    PrintNode pn3 = (PrintNode) nodes.get(3);
    assertEquals("'blah    blahblahblah'", pn3.getExpr().toSourceString());
    assertEquals(2, pn3.getChildren().size());
    PrintDirectiveNode pn3d0 = pn3.getChild(0);
    assertEquals("|escapeHtml", pn3d0.getName());
    PrintDirectiveNode pn3d1 = pn3.getChild(1);
    assertEquals("|insertWordBreaks", pn3d1.getName());
    assertEquals(8, ((IntegerNode) pn3d1.getArgs().get(0).getRoot()).getValue());
    assertEquals("XXX", pn3.genBasePhName());
    assertTrue(pn3.getExpr().getRoot() instanceof StringNode);
    assertFalse(pn0.genSamenessKey().equals(pn2.genSamenessKey()));
    assertFalse(pn3.genSamenessKey().equals(pn0.genSamenessKey()));
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) PrintDirectiveNode(com.google.template.soy.soytree.PrintDirectiveNode) StringNode(com.google.template.soy.exprtree.StringNode) PlusOpNode(com.google.template.soy.exprtree.OperatorNodes.PlusOpNode) PrintNode(com.google.template.soy.soytree.PrintNode) FieldAccessNode(com.google.template.soy.exprtree.FieldAccessNode) Test(org.junit.Test)

Example 5 with PrintDirectiveNode

use of com.google.template.soy.soytree.PrintDirectiveNode in project closure-templates by google.

the class GenJsExprsVisitor method visitPrintNode.

/**
 * Example:
 * <pre>
 *   {$boo.foo}
 *   {$goo.moo + 5}
 * </pre>
 * might generate
 * <pre>
 *   opt_data.boo.foo
 *   gooData4.moo + 5
 * </pre>
 */
@Override
protected void visitPrintNode(PrintNode node) {
    CodeChunk.WithValue expr = translateExpr(node.getExpr());
    // Process directives.
    for (PrintDirectiveNode directiveNode : node.getChildren()) {
        // Get directive.
        SoyPrintDirective directive = directiveNode.getPrintDirective();
        if (!(directive instanceof SoyJsSrcPrintDirective)) {
            errorReporter.report(node.getSourceLocation(), UNKNOWN_SOY_JS_SRC_PRINT_DIRECTIVE, directiveNode.getName());
            return;
        }
        // Get directive args.
        List<ExprRootNode> argNodes = directiveNode.getArgs();
        // Convert args to CodeChunks.
        List<CodeChunk.WithValue> argChunks = new ArrayList<>(argNodes.size());
        for (ExprRootNode argNode : argNodes) {
            argChunks.add(translateExpr(argNode));
        }
        // Apply directive.
        expr = SoyJsPluginUtils.applyDirective(translationContext.codeGenerator(), expr, (SoyJsSrcPrintDirective) directive, argChunks);
    }
    chunks.add(expr);
}
Also used : PrintDirectiveNode(com.google.template.soy.soytree.PrintDirectiveNode) WithValue(com.google.template.soy.jssrc.dsl.CodeChunk.WithValue) CodeChunk(com.google.template.soy.jssrc.dsl.CodeChunk) SoyPrintDirective(com.google.template.soy.shared.restricted.SoyPrintDirective) ArrayList(java.util.ArrayList) SoyJsSrcPrintDirective(com.google.template.soy.jssrc.restricted.SoyJsSrcPrintDirective) WithValue(com.google.template.soy.jssrc.dsl.CodeChunk.WithValue) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Aggregations

PrintDirectiveNode (com.google.template.soy.soytree.PrintDirectiveNode)7 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)3 SoyPrintDirective (com.google.template.soy.shared.restricted.SoyPrintDirective)3 ArrayList (java.util.ArrayList)3 SoyExpression (com.google.template.soy.jbcsrc.restricted.SoyExpression)2 EscapeHtmlDirective (com.google.template.soy.coredirectives.EscapeHtmlDirective)1 SoyValue (com.google.template.soy.data.SoyValue)1 UndefinedData (com.google.template.soy.data.restricted.UndefinedData)1 FieldAccessNode (com.google.template.soy.exprtree.FieldAccessNode)1 PlusOpNode (com.google.template.soy.exprtree.OperatorNodes.PlusOpNode)1 StringNode (com.google.template.soy.exprtree.StringNode)1 BasicExpressionCompiler (com.google.template.soy.jbcsrc.ExpressionCompiler.BasicExpressionCompiler)1 Expression (com.google.template.soy.jbcsrc.restricted.Expression)1 CodeChunk (com.google.template.soy.jssrc.dsl.CodeChunk)1 WithValue (com.google.template.soy.jssrc.dsl.CodeChunk.WithValue)1 SoyJsSrcPrintDirective (com.google.template.soy.jssrc.restricted.SoyJsSrcPrintDirective)1 PyExpr (com.google.template.soy.pysrc.restricted.PyExpr)1 SoyPySrcPrintDirective (com.google.template.soy.pysrc.restricted.SoyPySrcPrintDirective)1 PrintNode (com.google.template.soy.soytree.PrintNode)1 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)1