Automatically Load a Syntax Highlighter

In previous examples, the syntax highlighters were registered manually. However, AsciidoctorJ provides another way to register syntax highlighters. If any implementation of the SPI interface is present on the classpath, it will be executed.

To create an autoloadable extension you should do the next steps:

Create a class that implements org.asciidoctor.jruby.syntaxhighlighter.spi.SyntaxHighlighterRegistry.

org.asciidoctor.integrationguide.syntaxhighlighter.HighlightJsExtension.java
import org.asciidoctor.jruby.syntaxhighlighter.spi.SyntaxHighlighterRegistry;

public class HighlightJsExtension implements SyntaxHighlighterRegistry { (1)
    @Override
    public void register(Asciidoctor asciidoctor) { (2)
        asciidoctor.syntaxHighlighterRegistry()     (3)
            .register(HighlightJsHighlighter.class, "autoloadedHighlightJs");
    }
}
1 To autoload extensions you need to implement SyntaxHighlighterRegistry.
2 AsciidoctorJ will automatically run the register method. The method is responsible for registering all extensions.
3 All required syntax highlighters are registered.

Next, you need to create a file called org.asciidoctor.jruby.syntaxhighlighter.spi.SyntaxHighlighterRegistry inside META-INF/services with the implementation’s full qualified name.

META-INF/services/org.asciidoctor.jruby.syntaxhighlighter.spi.SyntaxHighlighterRegistry
org.asciidoctor.integrationguide.syntaxhighlighter.HighlightJsExtension

And that’s all. Now when a .jar file containing the previous structure is dropped into the classpath of AsciidoctorJ, the register method will be executed automatically and the extensions will be registered.

If you have installed AsciidoctorJ as CLI, the asciidoctorj command will be on the path, and you can use:

$ asciidoctorj -cp=lib/myextension.jar test.adoc

If you have downloaded the distribution jars only, use a command like:

$ java -cp lib/jruby-complete-{jruby-version}.jar;lib/asciidoctorj-cli-{artifact-version}.jar;lib/asciidoctorj-api-{artifact-version}.jar;lib/asciidoctorj-{artifact-version}.jar;lib/jcommander-{jcommander-version}.jar;lib/myextension.jar org.asciidoctor.cli.jruby.AsciidoctorInvoker test.adoc