Create a link with a target blank attribute (in an asciidoctorj macro)

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

Create a link with a target blank attribute (in an asciidoctorj macro)

Jeremie Bresson
I need to create a link having a 'target="_blank"' attribute. This works great:
:linkattrs:

Hello link:http://eclipse.org[some text, window=\"_blank\"]

Now I do not understand how I can get the same inside my asciidoctor macro. I have tried several variant of:
  @Override
  protected Object process(AbstractBlock parent, String repository, Map<String, Object> attributes) {
    // Define options for an 'anchor' element:
    Map<String, Object> options = new HashMap<String, Object>();
    options.put("type", ":link");
    options.put("target", "http://eclipse.org");

    attributes.put("linkattrs", true);
    options.put("window", "_blank");

    // Create the 'anchor' node:
    Inline inline = createInline(parent, "anchor", "some text", attributes, options);

    // Convert to String value:
    return inline.convert();
  }
I think my problem is this linkattrs attribute.
Thank you in advance for your help.
Reply | Threaded
Open this post in threaded view
|

Re: Create a link with a target blank attribute (in an asciidoctorj macro)

mojavelinux
Administrator
Jérémie,

The correct way to create a link with a target="_blank" attribute in the process method of an Asciidoctor inline macro is as follows:

[source,java]
----
Map<String, Object> options = new HashMap<String, Object>();
options.put("type", ":link"); // <1>
options.put("target", target); // <2>
attributes.put("window", "_blank"); // <3>
Inline anchor = createInline(parent, "anchor", "link text", attributes, options); // <4>
return anchor.convert();
----
<1> Sets the anchor type to :link (as there are multiple types of anchors)
<2> Sets the URL (i.e., target) of the anchor, which is used as the value of the HTML href attribute
<3> Sets the target window, which is used as the value of the HTML target attribute
<4> Sets the inline type to "anchor" (the node name) and the link text to "link text"

Notice that the "window" must be an attribute, not an option. You can see where it is used here:


The linkattrs attribute doesn't apply here. That attribute controls how the built-in link macro is handled. By default, the content between the square brackets of a link macro is not parsed into an attribute list (instead, the whole value is used as the link text). The linkattrs attribute changes the behavior of the parser so that the content between the square brackets is parsed into an attribute list. In that case, only the first positional attribute is used as the link text.

The linkattrs attribute is not applicable to a custom inline macro.

I hope that helps clear things up!

Cheers,

-Dan


On Wed, Mar 30, 2016 at 11:22 PM, Jeremie Bresson [via Asciidoctor :: Discussion] <[hidden email]> wrote:
I need to create a link having a 'target="_blank"' attribute. This works great:
:linkattrs:

Hello link:http://eclipse.org[some text, window=\"_blank\"]

Now I do not understand how I can get the same inside my asciidoctor macro. I have tried several variant of:
  @Override
  protected Object process(AbstractBlock parent, String repository, Map<String, Object> attributes) {
    // Define options for an 'anchor' element:
    Map<String, Object> options = new HashMap<String, Object>();
    options.put("type", ":link");
    options.put("target", "http://eclipse.org");

    attributes.put("linkattrs", true);
    options.put("window", "_blank");

    // Create the 'anchor' node:
    Inline inline = createInline(parent, "anchor", "some text", attributes, options);

    // Convert to String value:
    return inline.convert();
  }
I think my problem is this linkattrs attribute.
Thank you in advance for your help.



If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/Create-a-link-with-a-target-blank-attribute-in-an-asciidoctorj-macro-tp4576.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML



--
Dan Allen | @mojavelinux | http://google.com/profiles/dan.j.allen
Reply | Threaded
Open this post in threaded view
|

Re: Create a link with a target blank attribute (in an asciidoctorj macro)

Jeremie Bresson
Thank you a lot for this feedback. It works as you told!
I thought I have tried this combination, but obviously this is not the case.

Is there some semantic to understand the difference between the attributes vs options map?

I think I should start to learn to read ruby code...
Reply | Threaded
Open this post in threaded view
|

Re: Create a link with a target blank attribute (in an asciidoctorj macro)

mojavelinux
Administrator

On Tue, Apr 12, 2016 at 9:20 PM, Jeremie Bresson [via Asciidoctor :: Discussion] <[hidden email]> wrote:
Is there some semantic to understand the difference between the attributes vs options map?

Generally speaking, attributes are passed from the document (they are content) and options are internal state.

The line is sometimes blurry. It has a lot to do with how Asciidoctor is implemented. There aren't many node options, so if you learn what they are, then it becomes a bit more clear.

I'll admit that node options were not something I thought about being public when I added them. That's why the distinction between attributions and options sometimes appears arbitrary.

Cheers,

-Dan


--
Dan Allen | @mojavelinux | http://google.com/profiles/dan.j.allen
Reply | Threaded
Open this post in threaded view
|

Re: Create a link with a target blank attribute (in an asciidoctorj macro)

mojavelinux
Administrator
In reply to this post by Jeremie Bresson

On Thu, Apr 14, 2016 at 12:47 AM, Dan Allen <[hidden email]> wrote:
Generally speaking, attributes are passed from the document (they are content) and options are internal state.

Actually, it might even be more accurate to say that node options were a way to make generic objects, so they are really part of the node type (the "type" option) or first-class properties (the "target" option).

-Dan

--
Dan Allen | @mojavelinux | http://google.com/profiles/dan.j.allen