use of com.squarespace.template.CompiledTemplate in project template-compiler by Squarespace.
the class UnitLocalesTest method test.
private void test(CLDR.Locale locale, String templatePath, String expectedPath) throws Exception {
Pair<CompiledTemplate, JsonNode> compiled = load(templatePath, "units/_numbers.json");
Context ctx = new Context(compiled._2);
ctx.cldrLocale(locale);
ctx.execute(compiled._1.code());
String actual = reformat(ctx.buffer().toString());
String expected = reformat(GeneralUtils.loadResource(getClass(), expectedPath));
if (!actual.trim().equals(expected.trim())) {
String diff = TestCaseParser.diff(expected, actual);
throw new AssertionError("File '" + expectedPath + "' output does not match:\n" + diff);
}
}
use of com.squarespace.template.CompiledTemplate in project template-compiler by Squarespace.
the class UnitLocalesTest method load.
private Pair<CompiledTemplate, JsonNode> load(String templatePath, String jsonPath) throws Exception {
String template = GeneralUtils.loadResource(getClass(), templatePath);
String json = GeneralUtils.loadResource(getClass(), jsonPath);
CompiledTemplate code = compiler().compile(template);
JsonNode node = JsonUtils.decode(json);
return Pair.pair(code, node);
}
use of com.squarespace.template.CompiledTemplate 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.CompiledTemplate in project template-compiler by Squarespace.
the class TemplateC method tree.
/**
* Print the syntax tree for the given template.
*/
protected int tree(String templatePath, boolean preprocess) throws CodeException, IOException {
String template = readFile(templatePath);
CompiledTemplate compiled = compiler().compile(template, false, preprocess);
StringBuilder buf = new StringBuilder();
TreeEmitter.emit(compiled.code(), 0, buf);
System.out.println(buf.toString());
return 0;
}
use of com.squarespace.template.CompiledTemplate in project template-compiler by Squarespace.
the class TemplateC method stats.
/**
* Scan the compiled template and print statistics.
*/
protected int stats(String templatePath, boolean preprocess) throws CodeException, IOException {
String template = readFile(templatePath);
CompiledTemplate compiled = compiler().compile(template, false, preprocess);
ReferenceScanner scanner = new ReferenceScanner();
scanner.extract(compiled.code());
ObjectNode report = scanner.references().report();
String result = GeneralUtils.jsonPretty(report);
System.out.println(result);
return 0;
}
Aggregations