Search in sources :

Example 1 with TextCollectingAppendable

use of com.vladsch.flexmark.html.renderer.TextCollectingAppendable in project flexmark-java by vsch.

the class TocUtils method htmlHeadingTexts.

public static Paired<List<Heading>, List<String>> htmlHeadingTexts(NodeRendererContext context, List<Heading> headings, TocOptions tocOptions) {
    final List<String> headingContents = new ArrayList<String>(headings.size());
    final boolean isReversed = tocOptions.listType == TocOptions.ListType.SORTED_REVERSED || tocOptions.listType == TocOptions.ListType.FLAT_REVERSED;
    final boolean isSorted = tocOptions.listType == TocOptions.ListType.SORTED || tocOptions.listType == TocOptions.ListType.SORTED_REVERSED;
    final boolean needText = isReversed || isSorted;
    final HashMap<String, Heading> headingNodes = !needText ? null : new HashMap<String, Heading>(headings.size());
    final HashMap<String, String> headingTexts = !needText || tocOptions.isTextOnly ? null : new HashMap<String, String>(headings.size());
    for (Heading heading : headings) {
        String headingContent;
        // need to skip anchor links but render emphasis
        if (tocOptions.isTextOnly) {
            headingContent = getHeadingText(heading).toString();
        } else {
            TextCollectingAppendable out = getHeadingContent(context, heading);
            headingContent = out.getHtml();
            if (needText) {
                headingTexts.put(headingContent, getHeadingText(heading).toString());
            }
        }
        if (needText) {
            headingNodes.put(headingContent, heading);
        }
        headingContents.add(headingContent);
    }
    if (isSorted || isReversed) {
        if (tocOptions.isTextOnly) {
            if (isSorted) {
                Collections.sort(headingContents, new Comparator<String>() {

                    @Override
                    public int compare(String heading1, String heading2) {
                        return isReversed ? heading2.compareTo(heading1) : heading1.compareTo(heading2);
                    }
                });
            } else {
                Collections.reverse(headingContents);
            }
        } else {
            if (isSorted) {
                Collections.sort(headingContents, new Comparator<String>() {

                    @Override
                    public int compare(String heading1, String heading2) {
                        final String headingText1 = headingTexts.get(heading1);
                        final String headingText2 = headingTexts.get(heading2);
                        return isReversed ? headingText2.compareTo(headingText1) : headingText1.compareTo(headingText2);
                    }
                });
            } else {
                Collections.reverse(headingContents);
            }
        }
        headings = new ArrayList<Heading>();
        for (String headingContent : headingContents) {
            headings.add(headingNodes.get(headingContent));
        }
    }
    return Pair.of(headings, headingContents);
}
Also used : Heading(com.vladsch.flexmark.ast.Heading) TextCollectingAppendable(com.vladsch.flexmark.html.renderer.TextCollectingAppendable)

Example 2 with TextCollectingAppendable

use of com.vladsch.flexmark.html.renderer.TextCollectingAppendable in project flexmark-java by vsch.

the class TocUtils method getHeadingContent.

private static TextCollectingAppendable getHeadingContent(NodeRendererContext context, Heading header) {
    TextCollectingAppendable out = new TextCollectingAppendable();
    NodeRendererContext subContext = context.getSubContext(out, false);
    subContext.doNotRenderLinks();
    subContext.renderChildren(header);
    return out;
}
Also used : TextCollectingAppendable(com.vladsch.flexmark.html.renderer.TextCollectingAppendable) NodeRendererContext(com.vladsch.flexmark.html.renderer.NodeRendererContext)

Aggregations

TextCollectingAppendable (com.vladsch.flexmark.html.renderer.TextCollectingAppendable)2 Heading (com.vladsch.flexmark.ast.Heading)1 NodeRendererContext (com.vladsch.flexmark.html.renderer.NodeRendererContext)1