Я пошел за рулем, потому что {{#if}}
и {{else}}
и {{/if}}
уже обработаны в этой библиотеке
Я ввел помощника с именем ifCond
, который будет вызывать нашрон
Затем я заменил #if
на {{#ifCond
, заменил #else
на {{else}}
, заменил #endif
на {{/if}}
static ScriptEngineManager mgr = new ScriptEngineManager();
static ScriptEngine engine = mgr.getEngineByName("JavaScript");
public static void main(String[] args) throws Exception {
Bindings bindings = engine.createBindings();
bindings.put("TARGET_NOT_SUPPORTED", true);
bindings.put("HTML5_SUPPORT_DEFERRING_USER_SENSITIVE_REQUESTS", true);
bindings.put("MINIMAL_RUNTIME", false);
bindings.put("EXIT_RUNTIME", false);
bindings.put("MIN_IE_VERSION", "TARGET_NOT_SUPPORTED");
bindings.put("USE_PTHREADS", true);
bindings.put("WASM_BACKEND", true);
bindings.put("MIN_FIREFOX_VERSION", 64);
bindings.put("MIN_SAFARI_VERSION", "TARGET_NOT_SUPPORTED");
bindings.put("ENVIRONMENT_MAY_BE_WORKER", false);
bindings.put("ENVIRONMENT_MAY_BE_NODE", false);
bindings.put("ENVIRONMENT_MAY_BE_SHELL", false);
bindings.put("DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR", true);
bindings.put("OFFSCREENCANVAS_SUPPORT", true);
bindings.put("ASSERTIONS", false);
bindings.put("MIN_CHROME_VERSION", 37);
bindings.put("MIN_EDGE_VERSION", 13);
bindings.put("GL_DEBUG", false);
bindings.put("OFFSCREEN_FRAMEBUFFER", true);
bindings.put("TRACE_WEBGL_CALLS", true);
bindings.put("GL_SUPPORT_AUTOMATIC_ENABLE_EXTENSIONS", true);
bindings.put("GL_SUPPORT_EXPLICIT_SWAP_CONTROL", true);
bindings.put("MIN_WEBGL_VERSION", 1);
bindings.put("USE_WEBGPU", true);
bindings.put("WASM", true);
bindings.put("GL_EXTENSIONS_IN_PREFIXED_FORMAT", true);
bindings.put("MAX_WEBGL_VERSION", 3);
bindings.put("MIN_WEBGL_VERSION", 1);
bindings.put("FULL_ES2", true);
bindings.put("GL_ASSERTIONS", false);
bindings.put("GL_TRACK_ERRORS", true);
bindings.put("GL_POOL_TEMP_BUFFERS", true);
bindings.put("LEGACY_GL_EMULATION", false);
bindings.put("GL_FFP_ONLY", false);
bindings.put("GL_TESTING", false);
bindings.put("GL_PREINITIALIZED_CONTEXT", false);
bindings.put("GL_DISABLE_HALF_FLOAT_EXTENSION_IF_BROKEN", true);
bindings.put("OFFSCREEN_FRAMEBUFFER_FORBID_VAO_PATH", false);
bindings.put("WORKAROUND_OLD_WEBGL_UNIFORM_UPLOAD_IGNORED_OFFSET_BUG", true);
bindings.put("GL_EMULATE_GLES_VERSION_STRING_FORMAT", true);
bindings.put("WEBGL2_BACKWARDS_COMPATIBILITY_EMULATION", true);
bindings.put("PTHREAD_POOL_SIZE", 2);
bindings.put("PTHREADS_PROFILING", false);
bindings.put("USE_CLOSURE_COMPILER", true);
bindings.put("LOAD_SOURCE_MAP", false);
bindings.put("USE_OFFSET_CONVERTER", false);
bindings.put("PTHREADS_DEBUG", false);
bindings.put("OFFSCREENCANVASES_TO_PTHREAD", true);
bindings.put("ALLOW_BLOCKING_ON_MAIN_THREAD", false);
bindings.put("STACK_OVERFLOW_CHECK", false);
bindings.put("ALLOW_MEMORY_GROWTH", false);
bindings.put("MAXIMUM_MEMORY", -1);
bindings.put("ABORTING_MALLOC", 0);
bindings.put("TEST_MEMORY_GROWTH_FAILS", false);
bindings.put("MEMORYPROFILER", false);
bindings.put("CAN_ADDRESS_2GB", false);
bindings.put("EMSCRIPTEN_TRACING", false);
bindings.put("MEMORY_GROWTH_LINEAR_STEP", -1);
bindings.put("MEMORY_GROWTH_GEOMETRIC_CAP", 96);
bindings.put("FAST_UNROLLED_MEMCPY_AND_MEMSET", false);
bindings.put("DECLARE_ASM_MODULE_EXPORTS", false);
bindings.put("EMULATE_FUNCTION_POINTER_CASTS", false);
bindings.put("SUPPORT_LONGJMP", false);
bindings.put("SUPPORT_ERRNO", false);
bindings.put("PROXY_POSIX_SOCKETS", 0);
bindings.put("SOCKET_WEBRTC", false);
bindings.put("ENVIRONMENT_MAY_BE_WEB", true);
bindings.put("MAIN_MODULE", 0);
bindings.put("USE_ASAN", false);
bindings.put("FULL_ES3", true);
bindings.put("library_webgl.js", true);
engine.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
engine.eval("var LibraryManager = {\"has\":function(){return true;}}");
String json = "{\"name\": \"world\"}";
JsonNode jsonNode = new ObjectMapper().readValue(json, JsonNode.class);
Handlebars handlebars = new Handlebars();
handlebars.registerHelper("json", Jackson2Helper.INSTANCE);
Context context = Context
.newBuilder(jsonNode)
.resolver(JsonNodeValueResolver.INSTANCE,
JavaBeanValueResolver.INSTANCE,
FieldValueResolver.INSTANCE,
MapValueResolver.INSTANCE,
MethodValueResolver.INSTANCE
)
.build();
handlebars.registerHelper("ifCond", new Helper<String>() {
public Object apply(String context, Options options) throws IOException {
try {
return engine.eval(context)!=null && engine.eval(context).toString().equals("true") ? options.fn(this) : options.inverse(this);
} catch (Exception e) {
System.err.println("Unable to process: " + context);
return options.inverse(this);
}
}
});
Scanner s = new Scanner(new File("C:\\emsdk-1.39.13\\upstream\\emscripten\\src\\library.js"));
StringBuffer sb = new StringBuffer();
while(s.hasNextLine()) {
String line = s.nextLine();
if(line.startsWith("#if")) {
if(line.indexOf("//")!=-1)
line = line.substring(0, line.indexOf("//"));
line = line.substring(4);
sb.append("{{#ifCond \"" + line + "\"}}\r\n");
}else if(line.startsWith("#else")) {
if(line.indexOf("//")!=-1)
line = line.substring(0, line.indexOf("//"));
sb.append("{{" + line.substring(1) + "}}\r\n");
}else if(line.startsWith("#endif")) {
if(line.indexOf("//")!=-1)
line = line.substring(0, line.indexOf("//"));
sb.append("{{/ifCond}}\r\n");
}else {
while(line.indexOf("{{{")!=-1) {
line = line.replace("{{{", "");
}
while(line.indexOf("}}}")!=-1) {
line = line.replace("}}}", "");
}
sb.append(line + "\r\n");
}
}
s.close();
Template template = handlebars.compileInline(sb.toString());
System.out.println(template.apply(context));
Теперь проблема ветвления if решена