Skip to content

Commit

Permalink
added option to escape HTML when compiling Mustache templates
Browse files Browse the repository at this point in the history
  • Loading branch information
albogdano committed May 16, 2022
1 parent 62a6550 commit 9d844f3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
14 changes: 12 additions & 2 deletions para-core/src/main/java/com/erudika/para/core/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,13 @@ public static String markdownToHtml(String markdownString, boolean htmlTagsRende
* @param template a Mustache template
* @return the compiled template string
*/
public static String compileMustache(Map<String, Object> context, String template) {
public static String compileMustache(Map<String, Object> context, String template, boolean escapeHtml) {
if (context == null || StringUtils.isBlank(template)) {
return "";
}
Writer writer = new StringWriter();
try {
Mustache.compiler().escapeHTML(false).emptyStringIsFalse(true).compile(template).execute(context, writer);
Mustache.compiler().escapeHTML(escapeHtml).emptyStringIsFalse(true).compile(template).execute(context, writer);
} finally {
try {
writer.close();
Expand All @@ -347,6 +347,16 @@ public static String compileMustache(Map<String, Object> context, String templat
return writer.toString();
}

/**
* @see #compileMustache(java.util.Map, java.lang.String, boolean)
* @param context a map of fields and values
* @param template a Mustache template
* @return the compiled template string
*/
public static String compileMustache(Map<String, Object> context, String template) {
return compileMustache(context, template, false);
}

/**
* Abbreviates a string.
* @param str a string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@
*/
package com.erudika.para.core.utils;

import com.erudika.para.core.utils.Config;
import com.erudika.para.core.utils.Utils;
import com.erudika.para.core.annotations.Locked;
import com.erudika.para.core.App;
import com.erudika.para.core.ParaObject;
import com.erudika.para.core.Sysprop;
import com.erudika.para.core.Tag;
import com.erudika.para.core.User;
import com.erudika.para.core.Votable;
import static com.erudika.para.core.utils.Utils.*;
import com.erudika.para.core.annotations.Locked;
import static com.erudika.para.core.utils.ParaObjectUtils.*;
import static com.erudika.para.core.utils.Utils.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -114,6 +112,8 @@ public void testCompileMustache() {
Map<String, Object> map = new HashMap<>();
map.put("test", "string");
assertEquals("<html>string</html>", compileMustache(map, "<html>{{test}}</html>"));
map.put("test", "<b>string</b>");
assertEquals("<html>&lt;b&gt;string&lt;/b&gt;</html>", compileMustache(map, "<html>{{test}}</html>", true));
}

@Test
Expand Down

0 comments on commit 9d844f3

Please sign in to comment.