From d5b3447ec8832e893e096085052bd857649898ed Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 14 Feb 2016 18:56:36 +0000 Subject: Increment note indices --- .../org/zwobble/mammoth/DocumentConverter.java | 24 ++++++---------------- 1 file changed, 6 insertions(+), 18 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 47e24ee..a8d90f9 100644 --- a/src/main/java/org/zwobble/mammoth/DocumentConverter.java +++ b/src/main/java/org/zwobble/mammoth/DocumentConverter.java @@ -9,6 +9,7 @@ import org.zwobble.mammoth.html.Html; import org.zwobble.mammoth.html.HtmlNode; import java.io.IOException; +import java.util.ArrayList; import java.util.Base64; import java.util.List; import java.util.Map; @@ -22,7 +23,7 @@ public class DocumentConverter { DocumentConverter documentConverter = new DocumentConverter(idPrefix); List mainBody = documentConverter.convertChildrenToHtml(document); // TODO: can you have note references inside a note? - List notes = findNotes(document); + List notes = findNotes(document, documentConverter.noteReferences); if (notes.isEmpty()) { return mainBody; } else { @@ -33,32 +34,19 @@ public class DocumentConverter { } } - private static List findNotes(Document document) { - Iterable noteReferences = Iterables.filter(descendants(document), NoteReference.class); + private static List findNotes(Document document, Iterable noteReferences) { return ImmutableList.copyOf(Iterables.transform( noteReferences, // TODO: handle missing notes reference -> document.getNotes().findNote(reference.getNoteType(), reference.getNoteId()).get())); } - private static Iterable descendants(HasChildren element) { - return Iterables.concat(Iterables.transform(element.getChildren(), DocumentConverter::descendantsAndSelf)); - } - - private static Iterable descendantsAndSelf(DocumentElement element) { - if (element instanceof HasChildren) { - HasChildren hasChildren = (HasChildren) element; - return Iterables.concat(descendants(hasChildren), list(element)); - } else { - return list(element); - } - } - public static List convertToHtml(String idPrefix, DocumentElement element) { return new DocumentConverter(idPrefix).convertToHtml(element); } private final String idPrefix; + private final List noteReferences = new ArrayList<>(); private DocumentConverter(String idPrefix) { this.idPrefix = idPrefix; @@ -176,12 +164,12 @@ public class DocumentConverter { @Override public List visit(NoteReference noteReference) { + noteReferences.add(noteReference); String noteAnchor = generateNoteHtmlId(noteReference.getNoteType(), noteReference.getNoteId()); String noteReferenceAnchor = generateNoteRefHtmlId(noteReference.getNoteType(), noteReference.getNoteId()); return list(Html.element("sup", list( Html.element("a", map("href", "#" + noteAnchor, "id", noteReferenceAnchor), list( - // TODO: increment note indices - Html.text("[1]")))))); + Html.text("[" + noteReferences.size() + "]")))))); } @Override -- cgit v1.2.3