Search in sources :

Example 1 with Footnote

use of com.vladsch.flexmark.ext.footnotes.Footnote 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 Footnote

use of com.vladsch.flexmark.ext.footnotes.Footnote 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)

Aggregations

Footnote (com.vladsch.flexmark.ext.footnotes.Footnote)2 FootnoteBlock (com.vladsch.flexmark.ext.footnotes.FootnoteBlock)2 BasedSequence (com.vladsch.flexmark.util.sequence.BasedSequence)1