Reworked user-defined escaping to suit my tastes.

This commit is contained in:
Michael Bayne
2013-08-01 15:45:38 -07:00
parent 0797c89c38
commit d1384cd049
7 changed files with 104 additions and 114 deletions
@@ -0,0 +1,40 @@
//
// JMustache - A Java implementation of the Mustache templating language
// http://github.com/samskivert/jmustache/blob/master/LICENSE
package com.samskivert.mustache;
/**
* Defines some standard {@link Mustache.Escaper}s.
*/
public class Escapers
{
/** Escapes HTML entities. */
public static final Mustache.Escaper HTML = simple(new String[][] {
{ "&", "&" },
{ "'", "'" },
{ "\"", """ },
{ "<", "&lt;" },
{ ">", "&gt;" }
});
/** An escaper that does no escaping. */
public static final Mustache.Escaper NONE = new Mustache.Escaper() {
@Override public String escape (String text) {
return text;
}
};
/** Returns an escaper that replaces a list of text sequences with canned replacements.
* @param repls a list of {@code (text, replacement)} pairs. */
public static Mustache.Escaper simple (final String[]... repls) {
return new Mustache.Escaper() {
@Override public String escape (String text) {
for (String[] escape : repls) {
text = text.replace(escape[0], escape[1]);
}
return text;
}
};
}
}