Skip to content

Commit

Permalink
feat(mustache): Additional string processing functions. Resolves #961
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Sep 19, 2022
1 parent 0144b9e commit 82f108c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies {
api project(':jreleaser-tool-java-sdk')
api project(':jreleaser-utils')
api project(':jreleaser-templates')
api "org.apache.commons:commons-text:$commonsTextVersion"

compileOnly "org.kordamp.jipsy:jipsy-annotations:${jipsyVersion}"
annotationProcessor "org.kordamp.jipsy:jipsy-processor:${jipsyVersion}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.text.StringEscapeUtils;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.jreleaser.bundle.RB;
import org.jreleaser.extensions.api.mustache.MustacheExtensionPoint;
import org.jreleaser.model.Constants;
import org.jreleaser.mustache.MustacheUtils;
import org.jreleaser.util.Algorithm;
import org.jreleaser.util.StringUtils;

Expand Down Expand Up @@ -69,6 +71,23 @@ public void apply(Map<String, Object> context) {
EnumSet.allOf(Algorithm.class)
.forEach(algorithm -> context.put("f_checksum_" + algorithm.formatted(), new FileChecksumFunction(algorithm)));
context.put("f_json", new JsonFunction());
context.put("f_escape_csv", new DelegatingFunction(StringEscapeUtils::escapeCsv));
context.put("f_escape_ecma_script", new DelegatingFunction(StringEscapeUtils::escapeEcmaScript));
context.put("f_escape_html3", new DelegatingFunction(StringEscapeUtils::escapeHtml3));
context.put("f_escape_html4", new DelegatingFunction(StringEscapeUtils::escapeHtml4));
context.put("f_escape_java", new DelegatingFunction(StringEscapeUtils::escapeJava));
context.put("f_escape_json", new DelegatingFunction(StringEscapeUtils::escapeJson));
context.put("f_escape_xml10", new DelegatingFunction(StringEscapeUtils::escapeXml10));
context.put("f_escape_xml11", new DelegatingFunction(StringEscapeUtils::escapeXml11));
context.put("f_escape_xsi", new DelegatingFunction(StringEscapeUtils::escapeXSI));
context.put("f_chop", new DelegatingFunction(org.apache.commons.lang3.StringUtils::chop));
context.put("f_delete_whitespace", new DelegatingFunction(org.apache.commons.lang3.StringUtils::deleteWhitespace));
context.put("f_normalize_whitespace", new DelegatingFunction(org.apache.commons.lang3.StringUtils::normalizeSpace));
context.put("f_reverse", new DelegatingFunction(org.apache.commons.lang3.StringUtils::reverse));
context.put("f_strip", new DelegatingFunction(org.apache.commons.lang3.StringUtils::strip));
context.put("f_swapcase", new DelegatingFunction(org.apache.commons.lang3.StringUtils::swapCase));

context.put("f_recursive_eval", new RecursiveEvalFunction(context));
}

private static class TimeFormatFunction implements Function<String, String> {
Expand Down Expand Up @@ -231,4 +250,30 @@ public String apply(Object input) {
}
}
}

private static class DelegatingFunction implements Function<String, String> {
private final Function<String, String> delegate;

private DelegatingFunction(Function<String, String> delegate) {
this.delegate = delegate;
}

@Override
public String apply(String input) {
return delegate.apply(input);
}
}

private class RecursiveEvalFunction implements Function<String, String> {
private final Map<String, Object> context;

public RecursiveEvalFunction(Map<String, Object> context) {
this.context = context;
}

@Override
public String apply(String input) {
return MustacheUtils.applyTemplate(input, context);
}
}
}
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ commonsIoVersion = 2.11.0
commonsLang3Version = 3.12.0
commonmarkVersion = 0.19.0
commonsNetVersion = 3.8.0
commonsTextVersion = 1.9
feignVersion = 11.9.1
feignFormVersion = 3.8.0
githubVersion = 1.129
Expand Down

0 comments on commit 82f108c

Please sign in to comment.