AsciidoctorJ/BlockMacroProcessor: how to create image block

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

AsciidoctorJ/BlockMacroProcessor: how to create image block

herbh
Hi everybody,

I am currently experimenting with Asciidoctor(J) and it is so much fun to work with it so far.

Now I'm getting into trouble while writing an extension based on AsciidoctorJ/BlockMacroProcessor. This processor generates dynamically an image based on the target and should return an image block but I get an error.

I do following:

@Override
protected Object process(AbstractBlock parent, String target, Map<String, Object> attributes)
{
    // generate image based on target
    // ...
    
    // return image block
    final HashMap<Object, Object> options = new HashMap<Object, Object>();
    options.put("target", "myimage.png");
    options.put("alt", target);
    options.put("title", target);
    options.put("link", "http://example.com");
    
    return createBlock(parent, "image", target, attributes, options);
}

I get following error:
org.jruby.exceptions.RaiseException: (TypeError) asciidoctor: FAILED: <stdin>: Failed to parse source, can't convert nil into String
	at RUBY.load(C:/Users/herbh/.gradle/caches/modules-2/files-2.1/org.asciidoctor/asciidoctorj/1.5.2/39d33f739ec1c46f6e908a725264eb74b23c9f99/asciidoctorj-1.5.2.jar!/gems/asciidoctor-1.5.2/lib/asciidoctor.rb:1362)
	at RUBY.convert(C:/Users/herbh/.gradle/caches/modules-2/files-2.1/org.asciidoctor/asciidoctorj/1.5.2/39d33f739ec1c46f6e908a725264eb74b23c9f99/asciidoctorj-1.5.2.jar!/gems/asciidoctor-1.5.2/lib/asciidoctor.rb:1458)
	at RUBY.convert(<script>:72)
	at org.jruby.gen.InterfaceImpl741224867.convert(org/jruby/gen/InterfaceImpl741224867.gen:13)

Are my options wrong? I'm not a Ruby programmer but after taking a look at the BlockImageTemplate-RDoc I assume that my options map is filled correctly, isn't it?

Where can I find the (required) options and attributes and their meaning and possible values for createInline and createBlock methods of AsciidoctorJ Processors?

Thank you very much for this great piece of software!
Reply | Threaded
Open this post in threaded view
|

Re: AsciidoctorJ/BlockMacroProcessor: how to create image block

mojavelinux
Administrator
Heribert,

I'm not sure if what I'm going to suggest will fix the error, but hopefully it should get you closer to what you are trying to accomplish.

When creating an image, the third argument can be blank since an image doesn't have content.

----
return createBlock(parent, "image", "", attributes, options);
----

You also have attributes and options mixed up. Likely, you don't need options. They are for special processing on a block. Most of the time, you just need attributes. Instead, you would do:

----
final HashMap<String, Object> attrs = new HashMap<String, Object>();
attrs.put("target", "myimage.png");
attrs.put("alt", target);
attrs.put("title", target);
attrs.put("link", "http://example.com");
return createBlock(parent, "image", "", attrs, new HashMap<Object, Object>());
----

Unfortunately, the exception that you are getting is not helpful because the real error is being swallowed. This is an issue we are addressing in 1.5.3.

What might be helpful is if we had a testing in AsciidoctorJ that creates an image block in an extension. That way, we not only have an example but we are testing to make sure that creating an image block works. See this directory: https://github.com/asciidoctor/asciidoctorj/tree/master/asciidoctorj-core/src/test/java/org/asciidoctor/extension

Also note that the following AsciidoctorJ extension creates dynamic images (though the code is in Groovy). It should give you some ideas: https://github.com/fix/asciidoctor-screenshot/blob/master/buildSrc/src/main/groovy/org/asciidoctor/extension/ScreenshotBlock.groovy

Cheers,

-Dan

On Fri, Apr 24, 2015 at 3:53 AM, herbh [via Asciidoctor :: Discussion] <[hidden email]> wrote:
Hi everybody,

I am currently experimenting with Asciidoctor(J) and it is so much fun to work with it so far.

Now I'm getting into trouble while writing an extension based on AsciidoctorJ/BlockMacroProcessor. This processor generates dynamically an image based on the target and should return an image block but I get an error.

I do following:

@Override
protected Object process(AbstractBlock parent, String target, Map<String, Object> attributes)
{
    // generate image based on target
    // ...
    
    // return image block
    final HashMap<Object, Object> options = new HashMap<Object, Object>();
    options.put("target", "myimage.png");
    options.put("alt", target);
    options.put("title", target);
    options.put("link", "http://example.com");
    
    return createBlock(parent, "image", target, attributes, options);
}

I get following error:
org.jruby.exceptions.RaiseException: (TypeError) asciidoctor: FAILED: <stdin>: Failed to parse source, can't convert nil into String
	at RUBY.load(C:/Users/herbh/.gradle/caches/modules-2/files-2.1/org.asciidoctor/asciidoctorj/1.5.2/39d33f739ec1c46f6e908a725264eb74b23c9f99/asciidoctorj-1.5.2.jar!/gems/asciidoctor-1.5.2/lib/asciidoctor.rb:1362)
	at RUBY.convert(C:/Users/herbh/.gradle/caches/modules-2/files-2.1/org.asciidoctor/asciidoctorj/1.5.2/39d33f739ec1c46f6e908a725264eb74b23c9f99/asciidoctorj-1.5.2.jar!/gems/asciidoctor-1.5.2/lib/asciidoctor.rb:1458)
	at RUBY.convert(<script>:72)
	at org.jruby.gen.InterfaceImpl741224867.convert(org/jruby/gen/InterfaceImpl741224867.gen:13)

Are my options wrong? I'm not a Ruby programmer but after taking a look at the BlockImageTemplate-RDoc I assume that my options map is filled correctly, isn't it?

Where can I find the (required) options and attributes and their meaning and possible values for createInline and createBlock methods of AsciidoctorJ Processors?

Thank you very much for this great piece of software!


If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/AsciidoctorJ-BlockMacroProcessor-how-to-create-image-block-tp3025.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML



--