use of com.google.template.soy.pysrc.restricted.PyStringExpr 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);
}
use of com.google.template.soy.pysrc.restricted.PyStringExpr in project closure-templates by google.
the class GenPyExprsVisitor method visitIfNode.
/**
* If all the children are computable as expressions, the IfNode can be written as a ternary
* conditional expression.
*/
@Override
protected void visitIfNode(IfNode node) {
// Create another instance of this visitor for generating Python expressions from children.
GenPyExprsVisitor genPyExprsVisitor = genPyExprsVisitorFactory.create(localVarExprs, errorReporter);
TranslateToPyExprVisitor translator = new TranslateToPyExprVisitor(localVarExprs, errorReporter);
StringBuilder pyExprTextSb = new StringBuilder();
boolean hasElse = false;
for (SoyNode child : node.getChildren()) {
if (child instanceof IfCondNode) {
IfCondNode icn = (IfCondNode) child;
// Python ternary conditional expressions modify the order of the conditional from
// <conditional> ? <true> : <false> to
// <true> if <conditional> else <false>
PyExpr condBlock = PyExprUtils.concatPyExprs(genPyExprsVisitor.exec(icn)).toPyString();
condBlock = PyExprUtils.maybeProtect(condBlock, PyExprUtils.pyPrecedenceForOperator(Operator.CONDITIONAL));
pyExprTextSb.append(condBlock.getText());
// Append the conditional and if/else syntax.
PyExpr condPyExpr = translator.exec(icn.getExpr());
pyExprTextSb.append(" if ").append(condPyExpr.getText()).append(" else ");
} else if (child instanceof IfElseNode) {
hasElse = true;
IfElseNode ien = (IfElseNode) child;
PyExpr elseBlock = PyExprUtils.concatPyExprs(genPyExprsVisitor.exec(ien)).toPyString();
pyExprTextSb.append(elseBlock.getText());
} else {
throw new AssertionError("Unexpected if child node type. Child: " + child);
}
}
if (!hasElse) {
pyExprTextSb.append("''");
}
// By their nature, inline'd conditionals can only contain output strings, so they can be
// treated as a string type with a conditional precedence.
pyExprs.add(new PyStringExpr(pyExprTextSb.toString(), PyExprUtils.pyPrecedenceForOperator(Operator.CONDITIONAL)));
}
use of com.google.template.soy.pysrc.restricted.PyStringExpr in project closure-templates by google.
the class StrIndexOfFunctionTest method testComputeForPySrc_stringInput.
@Test
public void testComputeForPySrc_stringInput() {
StrIndexOfFunction strIndexOf = new StrIndexOfFunction();
PyExpr base = new PyStringExpr("'foobar'", Integer.MAX_VALUE);
PyExpr substring = new PyStringExpr("'bar'", Integer.MAX_VALUE);
assertThat(strIndexOf.computeForPySrc(ImmutableList.of(base, substring))).isEqualTo(new PyExpr("('foobar').find('bar')", Integer.MAX_VALUE));
}
use of com.google.template.soy.pysrc.restricted.PyStringExpr in project closure-templates by google.
the class TextDirectiveTest method testApplyForPySrc.
@Test
public void testApplyForPySrc() {
TextDirective textDirective = new TextDirective();
PyExpr pyExpr = new PyExpr("whatever", Integer.MAX_VALUE);
assertThat(textDirective.applyForPySrc(pyExpr, ImmutableList.<PyExpr>of()).getText()).isEqualTo("str(whatever)");
PyExpr stringExpr = new PyStringExpr("'string'", Integer.MAX_VALUE);
assertThat(textDirective.applyForPySrc(stringExpr, ImmutableList.<PyExpr>of()).getText()).isEqualTo("'string'");
}
use of com.google.template.soy.pysrc.restricted.PyStringExpr in project closure-templates by google.
the class StrContainsFunctionTest method testComputeForPySrc_stringInput.
@Test
public void testComputeForPySrc_stringInput() {
StrContainsFunction strContains = new StrContainsFunction();
PyExpr base = new PyStringExpr("'foobar'", Integer.MAX_VALUE);
PyExpr substring = new PyStringExpr("'bar'", Integer.MAX_VALUE);
assertThat(strContains.computeForPySrc(ImmutableList.of(base, substring))).isEqualTo(new PyExpr("('foobar').find('bar') != -1", PyExprUtils.pyPrecedenceForOperator(Operator.NOT_EQUAL)));
}
Aggregations