aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-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
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java22
6 files changed, 87 insertions, 9 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;
+ }
+}
diff --git a/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java b/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java
index e53f698..fd1402f 100644
--- a/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java
+++ b/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java
@@ -5,6 +5,9 @@ import org.zwobble.mammoth.DocumentConverter;
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;
import java.util.List;
@@ -35,7 +38,7 @@ public class DocumentConverterTests {
@Test
public void forceWriteIsInsertedIntoParagraphIfEmptyParagraphsShouldBePreserved() {
assertThat(
- DocumentConverter.convertToHtml("", true, make(a(PARAGRAPH))),
+ DocumentConverter.convertToHtml("", true, StyleMap.EMPTY, make(a(PARAGRAPH))),
deepEquals(list(Html.element("p", list(Html.FORCE_WRITE)))));
}
@@ -73,6 +76,15 @@ public class DocumentConverterTests {
}
@Test
+ public void underliningCanBeMappedUsingStyleMapping() {
+ assertThat(
+ convertToHtml(
+ make(a(RUN, with(UNDERLINE, true), with(CHILDREN, list(new Text("Hello"))))),
+ new StyleMap(new HtmlPath(list(new HtmlPathElement("em"))))),
+ deepEquals(list(Html.element("em", list(Html.text("Hello"))))));
+ }
+
+ @Test
public void struckthroughRunsAreWrappedInStrikethroughTagsByDefault() {
assertThat(
convertToHtml(make(a(RUN, with(STRIKETHROUGH, true), with(CHILDREN, list(new Text("Hello")))))),
@@ -210,10 +222,14 @@ public class DocumentConverterTests {
}
private List<HtmlNode> convertToHtml(Document document) {
- return DocumentConverter.convertToHtml("doc-42-", false, document);
+ return DocumentConverter.convertToHtml("doc-42-", false, StyleMap.EMPTY, document);
}
private List<HtmlNode> convertToHtml(DocumentElement element) {
- return DocumentConverter.convertToHtml("doc-42-", false, element);
+ return convertToHtml(element, StyleMap.EMPTY);
+ }
+
+ private List<HtmlNode> convertToHtml(DocumentElement element, StyleMap styleMap) {
+ return DocumentConverter.convertToHtml("doc-42-", false, styleMap, element);
}
}