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);
}
}
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();
}
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);
}
}
Aggregations