提交 c87513fc 编写于 作者: sq3536's avatar sq3536

handlebars

上级 aed15049
......@@ -48,7 +48,7 @@ public abstract class AbstractTemplatingEngineAdapter implements TemplatingEngin
*
* @return The full path, without extension (e.g. /path/to/file.txt => /path/to/file)
*/
private String getPathWithoutExtension(String location) {
public String getPathWithoutExtension(String location) {
if (location == null) return null;
int idx = location.lastIndexOf('.');
if (idx == -1) return location;
......
......@@ -17,16 +17,14 @@
package cn.ibizlab.codegen.templating;
import cn.ibizlab.codegen.templating.handlebars.BaseModelValueResolver;
import com.github.jknack.handlebars.Context;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Jackson2Helper;
import com.github.jknack.handlebars.Template;
import com.github.jknack.handlebars.*;
import com.github.jknack.handlebars.context.FieldValueResolver;
import com.github.jknack.handlebars.context.JavaBeanValueResolver;
import com.github.jknack.handlebars.context.MapValueResolver;
import com.github.jknack.handlebars.context.MethodValueResolver;
import com.github.jknack.handlebars.helper.ConditionalHelpers;
import com.github.jknack.handlebars.helper.StringHelpers;
import com.github.jknack.handlebars.helper.WithHelper;
import com.github.jknack.handlebars.io.AbstractTemplateLoader;
import com.github.jknack.handlebars.io.StringTemplateSource;
import com.github.jknack.handlebars.io.TemplateLoader;
......@@ -46,7 +44,7 @@ public class HandlebarsEngineAdapter extends AbstractTemplatingEngineAdapter {
// We use this as a simple lookup for valid file name extensions. This adapter will inspect .mustache (built-in) and infer the relevant handlebars filename
private final String[] canCompileFromExtensions = {".handlebars",".hbs",".mustache"};
private boolean infiniteLoops = false;
private boolean prettyPrint = false;
private boolean prettyPrint = true;
/**
* Provides an identifier used to load the adapter. This could be a name, uuid, or any other string.
......@@ -79,8 +77,31 @@ public class HandlebarsEngineAdapter extends AbstractTemplatingEngineAdapter {
Handlebars handlebars = new Handlebars(loader);
handlebars.registerHelperMissing((obj, options) -> {
//LOGGER.warn(String.format(Locale.ROOT, "Unregistered helper name '%s', processing template:%n%s", options.helperName, options.fn.text()));
Options.Buffer buffer = options.buffer();
if("with".equalsIgnoreCase(options.helperName))
{
if (options.isFalsy(obj)) {
buffer.append(options.inverse());
} else {
buffer.append(options.fn());
}
}
else if("unless".equalsIgnoreCase(options.helperName))
{
if (options.isFalsy(obj)) {
buffer.append(options.fn());
} else {
buffer.append(options.inverse());
}
}
else
{
LOGGER.warn(String.format(Locale.ROOT, "Unregistered helper name '%s', processing template:%n%s", options.helperName, options.fn.text()));
return "";
}
return buffer;
});
handlebars.registerHelper("json", Jackson2Helper.INSTANCE);
StringHelpers.register(handlebars);
......@@ -116,7 +137,17 @@ public class HandlebarsEngineAdapter extends AbstractTemplatingEngineAdapter {
handlebars.setInfiniteLoops(infiniteLoops);
handlebars.setPrettyPrint(prettyPrint);
Template tmpl = handlebars.compileInline(templateFile);
return tmpl.apply(context);
String path = tmpl.apply(context);
for(String ext:getFileExtensions())
{
if(path.endsWith("."+ext))
{
path=this.getPathWithoutExtension(path);
break;
}
}
return path;
}
@SuppressWarnings("java:S108")
......
......@@ -30,7 +30,7 @@
<git-commit-id-plugin.version>4.9.10</git-commit-id-plugin.version>
<groovy.version>3.0.9</groovy.version>
<guava.version>30.1.1-jre</guava.version>
<handlebars-java.version>4.2.1</handlebars-java.version>
<handlebars-java.version>4.3.0</handlebars-java.version>
<jackson-threetenbp.version>2.10.0</jackson-threetenbp.version>
<jacoco.version>0.8.7</jacoco.version>
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册