use of com.google.template.soy.pysrc.restricted.PyExpr in project closure-templates by google.
the class BidiTextDirFunction method computeForPySrc.
@Override
public PyExpr computeForPySrc(List<PyExpr> args) {
PyExpr value = args.get(0);
PyExpr isHtml = (args.size() == 2) ? args.get(1) : null;
String callText = (isHtml != null) ? "bidi.text_dir(" + value.getText() + ", " + isHtml.getText() + ")" : "bidi.text_dir(" + value.getText() + ")";
return new PyExpr(callText, Integer.MAX_VALUE);
}
use of com.google.template.soy.pysrc.restricted.PyExpr in project closure-templates by google.
the class TruncateDirective method applyForPySrc.
@Override
public PyExpr applyForPySrc(PyExpr value, List<PyExpr> args) {
// Truncation always wants a string, so to potentially save an unnecessary conversion, we do
// optional coercing at compile time.
PyExpr input = value.toPyString();
PyExpr maxLen = args.get(0);
PyExpr doAddEllipsis = (args.size() == 2) ? args.get(1) : new PyExpr("True", Integer.MAX_VALUE);
PyFunctionExprBuilder fnBuilder = new PyFunctionExprBuilder("directives.truncate");
fnBuilder.addArg(input).addArg(maxLen).addArg(doAddEllipsis);
return fnBuilder.asPyStringExpr();
}
use of com.google.template.soy.pysrc.restricted.PyExpr in project closure-templates by google.
the class GenPyExprsVisitorTest method testMsgOnlyLiteralWithApostrophe.
@Test
public void testMsgOnlyLiteralWithApostrophe() {
// Should escape '\'' in format string.
String soyCode = "{msg meaning=\"verb\" desc=\"The word 'Archive' used as a verb.\"}" + "Archive's" + "{/msg}\n";
String expectedPyCode = "translator_impl.render_literal(" + "translator_impl.prepare_literal(" + "###, " + "'Archive\\'s'))";
assertThatSoyExpr(soyCode).compilesTo(new PyExpr(expectedPyCode, Integer.MAX_VALUE));
}
use of com.google.template.soy.pysrc.restricted.PyExpr in project closure-templates by google.
the class GenPyExprsVisitorTest method testMsgWithPluralAndOffset.
@Test
public void testMsgWithPluralAndOffset() {
String soyCode = "{@param numDrafts:?}\n" + "{msg desc=\"offset plural\"}" + "{plural $numDrafts offset=\"2\"}" + "{case 0}No drafts" + "{case 1}1 draft" + "{default}{remainder($numDrafts)} drafts" + "{/plural}" + "{/msg}";
String expectedPyCode = "translator_impl.render_plural(" + "translator_impl.prepare_plural(" + "###, " + "{" + "'=0': 'No drafts', " + "'=1': '1 draft', " + "'other': '{XXX} drafts'" + "}, " + "('NUM_DRAFTS', 'XXX')), " + "data.get('numDrafts'), " + "{" + "'NUM_DRAFTS': data.get('numDrafts'), " + "'XXX': str(data.get('numDrafts') - 2)" + "})";
assertThatSoyExpr(soyCode).compilesTo(new PyExpr(expectedPyCode, Integer.MAX_VALUE));
}
use of com.google.template.soy.pysrc.restricted.PyExpr in project closure-templates by google.
the class GenPyExprsVisitorTest method testMsgMultipleSoyExpressionsWithBraces.
@Test
public void testMsgMultipleSoyExpressionsWithBraces() {
String soyCode = "{@param username:?}\n" + "{@param greet:?}\n" + "{msg desc=\"var placeholder\"}" + "{$greet} {lb}{$username}{rb}" + "{/msg}\n";
String expectedPyCode = "translator_impl.render(" + "translator_impl.prepare(" + "###, " + "'{GREET} {{{USERNAME}}}', " + "('GREET', 'USERNAME')), " + "{" + "'GREET': str(data.get('greet')), " + "'USERNAME': str(data.get('username'))" + "})";
assertThatSoyExpr(soyCode).compilesTo(new PyExpr(expectedPyCode, Integer.MAX_VALUE));
}
Aggregations