diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81a66a0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/target/ +/.settings/ +/.classpath +/.project +/build/ +/.idea/ +*.iml + diff --git a/README.md b/README.md new file mode 100644 index 0000000..9d1dad5 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +## HTML Utils ## + +A collection of useful HTML utilities. + +Pretty Print + +```java +String pretty = HtmlUtils.prettyPring(html); +``` + +**Note:** Default spacing = 4 + +Pretty Print with defined spacing + +```java +String pretty = HtmlUtils.prettyPring(html, 2); +``` diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..953ced4 --- /dev/null +++ b/pom.xml @@ -0,0 +1,67 @@ + + 4.0.0 + + io.ljunggren + html-utils + 0.0.1-SNAPSHOT + jar + + html-utils + http://maven.apache.org + + + UTF-8 + 1.8 + 1.8 + + + + + org.jsoup + jsoup + 1.12.1 + + + junit + junit + 4.13.2 + test + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + + nexus + http://nexus.ljunggren.io/repository/maven-releases/ + + + nexus + http://nexus.ljunggren.io/repository/maven-snapshots/ + + + + + https://gitbucket.ljunggren.io/git/aljunggren/html-utils.git + scm:git:https://gitbucket.ljunggren.io/git/aljunggren/html-utils.git + scm:git:https://gitbucket.ljunggren.io/git/aljunggren/html-utils.git + HEAD + + + diff --git a/src/main/java/io/ljunggren/html/utils/HtmlUtils.java b/src/main/java/io/ljunggren/html/utils/HtmlUtils.java new file mode 100644 index 0000000..063d67b --- /dev/null +++ b/src/main/java/io/ljunggren/html/utils/HtmlUtils.java @@ -0,0 +1,27 @@ +package io.ljunggren.html.utils; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; + +public class HtmlUtils { + + public static String prettyPrint(String string) { + if (string == null) { + return null; + } + return prettyPrint(string, 4); + } + + public static String prettyPrint(String string, int indentAmount) { + if (string == null) { + return null; + } + if (indentAmount < 0) { + indentAmount = 0; + } + Document doc = Jsoup.parse(string); + doc.outputSettings().indentAmount(indentAmount); + return doc.toString(); + } + +} \ No newline at end of file diff --git a/src/test/java/io/ljunggren/html/utils/HtmlUtilsTest.java b/src/test/java/io/ljunggren/html/utils/HtmlUtilsTest.java new file mode 100644 index 0000000..7411da7 --- /dev/null +++ b/src/test/java/io/ljunggren/html/utils/HtmlUtilsTest.java @@ -0,0 +1,57 @@ +package io.ljunggren.html.utils; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class HtmlUtilsTest { + + @Test + public void prettyPrintTest() { + String expected = "\n" + + " \n" + + " \n" + + " Hello World\n" + + " \n" + + ""; + String actual = HtmlUtils.prettyPrint("Hello World"); + assertEquals(expected, actual); + } + + @Test + public void prettyPrintNullTest() { + String actual = HtmlUtils.prettyPrint(null); + assertNull(actual); + } + + @Test + public void prettyPrintWithDefinedSpacingTest() { + String expected = "\n" + + " \n" + + " \n" + + " Hello World\n" + + " \n" + + ""; + String actual = HtmlUtils.prettyPrint("Hello World", 2); + assertEquals(expected, actual); + } + + @Test + public void prettyPrintWithDefinedSpacingNullTest() { + String actual = HtmlUtils.prettyPrint(null, 2); + assertNull(actual); + } + + @Test + public void prettyPrintWithNegativeValueTest() { + String expected = "\n" + + "\n" + + "\n" + + "Hello World\n" + + "\n" + + ""; + String actual = HtmlUtils.prettyPrint("Hello World", -2); + assertEquals(expected, actual); + } + +}