hr.fer.tel.markdown.PlantUmlRendererOptions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of markdown-to-html Show documentation
Show all versions of markdown-to-html Show documentation
Library for converting markdown with code and plantuml to local html page.
package hr.fer.tel.markdown;
import java.util.HashSet;
import java.util.Set;
import com.credibledoc.plantuml.svggenerator.SvgGeneratorService;
import com.vladsch.flexmark.ast.FencedCodeBlock;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.html.HtmlWriter;
import com.vladsch.flexmark.html.renderer.DelegatingNodeRendererFactory;
import com.vladsch.flexmark.html.renderer.NodeRenderer;
import com.vladsch.flexmark.html.renderer.NodeRendererContext;
import com.vladsch.flexmark.html.renderer.NodeRenderingHandler;
import com.vladsch.flexmark.html.renderer.NodeRenderingHandler.CustomNodeRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.profile.pegdown.Extensions;
import com.vladsch.flexmark.profile.pegdown.PegdownOptionsAdapter;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.data.DataHolder;
import com.vladsch.flexmark.util.data.MutableDataHolder;
public class PlantUmlRendererOptions {
final private static DataHolder OPTIONS = PegdownOptionsAdapter.flexmarkOptions(Extensions.ALL,
PlantUmlExtension.create());
static final Parser PARSER = Parser.builder(OPTIONS).build();
static final HtmlRenderer RENDERER = HtmlRenderer.builder(OPTIONS).build();
static class PlantUmlExtension implements HtmlRenderer.HtmlRendererExtension {
@Override
public void rendererOptions(MutableDataHolder options) {
}
@Override
public void extend(HtmlRenderer.Builder htmlRendererBuilder, String rendererType) {
htmlRendererBuilder.nodeRendererFactory(new PlantUmlRenderer.Factory());
}
static PlantUmlExtension create() {
return new PlantUmlExtension();
}
}
static class PlantUmlRenderer implements NodeRenderer {
public static class Factory implements DelegatingNodeRendererFactory {
@Override
public NodeRenderer apply(DataHolder options) {
return new PlantUmlRenderer();
}
@Override
public Set> getDelegates() {
/// Set>();
// add node renderer factory classes to which this renderer will delegate some
/// of its rendering
// core node renderer is assumed to have all depend it so there is no need to
/// add it
// set.add(WikiLinkNodeRenderer.Factory.class);
// return set;
// return null if renderer does not delegate or delegates only to core node
// renderer
return null;
}
}
;
@Override
public Set> getNodeRenderingHandlers() {
HashSet> set = new HashSet<>();
set.add(new NodeRenderingHandler<>(FencedCodeBlock.class, new CustomNodeRenderer() {
@Override
public void render(FencedCodeBlock node, NodeRendererContext context, HtmlWriter html) {
// test the node to see if it needs overriding
if (node.getInfo().equals("plantuml")) {
//System.out.println("plantuml: " + node.getContentChars().normalizeEOL());
String svg = SvgGeneratorService.getInstance().generateSvgFromPlantUml(node.getContentChars().normalizeEOL());
html.append(svg);
} else {
context.delegateRender();
}
}
}));
return set;
}
}
// use the PARSER to parse and RENDERER to render with pegdown compatibility
public static void main(String[] args) {
// You can re-use parser and renderer instances
Node document = PARSER.parse("""
```plantuml
@startuml
Bob -> Alice : hello
Alice -> Bob : hi
@enduml
```
""");
String html = RENDERER.render(document); // "\n"
System.out.println(html);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy