private void printAttributes(StringBuilder buf, Node node) {
if (node instanceof Text)
printAttribute(buf, "literal", ((Text)node).getLiteral());
else if (node instanceof Code)
printAttribute(buf, "literal", ((Code)node).getLiteral());
else if (node instanceof IndentedCodeBlock)
printAttribute(buf, "literal", ((IndentedCodeBlock)node).getLiteral());
else if (node instanceof FencedCodeBlock)
printAttribute(buf, "literal", ((FencedCodeBlock)node).getLiteral());
else if (node instanceof HtmlBlock)
printAttribute(buf, "literal", ((HtmlBlock)node).getLiteral());
else if (node instanceof HtmlInline)
printAttribute(buf, "literal", ((HtmlInline)node).getLiteral());
else if (node instanceof Link) {
printAttribute(buf, "destination", ((Link)node).getDestination());
printAttribute(buf, "title", ((Link)node).getTitle());
} else if (node instanceof Image) {
printAttribute(buf, "destination", ((Image)node).getDestination());
printAttribute(buf, "title", ((Image)node).getTitle());
} else if (node instanceof Heading)
printAttribute(buf, "level", ((Heading)node).getLevel());
}
private void addMenu(Node document, List<MenuItem> menuItems) {
final List<MenuItem> reversedItems = new LinkedList<>(menuItems);
Collections.reverse(reversedItems);
for (final MenuItem menuItem : reversedItems) {
if (menuItem.level > 5) {
continue;
}
final Link link = new Link();
link.setTitle(menuItem.title);
final Text text = new Text();
text.setLiteral( menuItem.title);
link.appendChild(text);
link.setDestination("#" + menuItem.anchorId);
final HtmlInline indent = new HtmlInline();
final String cssClass = "menuItem" + menuItem.level;
indent.setLiteral("<a href=\"#" + menuItem.anchorId + "\" class=\"" + cssClass + "\">" + menuItem.title + "</a>");
document.prependChild(indent);
document.prependChild(new HardLineBreak());
}
}
@Override
protected Node parse() {
String m = match(HTML_TAG);
if (m != null) {
HtmlInline node = new HtmlInline();
node.setLiteral(m);
return node;
} else {
return null;
}
}
private List<MenuItem> addAnchorAndNumberingToHeaders(Node node) {
final List<MenuItem> menuItems = new LinkedList<>();
final int[] counters = new int[10];
node.accept(new AbstractVisitor() {
@Override
public void visit(Heading heading) {
final Text text = (Text) heading.getFirstChild();
final String content = text.getLiteral();
final boolean intro = "Introduction".equals(content); // Do not number Introduction
if (!intro) {
counters[heading.getLevel()]++;
for (int i = heading.getLevel() + 1; i < 6; i++) {
counters[i] = 0;
}
}
final StringBuilder sb = new StringBuilder();
for (int i = 1; i <= heading.getLevel(); i++) {
sb.append(counters[i]).append(".");
}
if (sb.length() > 1 && heading.getLevel() > 1) {
sb.delete(sb.length() - 1, sb.length() );
}
final String anchorId = content.replace(" ", "");
final HtmlInline htmlInline = new HtmlInline();
htmlInline.setLiteral("<a name=\"" + anchorId + "\"></a>");
heading.insertBefore(htmlInline);
final MenuItem menuItem = new MenuItem(content, anchorId, heading.getLevel());
menuItems.add(menuItem);
}
});
return menuItems;
}
@Override
public void visit(HtmlInline htmlInline) {
visit((Node) htmlInline);
}