Last Updated: September 27, 2021
·
3.449K
· lordofthejars

Enjoy the magic of Asciidoctor in Java

The asciidoctor-java-integration is the official means of using Asciidoctor to render all your AsciiDoc documentation using Java instead of Ruby.

Installation

It is a simple jar and do not require any special treatment, simply add jar into classpath.

<dependencies>
  <dependency>
    <groupId>org.asciidoctor</groupId>
    <artifactId>asciidoctor-java-integration</artifactId>
    <version>${asciidoctor.version}</version>                   
  </dependency>
</dependencies>

Usage

and now you are ready to render asciidoc documents to any backend supported by Asciidoctor.

For example you can render from a String formatted in AsciiDoc to a String in HTML.

import static org.asciidoctor.Asciidoctor.Factory.create;
import org.asciidoctor.Asciidoctor;

Asciidoctor asciidoctor = create();

String rendered = asciidoctor.render("*This* is it.",  Collections.EMPTY_MAP);
System.out.println(rendered);

Or also from a File.

String rendered = asciidoctor.renderFile("docs/sample.adoc", Collections.EMPTY_MAP);
System.out.println(rendered);

Finally you can set the options and attributes supported by Asciidoctor:

import static org.asciidoctor.AttributesBuilder.attributes;
import static org.asciidoctor.OptionsBuilder.options;

//docs/sample.xml file is generated by using next options
Map<String, Object> attributes = attributes().backend("docbook").asMap();
Map<String, Object> options = options().inPlace(true).attributes(attributes).asMap();

asciidoctor.renderFile("docs/sample.adoc", options);

Now you do not have excuse to no use Asciidoctor because it is not supported in Java.

Also take a look at:

We keep learning,
Alex.