Search in sources :

Example 51 with Context

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();
}
Also used : Context(com.squarespace.template.Context) Variables(com.squarespace.template.Variables)

Example 52 with Context

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();
}
Also used : Context(com.squarespace.template.Context) Variables(com.squarespace.template.Variables) Arguments(com.squarespace.template.Arguments)

Example 53 with Context

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();
}
Also used : Context(com.squarespace.template.Context) Compiler(com.squarespace.template.Compiler)

Example 54 with Context

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;
}
Also used : Context(com.squarespace.template.Context) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ErrorInfo(com.squarespace.template.ErrorInfo) JsonNode(com.fasterxml.jackson.databind.JsonNode) Instruction(com.squarespace.template.Instruction) CompiledTemplate(com.squarespace.template.CompiledTemplate)

Example 55 with Context

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);
}
Also used : Context(com.squarespace.template.Context) Instruction(com.squarespace.template.Instruction) Test(org.testng.annotations.Test)

Aggregations

Context (com.squarespace.template.Context)73 Test (org.testng.annotations.Test)55 DoubleNode (com.fasterxml.jackson.databind.node.DoubleNode)21 Instruction (com.squarespace.template.Instruction)14 Variables (com.squarespace.template.Variables)11 TextNode (com.fasterxml.jackson.databind.node.TextNode)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 Arguments (com.squarespace.template.Arguments)5 CodeMaker (com.squarespace.template.CodeMaker)4 Compiler (com.squarespace.template.Compiler)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 CompiledTemplate (com.squarespace.template.CompiledTemplate)2 CodeBuilder (com.squarespace.template.CodeBuilder)1 CodeExecuteException (com.squarespace.template.CodeExecuteException)1 CodeMachine (com.squarespace.template.CodeMachine)1 CodeSyntaxException (com.squarespace.template.CodeSyntaxException)1 ErrorInfo (com.squarespace.template.ErrorInfo)1 Locale (java.util.Locale)1