From bba2f4d95240ea3569e253609e95cc94c5c8ef13 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 20 Feb 2016 14:20:40 +0000 Subject: Add strikethrough to style map --- .../org/zwobble/mammoth/DocumentConverter.java | 14 ++------- .../java/org/zwobble/mammoth/styles/HtmlPath.java | 18 +++++++++-- .../zwobble/mammoth/styles/HtmlPathElement.java | 16 ++++++++-- .../java/org/zwobble/mammoth/styles/StyleMap.java | 16 +++++++--- .../zwobble/mammoth/styles/StyleMapBuilder.java | 35 ++++++++++++++++++++++ 5 files changed, 78 insertions(+), 21 deletions(-) create mode 100644 src/main/java/org/zwobble/mammoth/styles/StyleMapBuilder.java (limited to 'src/main/java/org') 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 visit(Run run) { List 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 wrapInPath(HtmlPath path, List nodes) { - for (HtmlPathElement element : Lists.reverse(path.getElements())) { - nodes = list(Html.element(element.getTagName(), nodes)); - } - return nodes; - } - @Override public List 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 elements; public HtmlPath(List elements) { this.elements = elements; } - public List getElements() { - return elements; + public List wrap(List 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 wrap(List 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); + } +} -- cgit v1.2.3