Search in sources :

Example 1 with FootnoteBlock

use of com.vladsch.flexmark.ext.footnotes.FootnoteBlock in project flexmark-java by vsch.

the class FootnoteLinkRefProcessor method createNode.

@Override
public Node createNode(BasedSequence nodeChars) {
    BasedSequence footnoteId = nodeChars.midSequence(2, -1).trim();
    FootnoteBlock footnoteBlock = footnoteId.length() > 0 ? footnoteRepository.get(footnoteId.toString()) : null;
    Footnote footnote = new Footnote(nodeChars.subSequence(0, 2), footnoteId, nodeChars.endSequence(1));
    footnote.setFootnoteBlock(footnoteBlock);
    if (footnoteBlock != null) {
        footnoteRepository.addFootnoteReference(footnoteBlock, footnote);
    }
    return footnote;
}
Also used : Footnote(com.vladsch.flexmark.ext.footnotes.Footnote) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) FootnoteBlock(com.vladsch.flexmark.ext.footnotes.FootnoteBlock)

Example 2 with FootnoteBlock

use of com.vladsch.flexmark.ext.footnotes.FootnoteBlock in project flexmark-java by vsch.

the class FootnoteNodeRenderer method render.

private void render(Footnote node, NodeRendererContext context, final HtmlWriter html) {
    FootnoteBlock footnoteBlock = node.getFootnoteBlock();
    if (footnoteBlock == null) {
        // just text
        html.raw("[^");
        context.renderChildren(node);
        html.raw("]");
    } else {
        final int footnoteOrdinal = footnoteBlock.getFootnoteOrdinal();
        html.attr("id", "fnref-" + footnoteOrdinal);
        html.srcPos(node.getChars()).withAttr().tag("sup", false, false, new Runnable() {

            @Override
            public void run() {
                if (!options.footnoteLinkRefClass.isEmpty())
                    html.attr("class", options.footnoteLinkRefClass);
                html.attr("href", "#fn-" + footnoteOrdinal);
                html.withAttr().tag("a");
                html.raw(options.footnoteRefPrefix + String.valueOf(footnoteOrdinal) + options.footnoteRefSuffix);
                html.tag("/a");
            }
        });
    }
}
Also used : FootnoteBlock(com.vladsch.flexmark.ext.footnotes.FootnoteBlock)

Example 3 with FootnoteBlock

use of com.vladsch.flexmark.ext.footnotes.FootnoteBlock in project flexmark-java by vsch.

the class FootnoteNodeRenderer method renderDocument.

@Override
public void renderDocument(final NodeRendererContext context, final HtmlWriter html, Document document, RenderingPhase phase) {
    if (phase == RenderingPhase.BODY_TOP) {
        if (recheckUndefinedReferences) {
            // need to see if have undefined footnotes that were defined after parsing
            final boolean[] hadNewFootnotes = { false };
            NodeVisitor visitor = new NodeVisitor(new VisitHandler<Footnote>(Footnote.class, new Visitor<Footnote>() {

                @Override
                public void visit(Footnote node) {
                    if (!node.isDefined()) {
                        FootnoteBlock footonoteBlock = node.getFootnoteBlock(footnoteRepository);
                        if (footonoteBlock != null) {
                            footnoteRepository.addFootnoteReference(footonoteBlock, node);
                            node.setFootnoteBlock(footonoteBlock);
                            hadNewFootnotes[0] = true;
                        }
                    }
                }
            }));
            visitor.visit(document);
            if (hadNewFootnotes[0]) {
                this.footnoteRepository.resolveFootnoteOrdinals();
            }
        }
    }
    if (phase == RenderingPhase.BODY_BOTTOM) {
        // here we dump the footnote blocks that were referenced in the document body, ie. ones with footnoteOrdinal > 0
        if (footnoteRepository.getReferencedFootnoteBlocks().size() > 0) {
            html.attr("class", "footnotes").withAttr().tagIndent("div", new Runnable() {

                @Override
                public void run() {
                    html.tagVoidLine("hr");
                    html.tagIndent("ol", new Runnable() {

                        @Override
                        public void run() {
                            for (final FootnoteBlock footnoteBlock : footnoteRepository.getReferencedFootnoteBlocks()) {
                                final int footnoteOrdinal = footnoteBlock.getFootnoteOrdinal();
                                html.attr("id", "fn-" + footnoteOrdinal);
                                html.withAttr().tagIndent("li", new Runnable() {

                                    @Override
                                    public void run() {
                                        context.renderChildren(footnoteBlock);
                                        html.attr("href", "#fnref-" + footnoteOrdinal);
                                        if (!options.footnoteBackLinkRefClass.isEmpty())
                                            html.attr("class", options.footnoteBackLinkRefClass);
                                        html.withAttr().tag("a");
                                        html.raw(options.footnoteBackRefString);
                                        html.tag("/a");
                                    }
                                });
                            }
                        }
                    });
                }
            });
        }
    }
}
Also used : Footnote(com.vladsch.flexmark.ext.footnotes.Footnote) FootnoteBlock(com.vladsch.flexmark.ext.footnotes.FootnoteBlock)

Example 4 with FootnoteBlock

use of com.vladsch.flexmark.ext.footnotes.FootnoteBlock in project flexmark-java by vsch.

the class FootnoteRepository method resolveFootnoteOrdinals.

public void resolveFootnoteOrdinals() {
    // need to sort by first referenced offset then set each to its ordinal position in the array+1
    Collections.sort(referencedFootnoteBlocks, new Comparator<FootnoteBlock>() {

        @Override
        public int compare(FootnoteBlock f1, FootnoteBlock f2) {
            return f1.getFirstReferenceOffset() - f2.getFirstReferenceOffset();
        }
    });
    int ordinal = 0;
    for (FootnoteBlock footnoteBlock : referencedFootnoteBlocks) {
        footnoteBlock.setFootnoteOrdinal(++ordinal);
    }
}
Also used : FootnoteBlock(com.vladsch.flexmark.ext.footnotes.FootnoteBlock)

Example 5 with FootnoteBlock

use of com.vladsch.flexmark.ext.footnotes.FootnoteBlock in project flexmark-java by vsch.

the class CoreNodeDocxRenderer method render.

private void render(Footnote node, final DocxRendererContext docx) {
    final FootnoteBlock footnoteBlock = node.getFootnoteBlock();
    if (footnoteBlock == null) {
        // just text
        final org.docx4j.wml.Text text = docx.addWrappedText();
        text.setValue("[^");
        docx.renderChildren(node);
        final org.docx4j.wml.Text text1 = docx.addWrappedText();
        text1.setValue("]");
    } else {
        try {
            BigInteger footnoteId = footnoteIDs.containsKey(footnoteBlock) ? footnoteIDs.get(footnoteBlock) : BigInteger.ZERO;
            final CTFtnEdn ftnEdn = docx.addFootnote(footnoteId);
            final BigInteger ftnEdnId = ftnEdn.getId();
            if (ftnEdnId.compareTo(footnoteId) != 0) {
                // Word does not like re-using footnotes, so we create a new one for every reference
                // footnoteIDs.put(footnoteBlock, ftnEdnId);
                docx.contextFramed(new Runnable() {

                    @Override
                    public void run() {
                        docx.setBlockFormatProvider(new FootnoteBlockFormatProvider<Node>(docx));
                        docx.setRunFormatProvider(new FootnoteRunFormatProvider<Node>(docx));
                        docx.setContentContainer(new ContentContainer() {

                            @Override
                            public RelationshipsPart getRelationshipsPart() {
                                try {
                                    return docx.getFootnotesPart().getRelationshipsPart();
                                } catch (Docx4JException e) {
                                    e.printStackTrace();
                                    return docx.getDocxDocument().getRelationshipsPart();
                                }
                            }

                            @Override
                            public Part getContainerPart() {
                                try {
                                    return docx.getFootnotesPart();
                                } catch (Docx4JException e) {
                                    e.printStackTrace();
                                    return docx.getDocxDocument();
                                }
                            }

                            @Override
                            public List<Object> getContent() {
                                return ftnEdn.getContent();
                            }

                            @Override
                            public Object getLastContentElement() {
                                final List<Object> content = getContent();
                                return content != null && content.size() > 0 ? content.get(content.size() - 1) : null;
                            }

                            @Override
                            public void addContentElement(final Object element) {
                                getContent().add(element);
                            }
                        });
                        docx.renderChildren(footnoteBlock);
                    }
                });
            }
        } catch (Docx4JException e) {
            e.printStackTrace();
        }
    }
}
Also used : Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException) org.docx4j.wml(org.docx4j.wml) BigInteger(java.math.BigInteger) FootnoteBlock(com.vladsch.flexmark.ext.footnotes.FootnoteBlock)

Aggregations

FootnoteBlock (com.vladsch.flexmark.ext.footnotes.FootnoteBlock)5 Footnote (com.vladsch.flexmark.ext.footnotes.Footnote)2 BasedSequence (com.vladsch.flexmark.util.sequence.BasedSequence)1 BigInteger (java.math.BigInteger)1 Docx4JException (org.docx4j.openpackaging.exceptions.Docx4JException)1 org.docx4j.wml (org.docx4j.wml)1