Search in sources :

Example 16 with PyStringExpr

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);
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) SoyPySrcPrintDirective(com.google.template.soy.pysrc.restricted.SoyPySrcPrintDirective) SoyPrintDirective(com.google.template.soy.shared.restricted.SoyPrintDirective) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr)

Example 17 with PyStringExpr

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)));
}
Also used : IfElseNode(com.google.template.soy.soytree.IfElseNode) SoyNode(com.google.template.soy.soytree.SoyNode) ParentSoyNode(com.google.template.soy.soytree.SoyNode.ParentSoyNode) IfCondNode(com.google.template.soy.soytree.IfCondNode) PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr)

Example 18 with PyStringExpr

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));
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr) Test(org.junit.Test)

Example 19 with PyStringExpr

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'");
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr) Test(org.junit.Test)

Example 20 with PyStringExpr

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)));
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr) Test(org.junit.Test)

Aggregations

PyStringExpr (com.google.template.soy.pysrc.restricted.PyStringExpr)22 PyExpr (com.google.template.soy.pysrc.restricted.PyExpr)21 Test (org.junit.Test)14 LinkedHashMap (java.util.LinkedHashMap)3 MsgPluralNode (com.google.template.soy.soytree.MsgPluralNode)2 SoyNode (com.google.template.soy.soytree.SoyNode)2 ParentSoyNode (com.google.template.soy.soytree.SoyNode.ParentSoyNode)2 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)1 SoyMsgPluralCaseSpec (com.google.template.soy.msgs.restricted.SoyMsgPluralCaseSpec)1 SoyMsgPluralPart (com.google.template.soy.msgs.restricted.SoyMsgPluralPart)1 PyFunctionExprBuilder (com.google.template.soy.pysrc.restricted.PyFunctionExprBuilder)1 PyListExpr (com.google.template.soy.pysrc.restricted.PyListExpr)1 SoyPySrcPrintDirective (com.google.template.soy.pysrc.restricted.SoyPySrcPrintDirective)1 SoyPrintDirective (com.google.template.soy.shared.restricted.SoyPrintDirective)1 AbstractParentSoyNode (com.google.template.soy.soytree.AbstractParentSoyNode)1 CallNode (com.google.template.soy.soytree.CallNode)1 CallParamContentNode (com.google.template.soy.soytree.CallParamContentNode)1 CallParamNode (com.google.template.soy.soytree.CallParamNode)1 CallParamValueNode (com.google.template.soy.soytree.CallParamValueNode)1 IfCondNode (com.google.template.soy.soytree.IfCondNode)1