diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-02-14 11:42:26 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-02-14 11:42:26 +0000 |
| commit | 8bdef5495085e7258ca884bdcc0159c264181895 (patch) | |
| tree | ac95ac3dafd6c747be7cbde8ca5788fa9844b2cd /src | |
| parent | 2c4946f27b559245d070a6d85ccfbea3d3051134 (diff) | |
Convert hyperlinks with href to HTML
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/java/org/zwobble/mammoth/DocumentConverter.java | 5 | ||||
| -rw-r--r-- | src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java | 9 |
2 files changed, 13 insertions, 1 deletions
diff --git a/src/main/java/org/zwobble/mammoth/DocumentConverter.java b/src/main/java/org/zwobble/mammoth/DocumentConverter.java index be2f68e..e8d9059 100644 --- a/src/main/java/org/zwobble/mammoth/DocumentConverter.java +++ b/src/main/java/org/zwobble/mammoth/DocumentConverter.java @@ -5,9 +5,11 @@ import org.zwobble.mammoth.html.Html; import org.zwobble.mammoth.html.HtmlNode; import java.util.List; +import java.util.Map; import static org.zwobble.mammoth.util.MammothLists.eagerFlatMap; import static org.zwobble.mammoth.util.MammothLists.list; +import static org.zwobble.mammoth.util.MammothMaps.map; public class DocumentConverter { public static List<HtmlNode> convertToHtml(Document document) { @@ -89,7 +91,8 @@ public class DocumentConverter { @Override public List<HtmlNode> visit(Hyperlink hyperlink) { - throw new UnsupportedOperationException(); + Map<String, String> attributes = map("href", hyperlink.getHref().get()); + return list(Html.element("a", attributes, convertChildrenToHtml(hyperlink))); } @Override diff --git a/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java b/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java index ff12f5d..db3087e 100644 --- a/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java @@ -4,6 +4,7 @@ import org.junit.Test; import org.zwobble.mammoth.DocumentConverter; import org.zwobble.mammoth.documents.Document; import org.zwobble.mammoth.documents.DocumentElement; +import org.zwobble.mammoth.documents.Hyperlink; import org.zwobble.mammoth.documents.Text; import org.zwobble.mammoth.html.Html; import org.zwobble.mammoth.html.HtmlNode; @@ -17,6 +18,7 @@ import static org.zwobble.mammoth.documents.VerticalAlignment.SUPERSCRIPT; import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals; import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.*; import static org.zwobble.mammoth.util.MammothLists.list; +import static org.zwobble.mammoth.util.MammothMaps.map; public class DocumentConverterTests { // TODO: styles (paragraph, run, underline, strikethrough) @@ -82,6 +84,13 @@ public class DocumentConverterTests { deepEquals(list(Html.element("sub", list(Html.text("Hello")))))); } + @Test + public void hyperlinkWithHrefIsConvertedToAnchorTag() { + assertThat( + convertToHtml(Hyperlink.href("http://example.com", list(new Text("Hello")))), + deepEquals(list(Html.element("a", map("href", "http://example.com"), list(Html.text("Hello")))))); + } + private List<HtmlNode> convertToHtml(DocumentElement element) { return DocumentConverter.convertToHtml(element); |
