use of com.google.template.soy.pysrc.restricted.SoyPySrcPrintDirective 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.pysrc.restricted.SoyPySrcPrintDirective in project closure-templates by google.
the class GenPyCallExprVisitor method escapeCall.
/**
* Escaping directives might apply to the output of the call node, so wrap the output with all
* required directives.
*
* @param callExpr The expression text of the call itself.
* @param directives The list of the directives to be applied to the call.
* @return A PyExpr containing the call expression with all directives applied.
*/
private PyExpr escapeCall(String callExpr, ImmutableList<SoyPrintDirective> directives) {
PyExpr escapedExpr = new PyExpr(callExpr, Integer.MAX_VALUE);
if (directives.isEmpty()) {
return escapedExpr;
}
// Successively wrap each escapedExpr in various directives.
for (SoyPrintDirective directive : directives) {
Preconditions.checkState(directive instanceof SoyPySrcPrintDirective, "Autoescaping produced a bogus directive: %s", directive.getName());
escapedExpr = ((SoyPySrcPrintDirective) directive).applyForPySrc(escapedExpr, ImmutableList.<PyExpr>of());
}
return escapedExpr;
}
use of com.google.template.soy.pysrc.restricted.SoyPySrcPrintDirective in project closure-templates by google.
the class GenPyExprsVisitor method visitMsgFallbackGroupNode.
@Override
protected void visitMsgFallbackGroupNode(MsgFallbackGroupNode node) {
PyExpr msg = generateMsgFunc(node.getMsg());
// MsgFallbackGroupNode could only have one child or two children. See MsgFallbackGroupNode.
if (node.hasFallbackMsg()) {
StringBuilder pyExprTextSb = new StringBuilder();
PyExpr fallbackMsg = generateMsgFunc(node.getFallbackMsg());
// Build Python ternary expression: a if cond else c
pyExprTextSb.append(msg.getText()).append(" if ");
// The fallback message is only used if the first message is not available, but the fallback
// is. So availability of both messages must be tested.
long firstId = MsgUtils.computeMsgIdForDualFormat(node.getMsg());
long secondId = MsgUtils.computeMsgIdForDualFormat(node.getFallbackMsg());
pyExprTextSb.append(PyExprUtils.TRANSLATOR_NAME).append(".is_msg_available(").append(firstId).append(")").append(" or not ").append(PyExprUtils.TRANSLATOR_NAME).append(".is_msg_available(").append(secondId).append(")");
pyExprTextSb.append(" else ").append(fallbackMsg.getText());
msg = new PyStringExpr(pyExprTextSb.toString(), PyExprUtils.pyPrecedenceForOperator(Operator.CONDITIONAL));
}
// Escaping directives apply to messages, especially in attribute context.
for (SoyPrintDirective directive : node.getEscapingDirectives()) {
Preconditions.checkState(directive instanceof SoyPySrcPrintDirective, "Contextual autoescaping produced a bogus directive: %s", directive.getName());
msg = ((SoyPySrcPrintDirective) directive).applyForPySrc(msg, ImmutableList.<PyExpr>of());
}
pyExprs.add(msg);
}
Aggregations