diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/java/org/zwobble/mammoth/Mammoth.java | 24 | ||||
| -rw-r--r-- | src/test/java/org/zwobble/mammoth/tests/MammothTests.java | 10 |
2 files changed, 23 insertions, 11 deletions
diff --git a/src/main/java/org/zwobble/mammoth/Mammoth.java b/src/main/java/org/zwobble/mammoth/Mammoth.java index 1e14482..d0c4aff 100644 --- a/src/main/java/org/zwobble/mammoth/Mammoth.java +++ b/src/main/java/org/zwobble/mammoth/Mammoth.java @@ -15,19 +15,31 @@ import java.util.zip.ZipFile; import static org.zwobble.mammoth.util.MammothLists.list; public class Mammoth { + public static class Options { + // TODO: what default prefix? + public static final Options DEFAULT = new Options("document"); + + private final String idPrefix; + + public Options(String idPrefix) { + this.idPrefix = idPrefix; + } + + public Options idPrefix(String prefix) { + return new Options(prefix); + } + } + @FunctionalInterface private interface BodyReaders { BodyXmlReader forName(String name); } - // TODO: provide Options builder public static Result<String> convertToHtml(File file) { - // TODO: prefix - String idPrefix = "document"; - return convertToHtml(file, idPrefix); + return convertToHtml(file, Options.DEFAULT); } - public static Result<String> convertToHtml(File file, String idPrefix) { + public static Result<String> convertToHtml(File file, Options options) { try (DocxFile zipFile = new ZippedDocxFile(new ZipFile(file))) { Styles styles = readStyles(zipFile); Numbering numbering = Numbering.EMPTY; @@ -44,7 +56,7 @@ public class Mammoth { DocumentXmlReader reader = new DocumentXmlReader(bodyReaders.forName("document"), notes); return reader.readElement(parseOfficeXml(zipFile, "word/document.xml")); }) - .map(nodes -> DocumentConverter.convertToHtml(idPrefix, nodes)) + .map(nodes -> DocumentConverter.convertToHtml(options.idPrefix, nodes)) .map(Html::stripEmpty) .map(Html::collapse) .map(Html::write); diff --git a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java index 9a44cbf..fd6d3c5 100644 --- a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java @@ -35,7 +35,7 @@ public class MammothTests { @Test public void footnotesAreAppendedToText() { assertThat( - convertToHtml("footnotes.docx", "doc-42"), + convertToHtml("footnotes.docx", Mammoth.Options.DEFAULT.idPrefix("doc-42")), deepEquals(success( "<p>Ouch" + "<sup><a href=\"#doc-42-footnote-1\" id=\"doc-42-footnote-ref-1\">[1]</a></sup>." + @@ -47,7 +47,7 @@ public class MammothTests { @Test public void endNotesAreAppendedToText() { assertThat( - convertToHtml("endnotes.docx", "doc-42"), + convertToHtml("endnotes.docx", Mammoth.Options.DEFAULT.idPrefix("doc-42")), deepEquals(success( "<p>Ouch" + "<sup><a href=\"#doc-42-endnote-2\" id=\"doc-42-endnote-ref-2\">[1]</a></sup>." + @@ -59,7 +59,7 @@ public class MammothTests { @Test public void relationshipsAreReadForEachFileContainingBodyXml() { assertThat( - convertToHtml("footnote-hyperlink.docx", "doc-42"), + convertToHtml("footnote-hyperlink.docx", Mammoth.Options.DEFAULT.idPrefix("doc-42")), deepEquals(success( "<p><sup><a href=\"#doc-42-footnote-1\" id=\"doc-42-footnote-ref-1\">[1]</a></sup></p>" + "<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>"))); @@ -70,8 +70,8 @@ public class MammothTests { return Mammoth.convertToHtml(file); } - private Result<String> convertToHtml(String name, String idPrefix) { + private Result<String> convertToHtml(String name, Mammoth.Options options) { File file = TestData.file(name); - return Mammoth.convertToHtml(file, idPrefix); + return Mammoth.convertToHtml(file, options); } } |
