Skip to content
This repository has been archived by the owner on Nov 29, 2022. It is now read-only.

docx: footnote bodies should be taken from the FootnoteBlock #94

Merged
merged 17 commits into from
Feb 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.kemitix.binder.app;

import org.jetbrains.annotations.NotNull;

import javax.enterprise.inject.Instance;
import javax.enterprise.util.TypeLiteral;
import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -46,7 +44,6 @@ public void destroy(T instance) {
throw new UnsupportedOperationException();
}

@NotNull
@Override
public Iterator<T> iterator() {
return instances.iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class DocxFacade
private final WordprocessingMLPackage mlPackage;

private final ObjectFactory factory = getWmlObjectFactory();
private final AtomicInteger myFootnoteRef = new AtomicInteger(0);
private final AtomicInteger myFootnoteRef = new AtomicInteger(-1);

@Inject
public DocxFacade(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.kemitix.binder.docx;

import lombok.SneakyThrows;
import net.kemitix.binder.spi.ManuscriptFormatException;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.parts.WordprocessingML.FootnotesPart;
import org.docx4j.wml.CTFootnotes;
Expand All @@ -20,20 +21,26 @@
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public interface DocxFacadeFootnoteMixIn
extends DocxFacadeParagraphMixIn {

AtomicInteger footnoteRef();

default R footnote(String ordinal, List<P> footnoteBody) {
//TODO add footnote bodies from FootnoteBlockDocxNodeHandler
// this will provide styles footnotes
return footnoteReference(footnoteBody);

/**
* Creates a Footnote with no body.
*
* <p>Use {@link #footnoteAddBody(R, Stream)} ()} to add tge body.</p>
* @param ordinal the ordinal of the footnote
* @return an R containing the footnote anchor as a subscript
*/
default R footnote(String ordinal) {
return footnoteReference(ordinal);
}

@SneakyThrows
default R footnoteReference(List<P> footnoteBody) {
default R footnoteReference(String ordinal) {
// in document.xml:
// <w:r>
// <w:rPr>
Expand All @@ -45,16 +52,21 @@ default R footnoteReference(List<P> footnoteBody) {
CTFootnotes contents = footnotesPart.getContents();
List<CTFtnEdn> footnotes = contents.getFootnote();
CTFtnEdn ctFtnEdn = getNextCtFtnEdn(footnotes);
ctFtnEdn.getContent().addAll(Arrays.asList(footnoteBody(footnoteBody)));
// //ctFtnEdn.getContent().addAll(Arrays.asList(footnoteBody(footnoteBody)));
CTFtnEdnRef ctFtnEdnRef = factory().createCTFtnEdnRef();
ctFtnEdnRef.setId(ctFtnEdn.getId());
BigInteger id = ctFtnEdn.getId();
ctFtnEdnRef.setId(id);
JAXBElement<CTFtnEdnRef> footnoteReference = factory().createRFootnoteReference(ctFtnEdnRef);

String footnoteOrdinal = ctFtnEdn.getId().toString();
String footnoteOrdinal = id.toString();
if (!footnoteOrdinal.equals(ordinal)) {
throw new RuntimeException(
"Footnotes are not in order. Found %s, when expecting %s"
.formatted(ordinal, footnoteOrdinal));
}

R r = r(new Object[]{
footnoteReference,
t(footnoteOrdinal)
footnoteReference
});

RPr rPr = rPr(r);
Expand All @@ -63,6 +75,29 @@ default R footnoteReference(List<P> footnoteBody) {
return r;
}

@SneakyThrows
default R footnoteAddBody(R r, Stream<Object> content) {
FootnotesPart footnotesPart = getFootnotesPart();
CTFootnotes contents = footnotesPart.getContents();
List<CTFtnEdn> footnotes = contents.getFootnote();
List<Object> rContent = r.getContent();
JAXBElement<CTFtnEdnRef> footnoteReference = (JAXBElement<CTFtnEdnRef>) rContent.get(0);
CTFtnEdnRef ctFtnEdnRef = footnoteReference.getValue();
BigInteger id = ctFtnEdnRef.getId();
CTFtnEdn ctFtnEdn = getCtFtnEdnById(footnotes, id);
List<Object> ctFtnEdnContent = ctFtnEdn.getContent();
List<Object> paras = Arrays.asList(footnoteBody(content.map(P.class::cast).collect(Collectors.toList())));
ctFtnEdnContent.addAll(paras);
return r;
}

default CTFtnEdn getCtFtnEdnById(List<CTFtnEdn> footnotes, BigInteger id) {
return footnotes.stream()
.filter(o -> o.getId().equals(id))
.findFirst()
.orElseThrow();
}

default CTFtnEdn getNextCtFtnEdn(List<CTFtnEdn> footnotes) {
var id = BigInteger.valueOf(footnoteRef().getAndIncrement());
return footnotes.stream()
Expand Down Expand Up @@ -96,7 +131,7 @@ default CTFootnotes initFootnotes() {
CTFootnotes ctFootnotes = factory().createCTFootnotes();
List<CTFtnEdn> footnotes = ctFootnotes.getFootnote();

// <w:footnote w:id="0" w:type="separator">
// <w:footnote w:id="-1" w:type="separator">
// <w:p>
// <w:r>
// <w:separator/>
Expand All @@ -105,18 +140,32 @@ default CTFootnotes initFootnotes() {
// </w:footnote>
CTFtnEdn separator = getNextCtFtnEdn(footnotes);
separator.setType(STFtnEdn.SEPARATOR);
separator.getContent().add(p(r(factory().createRSeparator())));

// // <w:footnote w:id="1" w:type="continuationSeparator">
// // <w:p>
// // <w:r>
// // <w:continuationSeparator/>
// // </w:r>
// // </w:p>
// // </w:footnote>
// CTFtnEdn continuation = getNextCtFtnEdn(footnotes);
// continuation.setType(STFtnEdn.CONTINUATION_SEPARATOR);
// continuation.getContent().add(p(r(objectfactory().createRContinuationSeparator())));
separator.setId(BigInteger.valueOf(-1));
separator.getContent().add(
p(
r(
factory().createRSeparator()
)
)
);

// <w:footnote w:id="0" w:type="continuationSeparator">
// <w:p>
// <w:r>
// <w:continuationSeparator/>
// </w:r>
// </w:p>
// </w:footnote>
CTFtnEdn continuation = getNextCtFtnEdn(footnotes);
continuation.setType(STFtnEdn.CONTINUATION_SEPARATOR);
continuation.setId(BigInteger.ZERO);
continuation.getContent().add(
p(
r(
factory().createRContinuationSeparator()
)
)
);

return ctFootnotes;
}
Expand Down Expand Up @@ -153,11 +202,12 @@ default Object[] footnoteBody(List<P> footnoteParas) {
RPr rPr = rPr(r);
rPr.setRStyle(rStyle("FootnoteCharacters"));

objects.add(r);
objects.addAll(
footnoteParas.stream()
.peek(para -> para.setPPr(pPr)
).collect(Collectors.toList()));
List<P> paras = footnoteParas.stream()
.peek(para -> para.setPPr(pPr)
).collect(Collectors.toList());
objects.addAll(paras);
P firstP = paras.get(0);
firstP.getContent().add(0, r);
return objects.toArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,19 @@ private Style styleFootnote() {

// indent margins
PPrBase.Ind ind = factory.createPPrBaseInd();
ind.setLeft(BigInteger.valueOf(339));
ind.setRight(BigInteger.valueOf(339));
ind.setHanging(BigInteger.valueOf(339));
BigInteger margin = BigInteger.valueOf(400);
ind.setLeft(margin);
ind.setRight(margin);
ind.setHanging(margin);
pPr.setInd(ind);

// do not put space between paragraphs of the same style
PPrBase.Spacing spacing = factory.createPPrBaseSpacing();
spacing.setBefore(BigInteger.ZERO);
spacing.setAfter(BigInteger.valueOf(75));
pPr.setSpacing(spacing);
// pPr.setContextualSpacing(factory.createBooleanDefaultTrue());

style.setPPr(pPr);

// Font size 10pt (i.e. 20 /2)
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package net.kemitix.binder.docx.mdconvert;

import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Document;
import lombok.extern.java.Log;
import net.kemitix.binder.markdown.Context;
import net.kemitix.binder.markdown.DocumentModifier;
import net.kemitix.binder.markdown.MarkdownConverter;
import net.kemitix.binder.markdown.NodeHandler;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream;

@Log
Expand All @@ -18,14 +22,17 @@ public class MarkdownDocxConverter

private final Parser parser;
private final Instance<NodeHandler<Object>> nodeHandlers;
private final Instance<DocumentModifier> documentModifiers;

@Inject
public MarkdownDocxConverter(
Parser parser,
@Docx Instance<NodeHandler<Object>> nodeHandlers
@Docx Instance<NodeHandler<Object>> nodeHandlers,
Instance<DocumentModifier> documentModifiers
) {
this.parser = parser;
this.nodeHandlers = nodeHandlers;
this.documentModifiers = documentModifiers;
}

@Override
Expand All @@ -38,4 +45,15 @@ public Stream<NodeHandler<Object>> getNodeHandlers() {
return nodeHandlers.stream();
}

@Override
public Document fixUpDocument(
Document document,
Context context
) {
var docReference = new AtomicReference<>(document);
documentModifiers.stream()
.forEachOrdered(modifier ->
docReference.getAndUpdate(d -> modifier.apply(d, context)));
return docReference.get();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package net.kemitix.binder.docx.mdconvert;

import lombok.extern.java.Log;
import net.kemitix.binder.docx.DocxFacade;
import net.kemitix.binder.markdown.TextNodeHandler;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.stream.Stream;

@Log
@Docx
@ApplicationScoped
public class TextDocxNodeHandler
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.kemitix.binder.docx.mdconvert.footnote;

import net.kemitix.binder.spi.Footnote;
import org.docx4j.wml.P;
import org.docx4j.wml.R;

import java.util.List;
import java.util.stream.Stream;

public interface DocxFootnote
extends Footnote<P, R> {
static DocxFootnote createDocx(String ordinal, R placeholder, Stream<P> content) {
Footnote<P, R> footnote = Footnote.create(ordinal, placeholder, content);
return new DocxFootnote() {
@Override
public String getOrdinal() {
return footnote.getOrdinal();
}

@Override
public R getPlaceholder() {
return footnote.getPlaceholder();
}

@Override
public List<P> getContent() {
return footnote.getContent();
}
};
}
}
Loading