Search in sources :

Example 6 with Heading

use of com.vladsch.flexmark.ast.Heading in project flexmark-java by vsch.

the class TocNodeRenderer method render.

private void render(TocBlock node, NodeRendererContext context, HtmlWriter html) {
    HeadingCollectingVisitor visitor = new HeadingCollectingVisitor();
    List<Heading> headings = visitor.collectAndGetHeadings(node.getDocument());
    if (headings != null) {
        TocOptionsParser optionsParser = new TocOptionsParser();
        TocOptions options = optionsParser.parseOption(node.getStyle(), this.options.withTitle(""), null).getFirst();
        renderTocHeaders(context, html, node, headings, options);
    }
}
Also used : Heading(com.vladsch.flexmark.ast.Heading) HeadingCollectingVisitor(com.vladsch.flexmark.ast.util.HeadingCollectingVisitor)

Example 7 with Heading

use of com.vladsch.flexmark.ast.Heading in project flexmark-java by vsch.

the class TocUtils method renderHtmlToc.

public static void renderHtmlToc(HtmlWriter html, BasedSequence sourceText, List<Heading> headings, List<String> headingTexts, TocOptions tocOptions) {
    if (headings.size() > 0 && (sourceText.isNotNull() || !tocOptions.title.isEmpty())) {
        if (sourceText.isNotNull())
            html.srcPos(sourceText);
        html.withAttr(TOC_CONTENT).tag("div").line().indent();
        html.tag("h" + tocOptions.titleLevel).text(tocOptions.title).tag("/h" + tocOptions.titleLevel).line();
    }
    int initLevel = -1;
    int lastLevel = -1;
    String listOpen = tocOptions.isNumbered ? "ol" : "ul";
    String listClose = "/" + listOpen;
    int listNesting = 0;
    boolean[] openedItems = new boolean[7];
    boolean[] openedList = new boolean[7];
    int[] openedItemAppendCount = new int[7];
    for (int i = 0; i < headings.size(); i++) {
        Heading header = headings.get(i);
        String headerText = headingTexts.get(i);
        int headerLevel = tocOptions.listType != TocOptions.ListType.HIERARCHY ? 1 : header.getLevel();
        if (initLevel == -1) {
            initLevel = headerLevel;
            lastLevel = headerLevel;
            html.withAttr().line().tag(listOpen).indent().line();
            openedList[0] = true;
        }
        if (lastLevel < headerLevel) {
            for (int lv = lastLevel; lv < headerLevel; lv++) {
                openedItems[lv + 1] = false;
                openedList[lv + 1] = false;
            }
            if (!openedList[lastLevel]) {
                html.withAttr().indent().line().tag(listOpen).indent();
                openedList[lastLevel] = true;
            }
        } else if (lastLevel == headerLevel) {
            if (openedItems[lastLevel]) {
                if (openedList[lastLevel])
                    html.unIndent().tag(listClose).line();
                html.lineIf(openedItemAppendCount[lastLevel] != html.getModCount()).tag("/li").line();
            }
            openedItems[lastLevel] = false;
            openedList[lastLevel] = false;
        } else {
            // lastLevel > headerLevel
            for (int lv = lastLevel; lv >= headerLevel; lv--) {
                if (openedItems[lv]) {
                    if (openedList[lv])
                        html.unIndent().tag(listClose).unIndent().line();
                    html.lineIf(openedItemAppendCount[lastLevel] != html.getModCount()).tag("/li").line();
                }
                openedItems[lv] = false;
                openedList[lv] = false;
            }
        }
        html.line().tag("li");
        openedItems[headerLevel] = true;
        String headerId = header.getAnchorRefId();
        if (headerId == null || headerId.isEmpty()) {
            html.raw(headerText);
        } else {
            html.attr("href", "#" + headerId).withAttr().tag("a");
            html.raw(headerText);
            html.tag("/a");
        }
        lastLevel = headerLevel;
        openedItemAppendCount[headerLevel] = html.getModCount();
    }
    for (int i = lastLevel; i >= 1; i--) {
        if (openedItems[i]) {
            if (openedList[i])
                html.unIndent().tag(listClose).unIndent().line();
            html.lineIf(openedItemAppendCount[lastLevel] != html.getModCount()).tag("/li").line();
        }
    }
    // close original list
    if (openedList[0])
        html.unIndent().tag(listClose).line();
    if (headings.size() > 0 && (sourceText.isNotNull() || !tocOptions.title.isEmpty())) {
        html.line().unIndent().tag("/div");
    }
    html.line();
}
Also used : Heading(com.vladsch.flexmark.ast.Heading)

Example 8 with Heading

use of com.vladsch.flexmark.ast.Heading in project flexmark-java by vsch.

the class SimTocNodeRenderer method render.

private void render(SimTocBlock node, NodeRendererContext context, HtmlWriter html) {
    HeadingCollectingVisitor visitor = new HeadingCollectingVisitor();
    List<Heading> headings = visitor.collectAndGetHeadings(node.getDocument());
    if (headings != null) {
        SimTocOptionsParser optionsParser = new SimTocOptionsParser();
        TocOptions options = optionsParser.parseOption(node.getStyle(), this.options, null).getFirst();
        if (node.getTitle().isNotNull()) {
            options = options.withTitle(node.getTitle().unescape());
        }
        renderTocHeaders(context, html, node, headings, options);
    }
}
Also used : Heading(com.vladsch.flexmark.ast.Heading) HeadingCollectingVisitor(com.vladsch.flexmark.ast.util.HeadingCollectingVisitor)

Aggregations

Heading (com.vladsch.flexmark.ast.Heading)8 Node (com.vladsch.flexmark.ast.Node)2 HeadingCollectingVisitor (com.vladsch.flexmark.ast.util.HeadingCollectingVisitor)2 TextCollectingVisitor (com.vladsch.flexmark.ast.util.TextCollectingVisitor)2 Block (com.vladsch.flexmark.ast.Block)1 AnchorLink (com.vladsch.flexmark.ext.anchorlink.AnchorLink)1 TextCollectingAppendable (com.vladsch.flexmark.html.renderer.TextCollectingAppendable)1 Computable (com.vladsch.flexmark.util.Computable)1 ValueRunnable (com.vladsch.flexmark.util.ValueRunnable)1