'Create image block with AsciidocJ

I want to create a BlockProcessor which transforms an open block into an image.

The following class is a simplified version of my actual processor:

@Name("foo")                                              
@Contexts({Contexts.OPEN})                            
@ContentModel(ContentModel.SIMPLE)
public class FooBlockProcessor extends BlockProcessor {

    @Override
    public Object process(StructuralNode parent, Reader reader, Map<String, Object> attributes) {
        var options = new HashMap<Object, Object>();
        options.put("source", null);
        options.put("target", "C:/Temp/PNG_transparency_demonstration_1.png");
        Block block = this.createBlock(parent, "image", options);
        return block;
    }
}

But after it returned it's block, I get the following exception:

186270 [JavaFX Application Thread] ERROR com.kodedu.boot.AppStarter - org.asciidoctor.jruby.internal.AsciidoctorCoreException: org.jruby.exceptions.NoMethodError: (NoMethodError) undefined method `start_with?' for nil:NilClass 
java.lang.RuntimeException: org.asciidoctor.jruby.internal.AsciidoctorCoreException: org.jruby.exceptions.NoMethodError: (NoMethodError) undefined method `start_with?' for nil:NilClass
    at com.kodedu.service.impl.ThreadServiceImpl.lambda$2(ThreadServiceImpl.java:89)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:447)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:446)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:831)
Caused by: org.asciidoctor.jruby.internal.AsciidoctorCoreException: org.jruby.exceptions.NoMethodError: (NoMethodError) undefined method `start_with?' for nil:NilClass
    at org.asciidoctor.jruby.internal.JRubyAsciidoctor.convert(JRubyAsciidoctor.java:320)
    at org.asciidoctor.jruby.internal.JRubyAsciidoctor.convert(JRubyAsciidoctor.java:336)
    at org.asciidoctor.jruby.internal.JRubyAsciidoctor.convert(JRubyAsciidoctor.java:331)
    at com.kodedu.service.convert.pdf.AsciidoctorPdfBookConverter.lambda$0(AsciidoctorPdfBookConverter.java:86)
    at com.kodedu.service.impl.ThreadServiceImpl.lambda$2(ThreadServiceImpl.java:85)
    ... 7 common frames omitted
Caused by: org.jruby.exceptions.NoMethodError: (NoMethodError) undefined method `start_with?' for nil:NilClass
    at RUBY.target_and_format(uri:classloader:/gems/asciidoctor-pdf-1.6.2/lib/asciidoctor/pdf/ext/asciidoctor/image.rb:28)
    at RUBY.convert_image(uri:classloader:/gems/asciidoctor-pdf-1.6.2/lib/asciidoctor/pdf/converter.rb:1395)
    at RUBY.convert(uri:classloader:/gems/asciidoctor-pdf-1.6.2/lib/asciidoctor/pdf/converter.rb:135)
    at RUBY.convert(uri:classloader:/gems/asciidoctor-2.0.17/lib/asciidoctor/abstract_block.rb:75)
    at RUBY.content(uri:classloader:/gems/asciidoctor-2.0.17/lib/asciidoctor/abstract_block.rb:84)
    at org.jruby.RubyArray.map(org/jruby/RubyArray.java:2589)
    at RUBY.content(uri:classloader:/gems/asciidoctor-2.0.17/lib/asciidoctor/abstract_block.rb:84)
    at RUBY.content(uri:classloader:/gems/asciidoctor-2.0.17/lib/asciidoctor/document.rb:1011)
    at RUBY.traverse(uri:classloader:/gems/asciidoctor-pdf-1.6.2/lib/asciidoctor/pdf/converter.rb:152)
    at RUBY.convert_document(uri:classloader:/gems/asciidoctor-pdf-1.6.2/lib/asciidoctor/pdf/converter.rb:261)
    at RUBY.indent_section(uri:classloader:/gems/asciidoctor-pdf-1.6.2/lib/asciidoctor/pdf/converter.rb:594)
    at RUBY.convert_document(uri:classloader:/gems/asciidoctor-pdf-1.6.2/lib/asciidoctor/pdf/converter.rb:208)
    at RUBY.convert(uri:classloader:/gems/asciidoctor-pdf-1.6.2/lib/asciidoctor/pdf/converter.rb:135)
    at RUBY.convert(uri:classloader:/gems/asciidoctor-2.0.17/lib/asciidoctor/document.rb:955)
    at RUBY.convert(uri:classloader:/gems/asciidoctor-2.0.17/lib/asciidoctor/convert.rb:118)

When I analyse the Block in the Eclipse-Debugger the rubyNode has the following value: #<Asciidoctor::Block@2000 {context: :image, content_model: :empty, style: nil, lines: 0}>

This seems quite empty, although I do not know how it should look like. Nevertheless I would expect to see the path to the image. So I assume, that I need to add some further values to options. But according to asciidoctor/extensions.rb create_image_block() it seems that only target is needed.



Solution 1:[1]

In an PlantUml example I checked image_attributes of diagram_processor.rb and I have seen that image_attributes it has the value {1=>"plantuml", 2=>nil, 3=>"png", "cloaked-context"=>:open}. Furthermore target is set after the Block was created in line 261.

Therefore if I change my code to the following, it works:

@Override
public Object process(StructuralNode parent, Reader reader, Map<String, Object> attributes) {
    Block block = this.createBlock(parent, "image", Collections.emptyMap());
    block.setAttribute("target", "C:/Temp/PNG_transparency_demonstration_1.png", true);
    return block;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gillesB