Search in sources :

Example 1 with MsgSelectNode

use of com.google.template.soy.soytree.MsgSelectNode in project closure-templates by google.

the class MsgUtils method buildMsgPartsForChildren.

// -----------------------------------------------------------------------------------------------
// Private helpers for building the list of message parts.
/**
 * Builds the list of SoyMsgParts for all the children of a given parent node.
 *
 * @param parent Can be MsgNode, MsgPluralCaseNode, MsgPluralDefaultNode, MsgSelectCaseNode, or
 *     MsgSelectDefaultNode.
 * @param msgNode The MsgNode containing 'parent'.
 */
private static ImmutableList<SoyMsgPart> buildMsgPartsForChildren(BlockNode parent, MsgNode msgNode) {
    ImmutableList.Builder<SoyMsgPart> msgParts = ImmutableList.builder();
    for (StandaloneNode child : parent.getChildren()) {
        if (child instanceof RawTextNode) {
            String rawText = ((RawTextNode) child).getRawText();
            msgParts.add(SoyMsgRawTextPart.of(rawText));
        } else if (child instanceof MsgPlaceholderNode) {
            PlaceholderInfo placeholder = msgNode.getPlaceholder((MsgPlaceholderNode) child);
            msgParts.add(new SoyMsgPlaceholderPart(placeholder.name(), placeholder.example()));
        } else if (child instanceof MsgPluralNode) {
            msgParts.add(buildMsgPartForPlural((MsgPluralNode) child, msgNode));
        } else if (child instanceof MsgSelectNode) {
            msgParts.add(buildMsgPartForSelect((MsgSelectNode) child, msgNode));
        }
    }
    return msgParts.build();
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) SoyMsgPlaceholderPart(com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart) ImmutableList(com.google.common.collect.ImmutableList) PlaceholderInfo(com.google.template.soy.soytree.MsgNode.PlaceholderInfo) MsgSelectNode(com.google.template.soy.soytree.MsgSelectNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) MsgPlaceholderNode(com.google.template.soy.soytree.MsgPlaceholderNode) MsgPluralNode(com.google.template.soy.soytree.MsgPluralNode) SoyMsgPart(com.google.template.soy.msgs.restricted.SoyMsgPart)

Example 2 with MsgSelectNode

use of com.google.template.soy.soytree.MsgSelectNode 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 3 with MsgSelectNode

use of com.google.template.soy.soytree.MsgSelectNode in project closure-templates by google.

the class TemplateParserTest method testParseMsgStmtWithNestedSelects.

@Test
public void testParseMsgStmtWithNestedSelects() throws Exception {
    String templateBody = "{@param gender1 : ?}{@param gender2: ?}{@param person1 : ?}{@param person2: ?}\n" + "{msg desc=\"A sample nested message\"}\n" + "  {select $gender1}\n" + "    {case 'female'}\n" + "      {select $gender2}\n" + "        {case 'female'}{$person1} added {$person2} and her friends to her circle.\n" + "        {default}{$person1} added {$person2} and his friends to her circle.\n" + "      {/select}\n" + "    {default}\n" + "      {select $gender2}\n" + "        {case 'female'}{$person1} put {$person2} and her friends to his circle.\n" + "        {default}{$person1} put {$person2} and his friends to his circle.\n" + "      {/select}\n" + "  {/select}\n" + "{/msg}\n";
    List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
    assertEquals(1, nodes.size());
    MsgNode mn = ((MsgFallbackGroupNode) nodes.get(0)).getChild(0);
    assertEquals(1, mn.numChildren());
    assertEquals("A sample nested message", mn.getDesc());
    // Outer select
    MsgSelectNode sn = (MsgSelectNode) mn.getChild(0);
    assertEquals("$gender1", sn.getExpr().toSourceString());
    // female and default
    assertEquals(2, sn.numChildren());
    // Outer select: Case 'female'
    MsgSelectCaseNode cnf = (MsgSelectCaseNode) sn.getChild(0);
    assertEquals("female", cnf.getCaseValue());
    // Another select
    assertEquals(1, cnf.numChildren());
    // Outer select: Case 'female': Inner select
    MsgSelectNode sn2 = (MsgSelectNode) cnf.getChild(0);
    assertEquals("$gender2", sn2.getExpr().toSourceString());
    // female and default
    assertEquals(2, sn2.numChildren());
    // Outer select: Case 'female': Inner select: Case 'female'
    MsgSelectCaseNode cnf2 = (MsgSelectCaseNode) sn2.getChild(0);
    assertEquals("female", cnf2.getCaseValue());
    assertEquals(4, cnf2.numChildren());
    // Outer select: Case 'female': Inner select: Case 'female': Placeholder $person1
    MsgPlaceholderNode phn1 = (MsgPlaceholderNode) cnf2.getChild(0);
    assertEquals("{$person1}", phn1.toSourceString());
    // Outer select: Case 'female': Inner select: Case 'female': RawText
    RawTextNode rtn1 = (RawTextNode) cnf2.getChild(1);
    assertEquals(" added ", rtn1.getRawText());
    // Outer select: Case 'female': Inner select: Case 'female': Placeholder $person2
    MsgPlaceholderNode phn2 = (MsgPlaceholderNode) cnf2.getChild(2);
    assertEquals("{$person2}", phn2.toSourceString());
    // Outer select: Case 'female': Inner select: Case 'female': RawText
    RawTextNode rtn2 = (RawTextNode) cnf2.getChild(3);
    assertEquals(" and her friends to her circle.", rtn2.getRawText());
    // Outer select: Case 'female': Inner select: Default
    MsgSelectDefaultNode dn2 = (MsgSelectDefaultNode) sn2.getChild(1);
    assertEquals(4, dn2.numChildren());
    // Outer select: Case 'female': Inner select: Default: Placeholder $person1
    MsgPlaceholderNode phn21 = (MsgPlaceholderNode) dn2.getChild(0);
    assertEquals("{$person1}", phn21.toSourceString());
    // Outer select: Case 'female': Inner select: Default: RawText
    RawTextNode rtn21 = (RawTextNode) dn2.getChild(1);
    assertEquals(" added ", rtn21.getRawText());
    // Outer select: Case 'female': Inner select: Default: Placeholder $person2
    MsgPlaceholderNode phn22 = (MsgPlaceholderNode) dn2.getChild(2);
    assertEquals("{$person2}", phn22.toSourceString());
    // Outer select: Case 'female': Inner select: Default: RawText
    RawTextNode rtn22 = (RawTextNode) dn2.getChild(3);
    assertEquals(" and his friends to her circle.", rtn22.getRawText());
    // Outer select: Default
    MsgSelectDefaultNode dn = (MsgSelectDefaultNode) sn.getChild(1);
    // Another select
    assertEquals(1, dn.numChildren());
    // Outer select: Default: Inner select
    MsgSelectNode sn3 = (MsgSelectNode) dn.getChild(0);
    assertEquals("$gender2", sn3.getExpr().toSourceString());
    // female and default
    assertEquals(2, sn3.numChildren());
    // Outer select: Default: Inner select: Case 'female'
    MsgSelectCaseNode cnf3 = (MsgSelectCaseNode) sn3.getChild(0);
    assertEquals("female", cnf3.getCaseValue());
    assertEquals(4, cnf3.numChildren());
    // Outer select: Default: Inner select: Case 'female': Placeholder $person1
    MsgPlaceholderNode phn3 = (MsgPlaceholderNode) cnf3.getChild(0);
    assertEquals("{$person1}", phn3.toSourceString());
    // Outer select: Default: Inner select: Case 'female': RawText
    RawTextNode rtn3 = (RawTextNode) cnf3.getChild(1);
    assertEquals(" put ", rtn3.getRawText());
    // Outer select: Default: Inner select: Case 'female': Placeholder $person2
    MsgPlaceholderNode phn4 = (MsgPlaceholderNode) cnf3.getChild(2);
    assertEquals("{$person2}", phn4.toSourceString());
    // Outer select: Default: Inner select: Case 'female': RawText
    RawTextNode rtn4 = (RawTextNode) cnf3.getChild(3);
    assertEquals(" and her friends to his circle.", rtn4.getRawText());
    // Outer select: Default: Inner select: Default
    MsgSelectDefaultNode dn3 = (MsgSelectDefaultNode) sn3.getChild(1);
    assertEquals(4, dn3.numChildren());
    // Outer select: Default: Inner select: Default: Placeholder $person1
    MsgPlaceholderNode phn5 = (MsgPlaceholderNode) dn3.getChild(0);
    assertEquals("{$person1}", phn5.toSourceString());
    // Outer select: Default: Inner select: Default: RawText
    RawTextNode rtn5 = (RawTextNode) dn3.getChild(1);
    assertEquals(" put ", rtn5.getRawText());
    // Outer select: Default: Inner select: Default: Placeholder $person2
    MsgPlaceholderNode phn6 = (MsgPlaceholderNode) dn3.getChild(2);
    assertEquals("{$person2}", phn6.toSourceString());
    // Outer select: Default: Inner select: Default: RawText
    RawTextNode rtn6 = (RawTextNode) dn3.getChild(3);
    assertEquals(" and his friends to his circle.", rtn6.getRawText());
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) MsgSelectDefaultNode(com.google.template.soy.soytree.MsgSelectDefaultNode) MsgFallbackGroupNode(com.google.template.soy.soytree.MsgFallbackGroupNode) MsgSelectNode(com.google.template.soy.soytree.MsgSelectNode) MsgSelectCaseNode(com.google.template.soy.soytree.MsgSelectCaseNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) MsgNode(com.google.template.soy.soytree.MsgNode) MsgPlaceholderNode(com.google.template.soy.soytree.MsgPlaceholderNode) Test(org.junit.Test)

Example 4 with MsgSelectNode

use of com.google.template.soy.soytree.MsgSelectNode in project closure-templates by google.

the class RewriteGenderMsgsVisitor method splitMsgForGender.

/**
 * Helper to split a msg for gender, by adding a 'select' node and cloning the msg's contents into
 * all 3 cases of the 'select' node ('female'/'male'/default).
 *
 * @param msg The message to split.
 * @param genderExpr The expression for the gender value.
 * @param baseSelectVarName The base select var name to use, or null if it should be generated
 *     from the gender expression.
 */
private void splitMsgForGender(MsgNode msg, ExprRootNode genderExpr, @Nullable String baseSelectVarName) {
    List<StandaloneNode> origChildren = ImmutableList.copyOf(msg.getChildren());
    msg.clearChildren();
    MsgSelectCaseNode femaleCase = new MsgSelectCaseNode(nodeIdGen.genId(), msg.getSourceLocation(), "female");
    femaleCase.addChildren(SoyTreeUtils.cloneListWithNewIds(origChildren, nodeIdGen));
    MsgSelectCaseNode maleCase = new MsgSelectCaseNode(nodeIdGen.genId(), msg.getSourceLocation(), "male");
    maleCase.addChildren(SoyTreeUtils.cloneListWithNewIds(origChildren, nodeIdGen));
    MsgSelectDefaultNode defaultCase = new MsgSelectDefaultNode(nodeIdGen.genId(), msg.getSourceLocation());
    defaultCase.addChildren(SoyTreeUtils.cloneListWithNewIds(origChildren, nodeIdGen));
    MsgSelectNode selectNode = new MsgSelectNode(nodeIdGen.genId(), msg.getSourceLocation(), genderExpr, baseSelectVarName);
    selectNode.addChild(femaleCase);
    selectNode.addChild(maleCase);
    selectNode.addChild(defaultCase);
    msg.addChild(selectNode);
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) MsgSelectDefaultNode(com.google.template.soy.soytree.MsgSelectDefaultNode) MsgSelectNode(com.google.template.soy.soytree.MsgSelectNode) MsgSelectCaseNode(com.google.template.soy.soytree.MsgSelectCaseNode)

Example 5 with MsgSelectNode

use of com.google.template.soy.soytree.MsgSelectNode in project closure-templates by google.

the class TemplateParserTest method testParseMsgStmtWithSelect.

@Test
public void testParseMsgStmtWithSelect() throws Exception {
    String templateBody = "{@param gender : ?}{@param person : ?}\n" + "{msg desc=\"A sample gender message\"}\n" + "  {select $gender}\n" + "    {case 'female'}{$person} added you to her circle.\n" + "    {default}{$person} added you to his circle.\n" + "  {/select}\n" + "{/msg}\n";
    List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
    assertEquals(1, nodes.size());
    MsgNode mn = ((MsgFallbackGroupNode) nodes.get(0)).getChild(0);
    assertEquals(1, mn.numChildren());
    assertEquals("A sample gender message", mn.getDesc());
    MsgSelectNode sn = (MsgSelectNode) mn.getChild(0);
    assertEquals("$gender", sn.getExpr().toSourceString());
    // female and default
    assertEquals(2, sn.numChildren());
    // Case 'female'
    MsgSelectCaseNode cnf = (MsgSelectCaseNode) sn.getChild(0);
    assertEquals("female", cnf.getCaseValue());
    assertEquals(2, cnf.numChildren());
    MsgPlaceholderNode phnf = (MsgPlaceholderNode) cnf.getChild(0);
    assertEquals("{$person}", phnf.toSourceString());
    RawTextNode rtnf = (RawTextNode) cnf.getChild(1);
    assertEquals(" added you to her circle.", rtnf.getRawText());
    // Default
    MsgSelectDefaultNode dn = (MsgSelectDefaultNode) sn.getChild(1);
    assertEquals(2, dn.numChildren());
    MsgPlaceholderNode phnd = (MsgPlaceholderNode) dn.getChild(0);
    assertEquals("{$person}", phnd.toSourceString());
    RawTextNode rtnd = (RawTextNode) dn.getChild(1);
    assertEquals(" added you to his circle.", rtnd.getRawText());
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) MsgSelectDefaultNode(com.google.template.soy.soytree.MsgSelectDefaultNode) MsgFallbackGroupNode(com.google.template.soy.soytree.MsgFallbackGroupNode) MsgSelectNode(com.google.template.soy.soytree.MsgSelectNode) MsgSelectCaseNode(com.google.template.soy.soytree.MsgSelectCaseNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) MsgNode(com.google.template.soy.soytree.MsgNode) MsgPlaceholderNode(com.google.template.soy.soytree.MsgPlaceholderNode) Test(org.junit.Test)

Aggregations

MsgSelectNode (com.google.template.soy.soytree.MsgSelectNode)6 MsgPlaceholderNode (com.google.template.soy.soytree.MsgPlaceholderNode)4 RawTextNode (com.google.template.soy.soytree.RawTextNode)4 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)4 MsgSelectCaseNode (com.google.template.soy.soytree.MsgSelectCaseNode)3 MsgSelectDefaultNode (com.google.template.soy.soytree.MsgSelectDefaultNode)3 MsgFallbackGroupNode (com.google.template.soy.soytree.MsgFallbackGroupNode)2 MsgNode (com.google.template.soy.soytree.MsgNode)2 MsgPluralNode (com.google.template.soy.soytree.MsgPluralNode)2 Test (org.junit.Test)2 ImmutableList (com.google.common.collect.ImmutableList)1 Expression (com.google.template.soy.jbcsrc.restricted.Expression)1 SoyExpression (com.google.template.soy.jbcsrc.restricted.SoyExpression)1 SoyMsgPart (com.google.template.soy.msgs.restricted.SoyMsgPart)1 SoyMsgPlaceholderPart (com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart)1 PyExpr (com.google.template.soy.pysrc.restricted.PyExpr)1 PyStringExpr (com.google.template.soy.pysrc.restricted.PyStringExpr)1 AbstractParentSoyNode (com.google.template.soy.soytree.AbstractParentSoyNode)1 CallNode (com.google.template.soy.soytree.CallNode)1 MsgHtmlTagNode (com.google.template.soy.soytree.MsgHtmlTagNode)1