aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-02-20 14:20:40 +0000
committerMichael Williamson <mike@zwobble.org>2016-02-20 14:20:40 +0000
commitbba2f4d95240ea3569e253609e95cc94c5c8ef13 (patch)
tree1a510988da0f17f4a3222a0b2f62de4b2f1cf641 /src
parenta3eb5f0c3ee50afafc517c4a7e36501539489178 (diff)
Add strikethrough to style map
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/zwobble/mammoth/DocumentConverter.java14
-rw-r--r--src/main/java/org/zwobble/mammoth/styles/HtmlPath.java18
-rw-r--r--src/main/java/org/zwobble/mammoth/styles/HtmlPathElement.java16
-rw-r--r--src/main/java/org/zwobble/mammoth/styles/StyleMap.java16
-rw-r--r--src/main/java/org/zwobble/mammoth/styles/StyleMapBuilder.java35
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java17
6 files changed, 90 insertions, 26 deletions
diff --git a/src/main/java/org/zwobble/mammoth/DocumentConverter.java b/src/main/java/org/zwobble/mammoth/DocumentConverter.java
index 0ec6e98..c2adf4d 100644
--- a/src/main/java/org/zwobble/mammoth/DocumentConverter.java
+++ b/src/main/java/org/zwobble/mammoth/DocumentConverter.java
@@ -3,13 +3,10 @@ package org.zwobble.mammoth;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
-import com.google.common.collect.Lists;
import com.google.common.io.ByteStreams;
import org.zwobble.mammoth.documents.*;
import org.zwobble.mammoth.html.Html;
import org.zwobble.mammoth.html.HtmlNode;
-import org.zwobble.mammoth.styles.HtmlPath;
-import org.zwobble.mammoth.styles.HtmlPathElement;
import org.zwobble.mammoth.styles.StyleMap;
import org.zwobble.mammoth.util.MammothLists;
@@ -96,10 +93,10 @@ public class DocumentConverter {
public List<HtmlNode> visit(Run run) {
List<HtmlNode> nodes = convertChildrenToHtml(run);
if (run.isStrikethrough()) {
- nodes = list(Html.collapsibleElement("s", nodes));
+ nodes = styleMap.getStrikethrough().wrap(nodes);
}
if (run.isUnderline()) {
- nodes = wrapInPath(styleMap.getUnderline(), nodes);
+ nodes = styleMap.getUnderline().wrap(nodes);
}
if (run.getVerticalAlignment() == VerticalAlignment.SUBSCRIPT) {
nodes = list(Html.collapsibleElement("sub", nodes));
@@ -116,13 +113,6 @@ public class DocumentConverter {
return nodes;
}
- private List<HtmlNode> wrapInPath(HtmlPath path, List<HtmlNode> nodes) {
- for (HtmlPathElement element : Lists.reverse(path.getElements())) {
- nodes = list(Html.element(element.getTagName(), nodes));
- }
- return nodes;
- }
-
@Override
public List<HtmlNode> visit(Text text) {
if (text.getValue().isEmpty()) {
diff --git a/src/main/java/org/zwobble/mammoth/styles/HtmlPath.java b/src/main/java/org/zwobble/mammoth/styles/HtmlPath.java
index 27e63c4..99bdf55 100644
--- a/src/main/java/org/zwobble/mammoth/styles/HtmlPath.java
+++ b/src/main/java/org/zwobble/mammoth/styles/HtmlPath.java
@@ -1,15 +1,29 @@
package org.zwobble.mammoth.styles;
+import com.google.common.collect.Lists;
+import org.zwobble.mammoth.html.HtmlNode;
+
import java.util.List;
public class HtmlPath {
+ public static HtmlPathElement element(String tagName) {
+ return new HtmlPathElement(tagName, false);
+ }
+
+ public static HtmlPathElement collapsibleElement(String tagName) {
+ return new HtmlPathElement(tagName, true);
+ }
+
private final List<HtmlPathElement> elements;
public HtmlPath(List<HtmlPathElement> elements) {
this.elements = elements;
}
- public List<HtmlPathElement> getElements() {
- return elements;
+ public List<HtmlNode> wrap(List<HtmlNode> nodes) {
+ for (HtmlPathElement element : Lists.reverse(elements)) {
+ nodes = element.wrap(nodes);
+ }
+ return nodes;
}
}
diff --git a/src/main/java/org/zwobble/mammoth/styles/HtmlPathElement.java b/src/main/java/org/zwobble/mammoth/styles/HtmlPathElement.java
index 0166c7a..d2d9363 100644
--- a/src/main/java/org/zwobble/mammoth/styles/HtmlPathElement.java
+++ b/src/main/java/org/zwobble/mammoth/styles/HtmlPathElement.java
@@ -1,13 +1,23 @@
package org.zwobble.mammoth.styles;
+import org.zwobble.mammoth.html.HtmlElement;
+import org.zwobble.mammoth.html.HtmlNode;
+
+import java.util.List;
+
+import static org.zwobble.mammoth.util.MammothLists.list;
+import static org.zwobble.mammoth.util.MammothMaps.map;
+
public class HtmlPathElement {
private final String tagName;
+ private final boolean isCollapsible;
- public HtmlPathElement(String tagName) {
+ public HtmlPathElement(String tagName, boolean isCollapsible) {
this.tagName = tagName;
+ this.isCollapsible = isCollapsible;
}
- public String getTagName() {
- return tagName;
+ public List<HtmlNode> wrap(List<HtmlNode> nodes) {
+ return list(new HtmlElement(tagName, map(), nodes, isCollapsible));
}
}
diff --git a/src/main/java/org/zwobble/mammoth/styles/StyleMap.java b/src/main/java/org/zwobble/mammoth/styles/StyleMap.java
index bdc8483..1db5c65 100644
--- a/src/main/java/org/zwobble/mammoth/styles/StyleMap.java
+++ b/src/main/java/org/zwobble/mammoth/styles/StyleMap.java
@@ -1,17 +1,25 @@
package org.zwobble.mammoth.styles;
-import static org.zwobble.mammoth.util.MammothLists.list;
-
public class StyleMap {
- public static final StyleMap EMPTY = new StyleMap(new HtmlPath(list()));
+ public static StyleMapBuilder builder() {
+ return new StyleMapBuilder();
+ }
+
+ public static final StyleMap EMPTY = new StyleMapBuilder().build();
private final HtmlPath underline;
+ private final HtmlPath strikethrough;
- public StyleMap(HtmlPath underline) {
+ public StyleMap(HtmlPath underline, HtmlPath strikethrough) {
this.underline = underline;
+ this.strikethrough = strikethrough;
}
public HtmlPath getUnderline() {
return underline;
}
+
+ public HtmlPath getStrikethrough() {
+ return strikethrough;
+ }
}
diff --git a/src/main/java/org/zwobble/mammoth/styles/StyleMapBuilder.java b/src/main/java/org/zwobble/mammoth/styles/StyleMapBuilder.java
new file mode 100644
index 0000000..93a2c8c
--- /dev/null
+++ b/src/main/java/org/zwobble/mammoth/styles/StyleMapBuilder.java
@@ -0,0 +1,35 @@
+package org.zwobble.mammoth.styles;
+
+import static org.zwobble.mammoth.util.MammothLists.list;
+
+public class StyleMapBuilder {
+ private HtmlPath underline;
+ private HtmlPath strikethrough;
+
+ public StyleMapBuilder() {
+ this.underline = new HtmlPath(list());
+ this.strikethrough = new HtmlPath(list(HtmlPath.collapsibleElement("s")));
+ }
+
+ public StyleMapBuilder underline(HtmlPathElement element) {
+ return underline(new HtmlPath(list(element)));
+ }
+
+ public StyleMapBuilder underline(HtmlPath path) {
+ this.underline = path;
+ return this;
+ }
+
+ public StyleMapBuilder strikethrough(HtmlPathElement element) {
+ return strikethrough(new HtmlPath(list(element)));
+ }
+
+ public StyleMapBuilder strikethrough(HtmlPath path) {
+ this.strikethrough = path;
+ return this;
+ }
+
+ public StyleMap build() {
+ return new StyleMap(underline, strikethrough);
+ }
+}
diff --git a/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java b/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java
index fd1402f..f81962b 100644
--- a/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java
+++ b/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java
@@ -6,7 +6,6 @@ import org.zwobble.mammoth.documents.*;
import org.zwobble.mammoth.html.Html;
import org.zwobble.mammoth.html.HtmlNode;
import org.zwobble.mammoth.styles.HtmlPath;
-import org.zwobble.mammoth.styles.HtmlPathElement;
import org.zwobble.mammoth.styles.StyleMap;
import java.io.ByteArrayInputStream;
@@ -15,9 +14,7 @@ import java.util.Optional;
import static com.natpryce.makeiteasy.MakeItEasy.*;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.contains;
-import static org.hamcrest.Matchers.hasEntry;
-import static org.hamcrest.Matchers.hasProperty;
+import static org.hamcrest.Matchers.*;
import static org.zwobble.mammoth.documents.VerticalAlignment.SUBSCRIPT;
import static org.zwobble.mammoth.documents.VerticalAlignment.SUPERSCRIPT;
import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals;
@@ -80,7 +77,8 @@ public class DocumentConverterTests {
assertThat(
convertToHtml(
make(a(RUN, with(UNDERLINE, true), with(CHILDREN, list(new Text("Hello"))))),
- new StyleMap(new HtmlPath(list(new HtmlPathElement("em"))))),
+ StyleMap.builder().underline(HtmlPath.element("em")).build()),
+
deepEquals(list(Html.element("em", list(Html.text("Hello"))))));
}
@@ -92,6 +90,15 @@ public class DocumentConverterTests {
}
@Test
+ public void struckthroughRunsCanBeMappedUsingStyleMapping() {
+ assertThat(
+ convertToHtml(
+ make(a(RUN, with(STRIKETHROUGH, true), with(CHILDREN, list(new Text("Hello"))))),
+ StyleMap.builder().strikethrough(HtmlPath.element("del")).build()),
+ deepEquals(list(Html.element("del", list(Html.text("Hello"))))));
+ }
+
+ @Test
public void superscriptRunsAreWrappedInSuperscriptTags() {
assertThat(
convertToHtml(make(a(RUN, with(VERTICAL_ALIGNMENT, SUPERSCRIPT), with(CHILDREN, list(new Text("Hello")))))),