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;
}
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());
}
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));
}
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() : "") + "]");
}
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));
}
Aggregations