diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-11-19 20:17:38 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-11-19 20:17:38 +0000 |
| commit | 1c320474b93de8c90bf903f6a748e83a9aa65706 (patch) | |
| tree | 24f3b1f235cbfa65680798bafd5e044f0503bb20 /src | |
| parent | cf51080fc233196a5bf6c40c06e1a4ec32e85d5b (diff) | |
Allow embedded style maps to be disabled
Diffstat (limited to 'src')
3 files changed, 47 insertions, 14 deletions
diff --git a/src/main/java/org/zwobble/mammoth/DocumentConverter.java b/src/main/java/org/zwobble/mammoth/DocumentConverter.java index 3a21fed..feba5ac 100644 --- a/src/main/java/org/zwobble/mammoth/DocumentConverter.java +++ b/src/main/java/org/zwobble/mammoth/DocumentConverter.java @@ -52,6 +52,14 @@ public class DocumentConverter { } /** + * By default, if the document contains an embedded style map, then it is combined with the default style map. + * Call this to ignore any embedded style maps. + */ + public DocumentConverter disableEmbeddedStyleMap() { + return new DocumentConverter(options.disableEmbeddedStyleMap()); + } + + /** * Converts {@code stream} into an HTML string. * Note that using this method instead of {@link #convertToHtml(File file)} * means that relative paths to other files, such as images, cannot be resolved. diff --git a/src/main/java/org/zwobble/mammoth/internal/conversion/DocumentToHtmlOptions.java b/src/main/java/org/zwobble/mammoth/internal/conversion/DocumentToHtmlOptions.java index 1133018..8a709af 100644 --- a/src/main/java/org/zwobble/mammoth/internal/conversion/DocumentToHtmlOptions.java +++ b/src/main/java/org/zwobble/mammoth/internal/conversion/DocumentToHtmlOptions.java @@ -5,26 +5,37 @@ import org.zwobble.mammoth.internal.styles.StyleMap; import org.zwobble.mammoth.internal.styles.parsing.StyleMapParser; public class DocumentToHtmlOptions { - public static final DocumentToHtmlOptions DEFAULT = new DocumentToHtmlOptions("", false, StyleMap.EMPTY, false); + public static final DocumentToHtmlOptions DEFAULT = new DocumentToHtmlOptions("", false, StyleMap.EMPTY, StyleMap.EMPTY, false, false); private final String idPrefix; private final boolean preserveEmptyParagraphs; private final StyleMap styleMap; + private final StyleMap embeddedStyleMap; private final boolean disableDefaultStyleMap; - - public DocumentToHtmlOptions(String idPrefix, boolean preserveEmptyParagraphs, StyleMap styleMap, boolean disableDefaultStyleMap) { + private final boolean disableEmbeddedStyleMap; + + public DocumentToHtmlOptions( + String idPrefix, + boolean preserveEmptyParagraphs, + StyleMap styleMap, + StyleMap embeddedStyleMap, + boolean disableDefaultStyleMap, + boolean disableEmbeddedStyleMap + ) { this.idPrefix = idPrefix; this.preserveEmptyParagraphs = preserveEmptyParagraphs; this.styleMap = styleMap; + this.embeddedStyleMap = embeddedStyleMap; this.disableDefaultStyleMap = disableDefaultStyleMap; + this.disableEmbeddedStyleMap = disableEmbeddedStyleMap; } public DocumentToHtmlOptions idPrefix(String prefix) { - return new DocumentToHtmlOptions(prefix, preserveEmptyParagraphs, styleMap, disableDefaultStyleMap); + return new DocumentToHtmlOptions(prefix, preserveEmptyParagraphs, styleMap, embeddedStyleMap, disableDefaultStyleMap, disableEmbeddedStyleMap); } public DocumentToHtmlOptions preserveEmptyParagraphs() { - return new DocumentToHtmlOptions(idPrefix, true, styleMap, disableDefaultStyleMap); + return new DocumentToHtmlOptions(idPrefix, true, styleMap, embeddedStyleMap, disableDefaultStyleMap, disableEmbeddedStyleMap); } public DocumentToHtmlOptions addStyleMap(String styleMap) { @@ -32,19 +43,19 @@ public class DocumentToHtmlOptions { } public DocumentToHtmlOptions addStyleMap(StyleMap styleMap) { - return withStyleMap(this.styleMap.update(styleMap)); + return new DocumentToHtmlOptions(idPrefix, preserveEmptyParagraphs, this.styleMap.update(styleMap), embeddedStyleMap, disableDefaultStyleMap, disableEmbeddedStyleMap); } public DocumentToHtmlOptions disableDefaultStyleMap() { - return new DocumentToHtmlOptions(idPrefix, preserveEmptyParagraphs, styleMap, true); + return new DocumentToHtmlOptions(idPrefix, preserveEmptyParagraphs, styleMap, embeddedStyleMap, true, disableEmbeddedStyleMap); } - public DocumentToHtmlOptions addEmbeddedStyleMap(StyleMap embeddedStyleMap) { - return withStyleMap(StyleMap.merge(styleMap, embeddedStyleMap)); + public DocumentToHtmlOptions disableEmbeddedStyleMap() { + return new DocumentToHtmlOptions(idPrefix, preserveEmptyParagraphs, styleMap, embeddedStyleMap, disableDefaultStyleMap, true); } - private DocumentToHtmlOptions withStyleMap(StyleMap styleMap) { - return new DocumentToHtmlOptions(idPrefix, preserveEmptyParagraphs, styleMap, disableDefaultStyleMap); + public DocumentToHtmlOptions addEmbeddedStyleMap(StyleMap embeddedStyleMap) { + return new DocumentToHtmlOptions(idPrefix, preserveEmptyParagraphs, styleMap, embeddedStyleMap, disableDefaultStyleMap, disableEmbeddedStyleMap); } public String idPrefix() { @@ -56,8 +67,14 @@ public class DocumentToHtmlOptions { } public StyleMap styleMap() { - return disableDefaultStyleMap ? styleMap : DefaultStyles.DEFAULT_STYLE_MAP.update(styleMap); + StyleMap styleMap = StyleMap.EMPTY; + if (!disableDefaultStyleMap) { + styleMap = styleMap.update(DefaultStyles.DEFAULT_STYLE_MAP); + } + if (!disableEmbeddedStyleMap) { + styleMap = styleMap.update(embeddedStyleMap); + } + styleMap = styleMap.update(this.styleMap); + return styleMap; } - - } diff --git a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java index bafd857..d7a2b66 100644 --- a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java @@ -252,6 +252,14 @@ public class MammothTests { } @Test + public void embeddedStyleMapsCanBeDisabled() throws IOException { + assertThat( + convertToHtml("embedded-style-map.docx", options -> options.disableEmbeddedStyleMap()), + isSuccess("<p>Walking on imported air</p>") + ); + } + + @Test public void embeddedStyleMapCanBeWrittenAndThenRead() throws IOException { InMemoryArchive archive = InMemoryArchive.fromStream(new FileInputStream(TestData.file("single-paragraph.docx"))); EmbeddedStyleMap.embedStyleMap(archive, "p => h1"); |
