From 1dca829f94b394a4a63e71b125f25cc609d06235 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 16 Feb 2016 22:42:53 +0000 Subject: Allow preservation of empty paragraphs --- .../java/org/zwobble/mammoth/DocumentConverter.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'src/main/java/org/zwobble/mammoth/DocumentConverter.java') diff --git a/src/main/java/org/zwobble/mammoth/DocumentConverter.java b/src/main/java/org/zwobble/mammoth/DocumentConverter.java index a6029e0..cbcade5 100644 --- a/src/main/java/org/zwobble/mammoth/DocumentConverter.java +++ b/src/main/java/org/zwobble/mammoth/DocumentConverter.java @@ -15,13 +15,12 @@ import java.util.Base64; import java.util.List; import java.util.Map; -import static org.zwobble.mammoth.util.MammothLists.eagerFlatMap; -import static org.zwobble.mammoth.util.MammothLists.list; +import static org.zwobble.mammoth.util.MammothLists.*; import static org.zwobble.mammoth.util.MammothMaps.map; public class DocumentConverter { - public static List convertToHtml(String idPrefix, Document document) { - DocumentConverter documentConverter = new DocumentConverter(idPrefix); + public static List convertToHtml(String idPrefix, boolean preserveEmptyParagraphs, Document document) { + DocumentConverter documentConverter = new DocumentConverter(idPrefix, preserveEmptyParagraphs); List mainBody = documentConverter.convertChildrenToHtml(document); // TODO: can you have note references inside a note? List notes = findNotes(document, documentConverter.noteReferences); @@ -42,15 +41,17 @@ public class DocumentConverter { reference -> document.getNotes().findNote(reference.getNoteType(), reference.getNoteId()).get())); } - public static List convertToHtml(String idPrefix, DocumentElement element) { - return new DocumentConverter(idPrefix).convertToHtml(element); + public static List convertToHtml(String idPrefix, boolean preserveEmptyParagraphs, DocumentElement element) { + return new DocumentConverter(idPrefix, preserveEmptyParagraphs).convertToHtml(element); } private final String idPrefix; + private final boolean preserveEmptyParagraphs; private final List noteReferences = new ArrayList<>(); - private DocumentConverter(String idPrefix) { + private DocumentConverter(String idPrefix, boolean preserveEmptyParagraphs) { this.idPrefix = idPrefix; + this.preserveEmptyParagraphs = preserveEmptyParagraphs; } private HtmlNode convertToHtml(Note note) { @@ -81,7 +82,8 @@ public class DocumentConverter { @Override public List visit(Paragraph paragraph) { List content = convertChildrenToHtml(paragraph); - return list(Html.element("p", content)); + List children = preserveEmptyParagraphs ? cons(Html.FORCE_WRITE, content) : content; + return list(Html.element("p", children)); } @Override -- cgit v1.2.3