aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-02-20 13:22:58 +0000
committerMichael Williamson <mike@zwobble.org>2016-02-20 13:22:58 +0000
commita3eb5f0c3ee50afafc517c4a7e36501539489178 (patch)
tree3dbe7b9da437ed583666a367d7401bdfb131bef6 /src/main/java/org
parent56f4ecc7ebe8a19111daf54e505bc68b6c13c2e9 (diff)
Allow underling to be styled
Diffstat (limited to 'src/main/java/org')
-rw-r--r--src/main/java/org/zwobble/mammoth/DocumentConverter.java26
-rw-r--r--src/main/java/org/zwobble/mammoth/Mammoth.java3
-rw-r--r--src/main/java/org/zwobble/mammoth/styles/HtmlPath.java15
-rw-r--r--src/main/java/org/zwobble/mammoth/styles/HtmlPathElement.java13
-rw-r--r--src/main/java/org/zwobble/mammoth/styles/StyleMap.java17
5 files changed, 68 insertions, 6 deletions
diff --git a/src/main/java/org/zwobble/mammoth/DocumentConverter.java b/src/main/java/org/zwobble/mammoth/DocumentConverter.java
index 5b34cc4..0ec6e98 100644
--- a/src/main/java/org/zwobble/mammoth/DocumentConverter.java
+++ b/src/main/java/org/zwobble/mammoth/DocumentConverter.java
@@ -3,10 +3,14 @@ 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;
import java.io.IOException;
@@ -19,8 +23,8 @@ import static org.zwobble.mammoth.util.MammothLists.*;
import static org.zwobble.mammoth.util.MammothMaps.map;
public class DocumentConverter {
- public static List<HtmlNode> convertToHtml(String idPrefix, boolean preserveEmptyParagraphs, Document document) {
- DocumentConverter documentConverter = new DocumentConverter(idPrefix, preserveEmptyParagraphs);
+ public static List<HtmlNode> convertToHtml(String idPrefix, boolean preserveEmptyParagraphs, StyleMap styleMap, Document document) {
+ DocumentConverter documentConverter = new DocumentConverter(idPrefix, preserveEmptyParagraphs, styleMap);
List<HtmlNode> mainBody = documentConverter.convertChildrenToHtml(document);
// TODO: can you have note references inside a note?
List<Note> notes = findNotes(document, documentConverter.noteReferences);
@@ -41,17 +45,19 @@ public class DocumentConverter {
reference -> document.getNotes().findNote(reference.getNoteType(), reference.getNoteId()).get()));
}
- public static List<HtmlNode> convertToHtml(String idPrefix, boolean preserveEmptyParagraphs, DocumentElement element) {
- return new DocumentConverter(idPrefix, preserveEmptyParagraphs).convertToHtml(element);
+ public static List<HtmlNode> convertToHtml(String idPrefix, boolean preserveEmptyParagraphs, StyleMap styleMap, DocumentElement element) {
+ return new DocumentConverter(idPrefix, preserveEmptyParagraphs, styleMap).convertToHtml(element);
}
private final String idPrefix;
private final boolean preserveEmptyParagraphs;
+ private final StyleMap styleMap;
private final List<NoteReference> noteReferences = new ArrayList<>();
- private DocumentConverter(String idPrefix, boolean preserveEmptyParagraphs) {
+ private DocumentConverter(String idPrefix, boolean preserveEmptyParagraphs, StyleMap styleMap) {
this.idPrefix = idPrefix;
this.preserveEmptyParagraphs = preserveEmptyParagraphs;
+ this.styleMap = styleMap;
}
private HtmlNode convertToHtml(Note note) {
@@ -92,6 +98,9 @@ public class DocumentConverter {
if (run.isStrikethrough()) {
nodes = list(Html.collapsibleElement("s", nodes));
}
+ if (run.isUnderline()) {
+ nodes = wrapInPath(styleMap.getUnderline(), nodes);
+ }
if (run.getVerticalAlignment() == VerticalAlignment.SUBSCRIPT) {
nodes = list(Html.collapsibleElement("sub", nodes));
}
@@ -107,6 +116,13 @@ 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/Mammoth.java b/src/main/java/org/zwobble/mammoth/Mammoth.java
index 7c1d9c8..6db7308 100644
--- a/src/main/java/org/zwobble/mammoth/Mammoth.java
+++ b/src/main/java/org/zwobble/mammoth/Mammoth.java
@@ -10,6 +10,7 @@ import org.zwobble.mammoth.docx.DocxFile;
import org.zwobble.mammoth.docx.ZippedDocxFile;
import org.zwobble.mammoth.html.Html;
import org.zwobble.mammoth.results.Result;
+import org.zwobble.mammoth.styles.StyleMap;
import org.zwobble.mammoth.util.Casts;
import java.io.File;
@@ -48,7 +49,7 @@ public class Mammoth {
public static Result<String> convertToHtml(File file, Options options) {
return withDocxFile(file, zipFile ->
readDocument(zipFile)
- .map(nodes -> DocumentConverter.convertToHtml(options.idPrefix, options.preserveEmptyParagraphs, nodes))
+ .map(nodes -> DocumentConverter.convertToHtml(options.idPrefix, options.preserveEmptyParagraphs, StyleMap.EMPTY, nodes))
.map(Html::stripEmpty)
.map(Html::collapse)
.map(Html::write));
diff --git a/src/main/java/org/zwobble/mammoth/styles/HtmlPath.java b/src/main/java/org/zwobble/mammoth/styles/HtmlPath.java
new file mode 100644
index 0000000..27e63c4
--- /dev/null
+++ b/src/main/java/org/zwobble/mammoth/styles/HtmlPath.java
@@ -0,0 +1,15 @@
+package org.zwobble.mammoth.styles;
+
+import java.util.List;
+
+public class HtmlPath {
+ private final List<HtmlPathElement> elements;
+
+ public HtmlPath(List<HtmlPathElement> elements) {
+ this.elements = elements;
+ }
+
+ public List<HtmlPathElement> getElements() {
+ return elements;
+ }
+}
diff --git a/src/main/java/org/zwobble/mammoth/styles/HtmlPathElement.java b/src/main/java/org/zwobble/mammoth/styles/HtmlPathElement.java
new file mode 100644
index 0000000..0166c7a
--- /dev/null
+++ b/src/main/java/org/zwobble/mammoth/styles/HtmlPathElement.java
@@ -0,0 +1,13 @@
+package org.zwobble.mammoth.styles;
+
+public class HtmlPathElement {
+ private final String tagName;
+
+ public HtmlPathElement(String tagName) {
+ this.tagName = tagName;
+ }
+
+ public String getTagName() {
+ return tagName;
+ }
+}
diff --git a/src/main/java/org/zwobble/mammoth/styles/StyleMap.java b/src/main/java/org/zwobble/mammoth/styles/StyleMap.java
new file mode 100644
index 0000000..bdc8483
--- /dev/null
+++ b/src/main/java/org/zwobble/mammoth/styles/StyleMap.java
@@ -0,0 +1,17 @@
+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()));
+
+ private final HtmlPath underline;
+
+ public StyleMap(HtmlPath underline) {
+ this.underline = underline;
+ }
+
+ public HtmlPath getUnderline() {
+ return underline;
+ }
+}