<commit_before><commit_msg>Add main helper to compile Hogan.js templates<commit_after>package net.champemont.jean.hogancompile.helper;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.net.URISyntaxException;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

public class HoganTemplateCompileHelper {
	
	private static String HOGANJS_FILENAME = "hogan-2.0.0.js";
	
	private static String COMPILE_CMD = "var result = Hogan.compile(template, {asString: true});";
	
	public static String compileHoganTemplate(String template, String templateName) throws IOException {
		String result = null;
		Context cx = Context.enter();
		Scriptable scope = cx.initStandardObjects();
		Reader hoganJS;
		try {
			hoganJS = new FileReader(new File(HoganTemplateCompileHelper.class.getClassLoader().getResource(HOGANJS_FILENAME).toURI()));
		} catch (URISyntaxException e) {
			throw new IOException(HOGANJS_FILENAME + " was not found on classpath. Aborting");
		}
		cx.evaluateReader(scope, hoganJS, HOGANJS_FILENAME, 1, null);
		scope.put("template", scope, template);
		cx.evaluateString(scope, COMPILE_CMD, "COMPILE_CMD", 1, null);
		
		result = "templates." + templateName + " = new Hogan.Template(" + (String) scope.get("result", scope) + ");";
		return result;
	}
	
	public static String getTemplateHeader() {
		return "var templates = {};";
	}
}
{'fraction_non_alphanumeric': 0.11818181818181818, 'fraction_numerical': 0.0034965034965034965, 'mean_word_length': 5.331858407079646, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1922997', 'n_tokens_mistral': 479, 'n_tokens_neox': 437, 'n_words': 106}