<commit_before>/*
 * Copyright 2011-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jetdrone.vertx.yoke.extras.engine;

import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Template;
import com.jetdrone.vertx.yoke.Engine;
import com.jetdrone.vertx.yoke.util.YokeAsyncResult;
import org.vertx.java.core.AsyncResult;
import org.vertx.java.core.AsyncResultHandler;
import org.vertx.java.core.Handler;

import java.io.IOException;
import java.util.Map;

public class HandlebarsEngine extends Engine<Template> {

    private final Handlebars handlebars = new Handlebars();

    @Override
    public void render(final String filename, final Map<String, Object> context, final Handler<AsyncResult<String>> next) {
        read(filename, new AsyncResultHandler<String>() {
            @Override
            public void handle(AsyncResult<String> asyncResult) {
                if (asyncResult.failed()) {
                    next.handle(new YokeAsyncResult<String>(asyncResult.cause()));
                } else {
                    try {
                        Template template = compile(filename, asyncResult.result());
                        next.handle(new YokeAsyncResult<>(template.apply(context)));
                    } catch (IOException ex) {
                        next.handle(new YokeAsyncResult<String>(ex));
                    }
                }
            }
        });
    }

    private Template compile(String filename, String templateText) throws IOException {

        Template template = getTemplateFromCache(filename);

        if (template == null) {
            // real compile
            template = handlebars.compileInline(templateText);
            putTemplateToCache(filename, template);
        }

        return template;
    }
}<commit_msg>#27 allow helpers in the engine<commit_after>/*
 * Copyright 2011-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jetdrone.vertx.yoke.extras.engine;

import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Helper;
import com.github.jknack.handlebars.Template;
import com.jetdrone.vertx.yoke.Engine;
import com.jetdrone.vertx.yoke.util.YokeAsyncResult;
import org.vertx.java.core.AsyncResult;
import org.vertx.java.core.AsyncResultHandler;
import org.vertx.java.core.Handler;

import java.io.IOException;
import java.util.Map;

public class HandlebarsEngine extends Engine<Template> {

    private final Handlebars handlebars = new Handlebars();

    @Override
    public void render(final String filename, final Map<String, Object> context, final Handler<AsyncResult<String>> next) {
        read(filename, new AsyncResultHandler<String>() {
            @Override
            public void handle(AsyncResult<String> asyncResult) {
                if (asyncResult.failed()) {
                    next.handle(new YokeAsyncResult<String>(asyncResult.cause()));
                } else {
                    try {
                        Template template = compile(filename, asyncResult.result());
                        next.handle(new YokeAsyncResult<>(template.apply(context)));
                    } catch (IOException ex) {
                        next.handle(new YokeAsyncResult<String>(ex));
                    }
                }
            }
        });
    }

    private Template compile(String filename, String templateText) throws IOException {

        Template template = getTemplateFromCache(filename);

        if (template == null) {
            // real compile
            template = handlebars.compileInline(templateText);
            putTemplateToCache(filename, template);
        }

        return template;
    }

    public void registerHelper(String name, Helper<?> helper) {
        handlebars.registerHelper(name, helper);
    }
}
{'fraction_non_alphanumeric': 0.09059446254071661, 'fraction_numerical': 0.005293159609120521, 'mean_word_length': 2.6609538002980626, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2500675', 'n_tokens_mistral': 1274, 'n_tokens_neox': 1223, 'n_words': 369}