Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handlebars helpers for statsheets #4188

Merged
merged 3 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,7 @@ dependencies {
implementation 'com.miglayout:miglayout-swing:11.1'

implementation 'com.github.jknack:handlebars:4.3.1'


implementation 'com.github.jknack:handlebars-helpers:4.3.1'
}


Expand Down
38 changes: 33 additions & 5 deletions src/main/java/net/rptools/maptool/util/HandlebarsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Template;
import com.github.jknack.handlebars.context.JavaBeanValueResolver;
import com.github.jknack.handlebars.helper.AssignHelper;
import com.github.jknack.handlebars.helper.ConditionalHelpers;
import com.github.jknack.handlebars.helper.NumberHelper;
import com.github.jknack.handlebars.helper.StringHelpers;
import java.io.IOException;
import java.util.Arrays;
import net.rptools.maptool.model.Token;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
* Utility class to apply a Handlebars template given a bean.
Expand All @@ -30,15 +38,29 @@ public class HandlebarsUtil<T> {
/** The compiled template. */
private final Template template;

/** Logging class instance. */
private static final Logger log = LogManager.getLogger(Token.class);

/**
* Creates a new instance of the utility class.
*
* @param stringTemplate The template to compile.
* @throws IOException If there is an error compiling the template.
*/
public HandlebarsUtil(String stringTemplate) throws IOException {
Handlebars handlebars = new Handlebars();
template = handlebars.compileInline(stringTemplate);
try {
Handlebars handlebars = new Handlebars();
StringHelpers.register(handlebars);
Arrays.stream(ConditionalHelpers.values())
.forEach(h -> handlebars.registerHelper(h.name(), h));
NumberHelper.register(handlebars);
handlebars.registerHelper(AssignHelper.NAME, AssignHelper.INSTANCE);

template = handlebars.compileInline(stringTemplate);
} catch (IOException e) {
log.error("Handlebars Error: {}", e.getMessage());
throw e;
}
}

/**
Expand All @@ -49,8 +71,14 @@ public HandlebarsUtil(String stringTemplate) throws IOException {
* @throws IOException If there is an error applying the template.
*/
public String apply(T bean) throws IOException {
Handlebars handlebars = new Handlebars();
var context = Context.newBuilder(bean).resolver(JavaBeanValueResolver.INSTANCE).build();
return template.apply(context);
try {

Handlebars handlebars = new Handlebars();
var context = Context.newBuilder(bean).resolver(JavaBeanValueResolver.INSTANCE).build();
return template.apply(context);
} catch (IOException e) {
log.error("Handlebars Error: {}", e.getMessage());
throw e;
}
}
}