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();
}
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;
}
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);
}
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()));
}
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);
}
Aggregations