use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class DecimalFormatterTest method format.
private static String format(String locale, Arguments args, String json) throws CodeException {
Context ctx = new Context(JsonUtils.decode(json));
ctx.javaLocale(Locale.forLanguageTag(locale));
DECIMAL.validateArgs(args);
Variables variables = new Variables("@", ctx.node());
DECIMAL.apply(ctx, args, variables);
return variables.first().node().asText();
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class LegacyMoneyFormatFactoryTest method legacy.
private String legacy(String locale, String currency, String number) throws CodeException {
Arguments args = mk.args(" " + locale);
LEGACY_MONEY.validateArgs(args);
Context ctx = new Context(moneyJson(number, currency));
Variables variables = new Variables("@", ctx.node());
LEGACY_MONEY.apply(ctx, args, variables);
return variables.first().node().asText();
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class MoneyFormatterLegacyTest method executeLocale.
private String executeLocale(String template, String json, String locale) throws CodeException {
Compiler compiler = compiler();
Context ctx = compiler.newExecutor().json(json).template(template).locale(Locale.forLanguageTag(locale)).execute();
return ctx.buffer().toString();
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class TemplateC method compile.
/**
* Compile a template against a given json tree and emit the result.
*/
protected int compile(String templatePath, String jsonPath, String partialsPath, String locale, boolean preprocess) throws CodeException, IOException {
String template = readFile(templatePath);
String json = "{}";
if (jsonPath != null) {
json = readFile(jsonPath);
}
String partials = null;
if (partialsPath != null) {
partials = readFile(partialsPath);
}
if (locale == null) {
locale = "en-US";
}
CompiledTemplate compiled = compiler().compile(template, true, preprocess);
StringBuilder errorBuf = new StringBuilder();
List<ErrorInfo> errors = compiled.errors();
if (!errors.isEmpty()) {
errorBuf.append("Caught errors executing template:\n");
for (ErrorInfo error : errors) {
errorBuf.append(" ").append(error.getMessage()).append('\n');
}
}
Instruction code = compiled.code();
// Parse the JSON context
JsonNode jsonTree = null;
try {
jsonTree = JsonUtils.decode(json);
} catch (IllegalArgumentException e) {
System.err.println("Caught error trying to parse JSON: " + e.getCause().getMessage());
return 1;
}
// Parse the optional JSON partials dictionary.
JsonNode partialsTree = null;
if (partials != null) {
try {
partialsTree = JsonUtils.decode(partials);
if (!(partialsTree instanceof ObjectNode)) {
System.err.println("Partials map JSON must be an object. Found " + partialsTree.getNodeType());
return 1;
}
} catch (IllegalArgumentException e) {
System.err.println("Caught error trying to parse partials: " + e.getCause().getMessage());
return 1;
}
}
// Perform the compile.
Context context = compiler().newExecutor().code(code).json(jsonTree).locale(Locale.forLanguageTag(locale)).safeExecution(true).partialsMap((ObjectNode) partialsTree).enableExpr(true).enableInclude(true).execute();
// If compile was successful, print the output.
System.out.print(context.buffer().toString());
if (errorBuf.length() > 0) {
System.err.println(errorBuf.toString());
}
return 0;
}
use of com.squarespace.template.Context in project template-compiler by Squarespace.
the class CoreFormattersTest method testApplyPartialErrorSyntax.
@Test
public void testApplyPartialErrorSyntax() throws CodeException {
String template = "{@|apply block}";
String input = "{}";
String partials = "{\"block\": \"{.section foo}{@}\"}";
Instruction inst = compiler().compile(template).code();
Context ctx = compiler().newExecutor().json(input).code(inst).partialsMap(partials).safeExecution(true).execute();
assertContext(ctx, "");
assertEquals(ctx.getErrors().size(), 1);
assertEquals(ctx.getErrors().get(0).getType(), COMPILE_PARTIAL_SYNTAX);
}
Aggregations