use of org.commonmark.node.AbstractVisitor in project hippo by NHS-digital-website.
the class CommonmarkMarkdownConverter method shiftHeadingsLevels.
private void shiftHeadingsLevels(final Node document, final int headingsLevelsOffset) {
final AbstractVisitor headingsLevelsShifter = new AbstractVisitor() {
@Override
public void visit(final Heading heading) {
heading.setLevel(heading.getLevel() + headingsLevelsOffset);
visitChildren(heading);
}
};
document.accept(headingsLevelsShifter);
}
use of org.commonmark.node.AbstractVisitor in project PocketHub by pockethub.
the class MarkwonUtils method createMarkwon.
public static Markwon createMarkwon(Context context, String baseUrl) {
final Prism4j prism4j = new Prism4j(new GrammarLocatorDef());
return Markwon.builder(context).usePlugin(StrikethroughPlugin.create()).usePlugin(TaskListPlugin.create(context)).usePlugin(HtmlPlugin.create()).usePlugin(new AbstractMarkwonPlugin() {
@Override
public void configureVisitor(@NonNull MarkwonVisitor.Builder builder) {
builder.on(FencedCodeBlock.class, (visitor, fencedCodeBlock) -> {
// We actually won't be applying code spans here, as our custom view will
// draw background and apply mono typeface
//
// NB the `trim` operation on literal (as code will have a new line at the end)
final CharSequence code = visitor.configuration().syntaxHighlight().highlight(fencedCodeBlock.getInfo(), fencedCodeBlock.getLiteral().trim());
visitor.builder().append(code);
});
}
@Override
public void configureParser(@NonNull Parser.Builder builder) {
super.configureParser(builder);
builder.postProcessor(new PostProcessor() {
@Override
public Node process(Node node) {
Visitor t = new AbstractVisitor() {
@Override
public void visit(HtmlBlock htmlBlock) {
String literal = htmlBlock.getLiteral();
if (literal.startsWith("<!--")) {
htmlBlock.unlink();
} else {
super.visit(htmlBlock);
}
}
};
node.accept(t);
return node;
}
});
}
}).usePlugin(GlideImagesPlugin.create(new GifAwareGlideStore(context))).usePlugin(new SpanLinkPlugin(baseUrl)).usePlugin(new AbstractMarkwonPlugin() {
@Override
public void configure(@NonNull Registry registry) {
registry.require(HtmlPlugin.class, htmlPlugin -> htmlPlugin.addHandler(new AlignHandler()));
}
}).usePlugin(TableEntryPlugin.create(TablePlugin.create(context))).usePlugin(SyntaxHighlightPlugin.create(prism4j, Prism4jThemeDefault.create())).usePlugin(new AsyncDrawableSchedulerPlugin()).build();
}
use of org.commonmark.node.AbstractVisitor in project hippo by NHS-digital-website.
the class CommonmarkMarkdownConverter method addBreaksBeforeH2Headings.
private void addBreaksBeforeH2Headings(final Node document) {
final AtomicBoolean isSubsequentH2Heading = new AtomicBoolean(false);
final AbstractVisitor horizontalLineAdder = new AbstractVisitor() {
@Override
public void visit(final Heading heading) {
if (heading.getLevel() == 2) {
if (isSubsequentH2Heading.get()) {
heading.insertBefore(new ThematicBreak());
} else {
isSubsequentH2Heading.set(true);
}
}
visitChildren(heading);
}
};
document.accept(horizontalLineAdder);
}
use of org.commonmark.node.AbstractVisitor in project hippo by NHS-digital-website.
the class CommonmarkMarkdownConverter method findTopHeadingLevel.
private int findTopHeadingLevel(final Node document) {
final AtomicInteger topHeadingLevel = new AtomicInteger();
final AbstractVisitor topHeadingLevelFinder = new AbstractVisitor() {
@Override
public void visit(final Heading heading) {
if (topHeadingLevel.get() == 0 || heading.getLevel() < topHeadingLevel.get()) {
topHeadingLevel.set(heading.getLevel());
}
visitChildren(heading);
}
};
document.accept(topHeadingLevelFinder);
return topHeadingLevel.get();
}
Aggregations