Misc cleanup
- Add javadoc to Config - Make CSSMin/JSMin configurable (add Minifier interface) - Change SimpleEscaper back to a util
This commit is contained in:
@@ -2,21 +2,48 @@ package j2html;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import j2html.utils.SimpleEscaper;
|
||||
import j2html.utils.TextEscaper;
|
||||
import j2html.utils.CSSMin;
|
||||
import j2html.utils.EscapeUtil;
|
||||
import j2html.utils.Indenter;
|
||||
import j2html.utils.JSMin;
|
||||
import j2html.utils.Minifier;
|
||||
import j2html.utils.TextEscaper;
|
||||
|
||||
public class Config {
|
||||
|
||||
private Config() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Change this to configure text-escaping
|
||||
* For example, to disable escaping, do 'Config.textEscaper = text -> text;'
|
||||
*/
|
||||
public static TextEscaper textEscaper = EscapeUtil::escape;
|
||||
|
||||
/**
|
||||
* Change this to configure css-minification.
|
||||
* The default minifier is https://github.com/barryvan/CSSMin
|
||||
*/
|
||||
public static Minifier cssMinifier = CSSMin::compressCss;
|
||||
|
||||
/**
|
||||
* Change this to configure js-minification.
|
||||
* The default minifier is a simple whitespace/newline stripper
|
||||
*/
|
||||
public static Minifier jsMinifier = JSMin::compressJs;
|
||||
|
||||
/**
|
||||
* Change this to configure indentation when rendering formatted html
|
||||
* The default is four spaces
|
||||
*/
|
||||
private static String FOUR_SPACES = " ";
|
||||
|
||||
public static TextEscaper textEscaper = new SimpleEscaper();
|
||||
|
||||
public static Indenter indenter = (level, text) -> String.join("", Collections.nCopies(level, FOUR_SPACES)) + text;
|
||||
|
||||
|
||||
/**
|
||||
* Change this to configure enable/disable closing empty tags (like <img/>)
|
||||
* The default is to NOT close them
|
||||
*/
|
||||
public static boolean closeEmptyTags = false;
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import j2html.Config;
|
||||
import j2html.utils.CSSMin;
|
||||
import j2html.utils.JSMin;
|
||||
|
||||
@@ -16,8 +17,8 @@ public class InlineStaticResource {
|
||||
public static ContainerTag get(String path, TargetFormat format) {
|
||||
String fileString = getFileAsString(path);
|
||||
switch (format) {
|
||||
case CSS_MIN : return style().with(rawHtml(CSSMin.compress(fileString)));
|
||||
case JS_MIN : return script().with(rawHtml(JSMin.compressJs(fileString)));
|
||||
case CSS_MIN : return style().with(rawHtml(Config.cssMinifier.minify(fileString)));
|
||||
case JS_MIN : return script().with(rawHtml(Config.jsMinifier.minify((fileString))));
|
||||
case CSS : return style().with(rawHtml(fileString));
|
||||
case JS : return script().with(rawHtml(fileString));
|
||||
default : throw new RuntimeException("Invalid target format");
|
||||
|
||||
@@ -79,7 +79,7 @@ public class CSSMin {
|
||||
* @param input the CSS
|
||||
* @return the compressed version
|
||||
*/
|
||||
public static String compress(String input) {
|
||||
public static String compressCss(String input) {
|
||||
try {
|
||||
int k,
|
||||
j, // Number of open braces
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package j2html.utils;
|
||||
|
||||
public class SimpleEscaper implements TextEscaper {
|
||||
public class EscapeUtil {
|
||||
|
||||
public String escape(String s) {
|
||||
public static String escape(String s) {
|
||||
if (s == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -302,4 +302,4 @@ public class JSMin {
|
||||
private class UnterminatedRegExpLiteralException extends Exception {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
6
src/main/java/j2html/utils/Minifier.java
Normal file
6
src/main/java/j2html/utils/Minifier.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package j2html.utils;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Minifier {
|
||||
String minify(String s);
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import java.util.concurrent.Callable;
|
||||
import org.apache.commons.lang3.StringEscapeUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import j2html.utils.SimpleEscaper;
|
||||
import j2html.utils.EscapeUtil;
|
||||
|
||||
public class PerformanceTest {
|
||||
|
||||
@@ -30,8 +30,8 @@ public class PerformanceTest {
|
||||
|
||||
@Test
|
||||
public void test_escaper_performnce() throws Exception {
|
||||
timeEscaper("SimpleEscaper#short", () -> new SimpleEscaper().escape(shortTestString));
|
||||
timeEscaper("SimpleEscaper#long", () -> new SimpleEscaper().escape(longTestString));
|
||||
timeEscaper("SimpleEscaper#short", () -> EscapeUtil.escape(shortTestString));
|
||||
timeEscaper("SimpleEscaper#long", () -> EscapeUtil.escape(longTestString));
|
||||
timeEscaper("ApacheEscaper#short", () -> StringEscapeUtils.escapeHtml4(shortTestString));
|
||||
timeEscaper("ApacheEscaper#long", () -> StringEscapeUtils.escapeHtml4(longTestString));
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package j2html;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import j2html.utils.SimpleEscaper;
|
||||
import j2html.utils.EscapeUtil;
|
||||
import j2html.utils.TextEscaper;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
@@ -12,9 +12,6 @@ public class TextEscaperTest {
|
||||
|
||||
@Test
|
||||
public void testTextEscaper() throws Exception {
|
||||
assertThat("default text escaper is SimpleEscaper",
|
||||
Config.textEscaper, is(instanceOf(SimpleEscaper.class)));
|
||||
|
||||
String expected = "<div></div>";
|
||||
assertThat("default text escaper works",
|
||||
Config.textEscaper.escape("<div></div>"), is(expected));
|
||||
@@ -26,7 +23,7 @@ public class TextEscaperTest {
|
||||
expected = "<div></div>";
|
||||
assertThat("user provided text escaper actually works",
|
||||
Config.textEscaper.escape("<div></div>"), is(expected));
|
||||
Config.textEscaper = new SimpleEscaper(); // reset escaper
|
||||
Config.textEscaper = EscapeUtil::escape; // reset escaper
|
||||
}
|
||||
|
||||
private static class NoOpEscaper implements TextEscaper {
|
||||
|
||||
Reference in New Issue
Block a user