diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-02-20 14:39:16 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-02-20 14:39:16 +0000 |
| commit | 9ac722cd96111afa9ec0522af4f6dbf95a62b7b4 (patch) | |
| tree | a299c551f97eb439928b4832d83dc050d4456031 /src/main/java/org/zwobble | |
| parent | e7040c3ad2424a7300c89d7d702ac2fc42d1bbb4 (diff) | |
Add style mappings for paragraphs by style ID
Diffstat (limited to 'src/main/java/org/zwobble')
5 files changed, 40 insertions, 8 deletions
diff --git a/src/main/java/org/zwobble/mammoth/DocumentConverter.java b/src/main/java/org/zwobble/mammoth/DocumentConverter.java index c2adf4d..ac09c8b 100644 --- a/src/main/java/org/zwobble/mammoth/DocumentConverter.java +++ b/src/main/java/org/zwobble/mammoth/DocumentConverter.java @@ -86,7 +86,7 @@ public class DocumentConverter { public List<HtmlNode> visit(Paragraph paragraph) { List<HtmlNode> content = convertChildrenToHtml(paragraph); List<HtmlNode> children = preserveEmptyParagraphs ? cons(Html.FORCE_WRITE, content) : content; - return list(Html.element("p", children)); + return styleMap.getParagraphStyleMapping(paragraph).wrap(children); } @Override diff --git a/src/main/java/org/zwobble/mammoth/styles/HtmlPath.java b/src/main/java/org/zwobble/mammoth/styles/HtmlPath.java index 99bdf55..b03d0b6 100644 --- a/src/main/java/org/zwobble/mammoth/styles/HtmlPath.java +++ b/src/main/java/org/zwobble/mammoth/styles/HtmlPath.java @@ -4,14 +4,21 @@ import com.google.common.collect.Lists; import org.zwobble.mammoth.html.HtmlNode; import java.util.List; +import java.util.Map; + +import static org.zwobble.mammoth.util.MammothMaps.map; public class HtmlPath { public static HtmlPathElement element(String tagName) { - return new HtmlPathElement(tagName, false); + return element(tagName, map()); + } + + public static HtmlPathElement element(String tagName, Map<String, String> attributes) { + return new HtmlPathElement(tagName, attributes, false); } public static HtmlPathElement collapsibleElement(String tagName) { - return new HtmlPathElement(tagName, true); + return new HtmlPathElement(tagName, map(), true); } private final List<HtmlPathElement> elements; diff --git a/src/main/java/org/zwobble/mammoth/styles/HtmlPathElement.java b/src/main/java/org/zwobble/mammoth/styles/HtmlPathElement.java index d2d9363..f250992 100644 --- a/src/main/java/org/zwobble/mammoth/styles/HtmlPathElement.java +++ b/src/main/java/org/zwobble/mammoth/styles/HtmlPathElement.java @@ -4,20 +4,22 @@ import org.zwobble.mammoth.html.HtmlElement; import org.zwobble.mammoth.html.HtmlNode; import java.util.List; +import java.util.Map; import static org.zwobble.mammoth.util.MammothLists.list; -import static org.zwobble.mammoth.util.MammothMaps.map; public class HtmlPathElement { private final String tagName; + private final Map<String, String> attributes; private final boolean isCollapsible; - public HtmlPathElement(String tagName, boolean isCollapsible) { + public HtmlPathElement(String tagName, Map<String, String> attributes, boolean isCollapsible) { this.tagName = tagName; + this.attributes = attributes; this.isCollapsible = isCollapsible; } public List<HtmlNode> wrap(List<HtmlNode> nodes) { - return list(new HtmlElement(tagName, map(), nodes, isCollapsible)); + return list(new HtmlElement(tagName, attributes, nodes, isCollapsible)); } } diff --git a/src/main/java/org/zwobble/mammoth/styles/StyleMap.java b/src/main/java/org/zwobble/mammoth/styles/StyleMap.java index 1db5c65..383554f 100644 --- a/src/main/java/org/zwobble/mammoth/styles/StyleMap.java +++ b/src/main/java/org/zwobble/mammoth/styles/StyleMap.java @@ -1,5 +1,12 @@ package org.zwobble.mammoth.styles; +import org.zwobble.mammoth.documents.Paragraph; + +import java.util.Map; +import java.util.Optional; + +import static org.zwobble.mammoth.util.MammothLists.list; + public class StyleMap { public static StyleMapBuilder builder() { return new StyleMapBuilder(); @@ -9,10 +16,12 @@ public class StyleMap { private final HtmlPath underline; private final HtmlPath strikethrough; + private final Map<String, HtmlPath> paragraphStyles; - public StyleMap(HtmlPath underline, HtmlPath strikethrough) { + public StyleMap(HtmlPath underline, HtmlPath strikethrough, Map<String, HtmlPath> paragraphStyles) { this.underline = underline; this.strikethrough = strikethrough; + this.paragraphStyles = paragraphStyles; } public HtmlPath getUnderline() { @@ -22,4 +31,10 @@ public class StyleMap { public HtmlPath getStrikethrough() { return strikethrough; } + + public HtmlPath getParagraphStyleMapping(Paragraph paragraph) { + return paragraph.getStyle() + .flatMap(style -> Optional.ofNullable(paragraphStyles.get(style.getStyleId()))) + .orElse(new HtmlPath(list(HtmlPath.element("p")))); + } } diff --git a/src/main/java/org/zwobble/mammoth/styles/StyleMapBuilder.java b/src/main/java/org/zwobble/mammoth/styles/StyleMapBuilder.java index 93a2c8c..c815e1b 100644 --- a/src/main/java/org/zwobble/mammoth/styles/StyleMapBuilder.java +++ b/src/main/java/org/zwobble/mammoth/styles/StyleMapBuilder.java @@ -1,10 +1,13 @@ package org.zwobble.mammoth.styles; +import com.google.common.collect.ImmutableMap; + import static org.zwobble.mammoth.util.MammothLists.list; public class StyleMapBuilder { private HtmlPath underline; private HtmlPath strikethrough; + private final ImmutableMap.Builder<String, HtmlPath> paragraphStyles = ImmutableMap.builder(); public StyleMapBuilder() { this.underline = new HtmlPath(list()); @@ -29,7 +32,12 @@ public class StyleMapBuilder { return this; } + public StyleMapBuilder mapParagraph(String styleId, HtmlPath path) { + paragraphStyles.put(styleId, path); + return this; + } + public StyleMap build() { - return new StyleMap(underline, strikethrough); + return new StyleMap(underline, strikethrough, paragraphStyles.build()); } } |
