Search in sources :

Example 1 with PyStringExpr

use of com.google.template.soy.pysrc.restricted.PyStringExpr in project closure-templates by google.

the class MsgFuncGenerator method collectVarNameListAndToPyExprMap.

/**
 * Private helper to process and collect all variables used within this msg node for code
 * generation.
 *
 * @return A Map populated with all the variables used with in this message node, using {@link
 *     MsgPlaceholderInitialNode#genBasePhName}.
 */
private Map<PyExpr, PyExpr> collectVarNameListAndToPyExprMap() {
    Map<PyExpr, PyExpr> nodePyVarToPyExprMap = new LinkedHashMap<>();
    for (Map.Entry<String, MsgSubstUnitNode> entry : msgNode.getVarNameToRepNodeMap().entrySet()) {
        MsgSubstUnitNode substUnitNode = entry.getValue();
        PyExpr substPyExpr = null;
        if (substUnitNode instanceof MsgPlaceholderNode) {
            SoyNode phInitialNode = ((AbstractParentSoyNode<?>) substUnitNode).getChild(0);
            if (phInitialNode instanceof PrintNode || phInitialNode instanceof CallNode || phInitialNode instanceof RawTextNode) {
                substPyExpr = PyExprUtils.concatPyExprs(genPyExprsVisitor.exec(phInitialNode)).toPyString();
            }
            // when the placeholder is generated by HTML tags
            if (phInitialNode instanceof MsgHtmlTagNode) {
                substPyExpr = PyExprUtils.concatPyExprs(genPyExprsVisitor.execOnChildren((ParentSoyNode<?>) phInitialNode)).toPyString();
            }
        } else if (substUnitNode instanceof MsgPluralNode) {
            // Translates {@link MsgPluralNode#pluralExpr} into a Python lookup expression.
            // Note that {@code pluralExpr} represents the soy expression of the {@code plural} attr,
            // i.e. the {@code $numDrafts} in {@code {plural $numDrafts}...{/plural}}.
            substPyExpr = translateToPyExprVisitor.exec(((MsgPluralNode) substUnitNode).getExpr());
        } else if (substUnitNode instanceof MsgSelectNode) {
            substPyExpr = translateToPyExprVisitor.exec(((MsgSelectNode) substUnitNode).getExpr());
        }
        if (substPyExpr != null) {
            nodePyVarToPyExprMap.put(new PyStringExpr("'" + entry.getKey() + "'"), substPyExpr);
        }
    }
    return nodePyVarToPyExprMap;
}
Also used : AbstractParentSoyNode(com.google.template.soy.soytree.AbstractParentSoyNode) SoyNode(com.google.template.soy.soytree.SoyNode) ParentSoyNode(com.google.template.soy.soytree.SoyNode.ParentSoyNode) AbstractParentSoyNode(com.google.template.soy.soytree.AbstractParentSoyNode) MsgSelectNode(com.google.template.soy.soytree.MsgSelectNode) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr) MsgSubstUnitNode(com.google.template.soy.soytree.SoyNode.MsgSubstUnitNode) CallNode(com.google.template.soy.soytree.CallNode) LinkedHashMap(java.util.LinkedHashMap) PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) ParentSoyNode(com.google.template.soy.soytree.SoyNode.ParentSoyNode) AbstractParentSoyNode(com.google.template.soy.soytree.AbstractParentSoyNode) MsgHtmlTagNode(com.google.template.soy.soytree.MsgHtmlTagNode) PrintNode(com.google.template.soy.soytree.PrintNode) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) RawTextNode(com.google.template.soy.soytree.RawTextNode) MsgPlaceholderNode(com.google.template.soy.soytree.MsgPlaceholderNode) MsgPluralNode(com.google.template.soy.soytree.MsgPluralNode)

Example 2 with PyStringExpr

use of com.google.template.soy.pysrc.restricted.PyStringExpr in project closure-templates by google.

the class GenPyCallExprVisitor method visitCallDelegateNode.

/**
 * Visits a delegate call node and builds the call expression to retrieve the function and execute
 * it. The get_delegate_fn returns the function directly, so its output can be called directly.
 *
 * @param node The delegate call node.
 * @return The call Python expression.
 */
@Override
protected PyExpr visitCallDelegateNode(CallDelegateNode node) {
    ExprRootNode variantSoyExpr = node.getDelCalleeVariantExpr();
    PyExpr variantPyExpr;
    if (variantSoyExpr == null) {
        // Case 1: Delegate call with empty variant.
        variantPyExpr = new PyStringExpr("''");
    } else {
        // Case 2: Delegate call with variant expression.
        TranslateToPyExprVisitor translator = new TranslateToPyExprVisitor(localVarStack, errorReporter);
        variantPyExpr = translator.exec(variantSoyExpr);
    }
    String calleeExprText = new PyFunctionExprBuilder("runtime.get_delegate_fn").addArg(node.getDelCalleeName()).addArg(variantPyExpr).addArg(node.allowEmptyDefault()).build();
    String callExprText = calleeExprText + "(" + genObjToPass(node) + ", ijData)";
    return escapeCall(callExprText, node.getEscapingDirectives());
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) PyFunctionExprBuilder(com.google.template.soy.pysrc.restricted.PyFunctionExprBuilder) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Example 3 with PyStringExpr

use of com.google.template.soy.pysrc.restricted.PyStringExpr in project closure-templates by google.

the class GenPyExprsVisitor method visitRawTextNode.

// -----------------------------------------------------------------------------------------------
// Implementations for specific nodes.
/**
 * Example:
 *
 * <pre>
 *   I'm feeling lucky!
 * </pre>
 *
 * generates
 *
 * <pre>
 *   'I\'m feeling lucky!'
 * </pre>
 */
@Override
protected void visitRawTextNode(RawTextNode node) {
    // Escape special characters in the text before writing as a string.
    String exprText = BaseUtils.escapeToSoyString(node.getRawText(), false, QuoteStyle.SINGLE);
    pyExprs.add(new PyStringExpr(exprText));
}
Also used : PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr)

Example 4 with PyStringExpr

use of com.google.template.soy.pysrc.restricted.PyStringExpr in project closure-templates by google.

the class StrSubFunction method computeForPySrc.

@Override
public PyExpr computeForPySrc(List<PyExpr> args) {
    // Coerce SanitizedContent args to strings.
    String base = args.get(0).toPyString().getText();
    PyExpr start = args.get(1);
    PyExpr end = args.size() == 3 ? args.get(2) : null;
    return new PyStringExpr("(" + base + ")[" + start.getText() + ":" + (end != null ? end.getText() : "") + "]");
}
Also used : PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr)

Example 5 with PyStringExpr

use of com.google.template.soy.pysrc.restricted.PyStringExpr in project closure-templates by google.

the class StrSubFunctionTest method testComputeForPySrc_nonStringInput.

@Test
public void testComputeForPySrc_nonStringInput() {
    StrSubFunction strSub = new StrSubFunction();
    PyExpr base = new PyExpr("foobar", Integer.MAX_VALUE);
    PyExpr start = new PyExpr("3", Integer.MAX_VALUE);
    assertThat(strSub.computeForPySrc(ImmutableList.of(base, start))).isEqualTo(new PyStringExpr("(str(foobar))[3:]", 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)

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