use of com.github.jknack.handlebars.io.FileTemplateLoader in project ballerina by ballerina-lang.
the class CodeGenerator method compileTemplate.
private Template compileTemplate(String defaultTemplateDir, String templateName) throws IOException {
String templatesDirPath = System.getProperty(GeneratorConstants.TEMPLATES_DIR_PATH_KEY, defaultTemplateDir);
ClassPathTemplateLoader cpTemplateLoader = new ClassPathTemplateLoader((templatesDirPath));
FileTemplateLoader fileTemplateLoader = new FileTemplateLoader(templatesDirPath);
cpTemplateLoader.setSuffix(GeneratorConstants.TEMPLATES_SUFFIX);
fileTemplateLoader.setSuffix(GeneratorConstants.TEMPLATES_SUFFIX);
Handlebars handlebars = new Handlebars().with(cpTemplateLoader, fileTemplateLoader);
handlebars.registerHelpers(StringHelpers.class);
handlebars.registerHelper("equals", (object, options) -> {
CharSequence result;
Object param0 = options.param(0);
if (param0 == null) {
throw new IllegalArgumentException("found 'null', expected 'string'");
}
if (object != null && object.toString().equals(param0.toString())) {
result = options.fn(options.context);
} else {
result = null;
}
return result;
});
return handlebars.compile(templateName);
}
use of com.github.jknack.handlebars.io.FileTemplateLoader in project ballerina by ballerina-lang.
the class CodeGenerator method compileTemplate.
/**
* This method will compile and return template of passed template definition.
*
* @param defaultTemplateDir template directory which contains set of templates
* @param templateName template file name to be used as template
* @return compiled template generated for template definition.
* @throws CodeGeneratorException throws IOException when compilation error occurs.
*/
private Template compileTemplate(String defaultTemplateDir, String templateName) throws CodeGeneratorException {
String templatesDirPath = System.getProperty(GeneratorConstants.TEMPLATES_DIR_PATH_KEY, defaultTemplateDir);
ClassPathTemplateLoader cpTemplateLoader = new ClassPathTemplateLoader((templatesDirPath));
FileTemplateLoader fileTemplateLoader = new FileTemplateLoader(templatesDirPath);
cpTemplateLoader.setSuffix(GeneratorConstants.TEMPLATES_SUFFIX);
fileTemplateLoader.setSuffix(GeneratorConstants.TEMPLATES_SUFFIX);
Handlebars handlebars = new Handlebars().with(cpTemplateLoader, fileTemplateLoader);
handlebars.registerHelpers(StringHelpers.class);
handlebars.registerHelper("equals", (object, options) -> {
CharSequence result;
Object param0 = options.param(0);
if (param0 == null) {
throw new IllegalArgumentException("found 'null', expected 'string'");
}
if (object != null && object.toString().equals(param0.toString())) {
result = options.fn(options.context);
} else {
result = null;
}
return result;
});
try {
return handlebars.compile(templateName);
} catch (IOException e) {
throw new CodeGeneratorException("Error while compiling template", e);
}
}
use of com.github.jknack.handlebars.io.FileTemplateLoader in project ballerina by ballerina-lang.
the class BalGenerationUtils method compileTemplate.
private static Template compileTemplate(String defaultTemplateDir, String templateName) throws IOException {
String templatesDirPath = System.getProperty(TEMPLATES_DIR_PATH_KEY, defaultTemplateDir);
ClassPathTemplateLoader cpTemplateLoader = new ClassPathTemplateLoader((templatesDirPath));
FileTemplateLoader fileTemplateLoader = new FileTemplateLoader(templatesDirPath);
cpTemplateLoader.setSuffix(TEMPLATES_SUFFIX);
fileTemplateLoader.setSuffix(TEMPLATES_SUFFIX);
Handlebars handlebars = new Handlebars().with(cpTemplateLoader, fileTemplateLoader);
handlebars.registerHelpers(StringHelpers.class);
handlebars.registerHelper("equals", (object, options) -> {
CharSequence result;
Object param0 = options.param(0);
if (param0 == null) {
throw new IllegalArgumentException("found n'null', expected 'string'");
}
if (object != null && object.toString().equals(param0.toString())) {
result = options.fn(options.context);
} else {
result = null;
}
return result;
});
return handlebars.compile(templateName);
}
use of com.github.jknack.handlebars.io.FileTemplateLoader in project ballerina by ballerina-lang.
the class Writer method writeHtmlDocument.
/**
* Write the HTML document from the Page object for a bal package.
* @param object Page object which is generated from the bal package.
* @param packageTemplateName hbs template file to be used.
* @param filePath path of the file to write the output.
*/
public static void writeHtmlDocument(Object object, String packageTemplateName, String filePath) {
String templatesFolderPath = System.getProperty(BallerinaDocConstants.TEMPLATES_FOLDER_PATH_KEY, File.separator + "docerina-templates" + File.separator + "html");
PrintWriter writer = null;
try {
Handlebars handlebars = new Handlebars().with(new ClassPathTemplateLoader(templatesFolderPath), new FileTemplateLoader(templatesFolderPath));
handlebars.registerHelpers(StringHelpers.class);
Template template = handlebars.compile(packageTemplateName);
writer = new PrintWriter(filePath, "UTF-8");
Context context = Context.newBuilder(object).resolver(FieldValueResolver.INSTANCE).build();
writer.println(template.apply(context));
out.println("HTML file written: " + filePath);
} catch (IOException e) {
out.println("docerina: could not write HTML file " + filePath + System.lineSeparator() + e.getMessage());
} finally {
if (writer != null) {
writer.close();
}
}
}
use of com.github.jknack.handlebars.io.FileTemplateLoader in project fess by codelibs.
the class ViewHelper method createCacheContent.
public String createCacheContent(final Map<String, Object> doc, final String[] queries) {
final FessConfig fessConfig = ComponentUtil.getFessConfig();
final FileTemplateLoader loader = new FileTemplateLoader(ResourceUtil.getViewTemplatePath().toFile());
final Handlebars handlebars = new Handlebars(loader);
Locale locale = ComponentUtil.getRequestManager().getUserLocale();
if (locale == null) {
locale = Locale.ENGLISH;
}
String url = DocumentUtil.getValue(doc, fessConfig.getIndexFieldUrl(), String.class);
if (url == null) {
url = ComponentUtil.getMessageManager().getMessage(locale, "labels.search_unknown");
}
doc.put(fessConfig.getResponseFieldUrlLink(), getUrlLink(doc));
String createdStr;
final Date created = DocumentUtil.getValue(doc, fessConfig.getIndexFieldCreated(), Date.class);
if (created != null) {
final SimpleDateFormat sdf = new SimpleDateFormat(CoreLibConstants.DATE_FORMAT_ISO_8601_EXTEND);
createdStr = sdf.format(created);
} else {
createdStr = ComponentUtil.getMessageManager().getMessage(locale, "labels.search_unknown");
}
doc.put(CACHE_MSG, ComponentUtil.getMessageManager().getMessage(locale, "labels.search_cache_msg", url, createdStr));
doc.put(QUERIES, queries);
String cache = DocumentUtil.getValue(doc, fessConfig.getIndexFieldCache(), String.class);
if (cache != null) {
final String mimetype = DocumentUtil.getValue(doc, fessConfig.getIndexFieldMimetype(), String.class);
if (!ComponentUtil.getFessConfig().isHtmlMimetypeForCache(mimetype)) {
cache = StringEscapeUtils.escapeHtml4(cache);
}
cache = ComponentUtil.getPathMappingHelper().replaceUrls(cache);
if (queries != null && queries.length > 0) {
doc.put(HL_CACHE, replaceHighlightQueries(cache, queries));
} else {
doc.put(HL_CACHE, cache);
}
} else {
doc.put(fessConfig.getIndexFieldCache(), StringUtil.EMPTY);
doc.put(HL_CACHE, StringUtil.EMPTY);
}
try {
final Template template = handlebars.compile(cacheTemplateName);
final Context hbsContext = Context.newContext(doc);
return template.apply(hbsContext);
} catch (final Exception e) {
logger.warn("Failed to create a cache response.", e);
}
return null;
}
Aggregations