<commit_before>package org.wso2.carbon.uuf.handlebars.helpers.runtime;

import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Helper;
import com.github.jknack.handlebars.Options;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.uuf.core.API;
import org.wso2.carbon.uuf.core.ComponentLookup;
import org.wso2.carbon.uuf.core.Fragment;
import org.wso2.carbon.uuf.core.RequestLookup;
import org.wso2.carbon.uuf.handlebars.model.ContextModel;
import org.wso2.carbon.uuf.model.MapModel;
import org.wso2.carbon.uuf.model.Model;

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

import static org.wso2.carbon.uuf.handlebars.HbsRenderable.DATA_KEY_API;
import static org.wso2.carbon.uuf.handlebars.HbsRenderable.DATA_KEY_LOOKUP;
import static org.wso2.carbon.uuf.handlebars.HbsRenderable.DATA_KEY_REQUEST_LOOKUP;

public class IncludeFragmentHelper implements Helper<String> {

    public static final String HELPER_NAME = "includeFragment";
    private static final Logger log = LoggerFactory.getLogger(IncludeFragmentHelper.class);

    @Override
    public CharSequence apply(String fragmentName, Options options) throws IOException {
        ComponentLookup lookup = options.data(DATA_KEY_LOOKUP);
        Optional<Fragment> renderingFragment = lookup.getFragment(fragmentName);
        if (!renderingFragment.isPresent()) {
            throw new IllegalArgumentException("Fragment '" + fragmentName + "' does not exists in Component '" +
                                                       lookup.getComponentName() + "' or in its dependencies.");
        }

        Fragment fragment = renderingFragment.get();
        Map<String, Object> fragmentArgs = options.hash;
        Model model;
        if (fragmentArgs.isEmpty()) {
            model = new ContextModel(options.context);
        } else {
            model = new MapModel(fragmentArgs);
        }
        RequestLookup requestLookup = options.data(DATA_KEY_REQUEST_LOOKUP);
        API api = options.data(DATA_KEY_API);

        if (log.isDebugEnabled()) {
            log.debug("Fragment \"" + fragment + "\" is called from '" + options.fn.text() + "'.");
        }
        String content = fragment.render(model, lookup, requestLookup, api);
        return new Handlebars.SafeString(content);
    }
}
<commit_msg>changed the logic in 'apply' method of IncludeFragmentHelper classto pass the correct Handlebars Context to the including fragment<commit_after>package org.wso2.carbon.uuf.handlebars.helpers.runtime;

import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Helper;
import com.github.jknack.handlebars.Options;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.uuf.core.API;
import org.wso2.carbon.uuf.core.ComponentLookup;
import org.wso2.carbon.uuf.core.Fragment;
import org.wso2.carbon.uuf.core.RequestLookup;
import org.wso2.carbon.uuf.handlebars.model.ContextModel;
import org.wso2.carbon.uuf.model.Model;

import java.io.IOException;
import java.util.Optional;

import static org.wso2.carbon.uuf.handlebars.HbsRenderable.DATA_KEY_API;
import static org.wso2.carbon.uuf.handlebars.HbsRenderable.DATA_KEY_LOOKUP;
import static org.wso2.carbon.uuf.handlebars.HbsRenderable.DATA_KEY_REQUEST_LOOKUP;

public class IncludeFragmentHelper implements Helper<String> {

    public static final String HELPER_NAME = "includeFragment";
    private static final Logger log = LoggerFactory.getLogger(IncludeFragmentHelper.class);

    @Override
    public CharSequence apply(String fragmentName, Options options) throws IOException {
        ComponentLookup lookup = options.data(DATA_KEY_LOOKUP);
        Optional<Fragment> fragment = lookup.getFragment(fragmentName);
        if (!fragment.isPresent()) {
            throw new IllegalArgumentException("Fragment '" + fragmentName + "' does not exists in Component '" +
                                                       lookup.getComponentName() + "' or in its dependencies.");
        }

        if (log.isDebugEnabled()) {
            log.debug("Fragment \"" + fragment.get() + "\" is called from '" + options.fn.text() + "'.");
        }

        Model model = new ContextModel(options.context, options.hash);
        RequestLookup requestLookup = options.data(DATA_KEY_REQUEST_LOOKUP);
        API api = options.data(DATA_KEY_API);
        String content = fragment.get().render(model, lookup, requestLookup, api);
        return new Handlebars.SafeString(content);
    }
}

{'fraction_non_alphanumeric': 0.10422163588390501, 'fraction_numerical': 0.005496921723834653, 'mean_word_length': 3.6089159067882473, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6351600', 'n_tokens_mistral': 1339, 'n_tokens_neox': 1306, 'n_words': 260}