aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/zwobble/mammoth/Mammoth.java46
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/MammothTests.java7
2 files changed, 46 insertions, 7 deletions
diff --git a/src/main/java/org/zwobble/mammoth/Mammoth.java b/src/main/java/org/zwobble/mammoth/Mammoth.java
index 591f899..6e4766e 100644
--- a/src/main/java/org/zwobble/mammoth/Mammoth.java
+++ b/src/main/java/org/zwobble/mammoth/Mammoth.java
@@ -1,14 +1,17 @@
package org.zwobble.mammoth;
-import org.zwobble.mammoth.documents.Notes;
+import com.google.common.collect.Iterables;
+import org.zwobble.mammoth.documents.*;
import org.zwobble.mammoth.docx.*;
import org.zwobble.mammoth.html.Html;
import org.zwobble.mammoth.results.Result;
+import org.zwobble.mammoth.util.Casts;
import org.zwobble.mammoth.util.MammothLists;
import org.zwobble.mammoth.xml.XmlElement;
import java.io.File;
import java.io.IOException;
+import java.util.List;
import java.util.Optional;
import java.util.zip.ZipFile;
@@ -45,6 +48,39 @@ public class Mammoth {
}
public static Result<String> convertToHtml(File file, Options options) {
+ return readDocument(file, options)
+ .map(nodes -> DocumentConverter.convertToHtml(options.idPrefix, options.preserveEmptyParagraphs, nodes))
+ .map(Html::stripEmpty)
+ .map(Html::collapse)
+ .map(Html::write);
+ }
+
+ public static Result<String> extractRawText(File file) {
+ return readDocument(file, Options.DEFAULT)
+ .map(document -> extractRawTextOfChildren(document));
+ }
+
+ private static String extractRawTextOfChildren(HasChildren parent) {
+ return extractRawText(parent.getChildren());
+ }
+
+ private static String extractRawText(List<DocumentElement> nodes) {
+ return String.join("", Iterables.transform(nodes, node -> extractRawText(node)));
+ }
+
+ private static String extractRawText(DocumentElement node) {
+ return Casts.tryCast(Text.class, node)
+ .map(Text::getValue)
+ .orElseGet(() -> {
+ List<DocumentElement> children = Casts.tryCast(HasChildren.class, node)
+ .map(HasChildren::getChildren)
+ .orElse(list());
+ String suffix = node instanceof Paragraph ? "\n\n" : "";
+ return extractRawText(children) + suffix;
+ });
+ }
+
+ private static Result<Document> readDocument(File file, Options options) {
try (DocxFile zipFile = new ZippedDocxFile(new ZipFile(file))) {
Styles styles = readStyles(zipFile);
Numbering numbering = Numbering.EMPTY;
@@ -60,13 +96,9 @@ public class Mammoth {
.flatMap(notes -> {
DocumentXmlReader reader = new DocumentXmlReader(bodyReaders.forName("document"), notes);
return reader.readElement(parseOfficeXml(zipFile, "word/document.xml"));
- })
- .map(nodes -> DocumentConverter.convertToHtml(options.idPrefix, options.preserveEmptyParagraphs, nodes))
- .map(Html::stripEmpty)
- .map(Html::collapse)
- .map(Html::write);
+ });
} catch (IOException e) {
- throw new UnsupportedOperationException("Should return a result of failure");
+ throw new UnsupportedOperationException("Should return a result of failure");
}
}
diff --git a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java
index 7186216..fb88ee2 100644
--- a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java
+++ b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java
@@ -72,6 +72,13 @@ public class MammothTests {
"<ol><li id=\"doc-42-footnote-1\"><p> <a href=\"http://www.example.com\">Example</a> <a href=\"#doc-42-footnote-ref-1\">↑</a></p></li></ol>")));
}
+ @Test
+ public void canExtractRawText() {
+ assertThat(
+ Mammoth.extractRawText(TestData.file("simple-list.docx")),
+ deepEquals(success("Apple\n\nBanana\n\n")));
+ }
+
private Result<String> convertToHtml(String name) {
File file = TestData.file(name);
return Mammoth.convertToHtml(file);