Loading custom extension from CLI

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

Loading custom extension from CLI

asotobu
Hi I am trying to run a very simple Ruby extension I have developed from CLI, but it don't work. Probably it is because of something related of Ruby and not Asciidoctor.

[source, ruby]
.extension.rb
----
require 'asciidoctor'
require 'asciidoctor/extensions'

class EmojifyBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor
  def process parent, target, attributes

    size = (attributes.has_key? 'size') ? attributes['size'] : '24'

    source = %(
    <img height="#{size}" src="http://www.tortue.me/emoji/#{target}.png" width="#{size}" />
    )
    Asciidoctor::Block.new parent, :pass, :content_model => :raw, :source => source
  end
end

Asciidoctor::Extensions.register do |document|
  if document.basebackend? 'html'
    block_macro :emoji, EmojifyBlockMacro
  end
end
----

Then when I run from the same directory of extension.rb next line:

asciidoctor test.adoc -r ./extension.rb -o test.html

Next exception id thrown:

extension.rb:17:in `block in <top (required)>': undefined method `basebackend?' for #<Asciidoctor::Extensions::Registry:0x00000002777228> (NoMethodError)
        from /var/lib/gems/1.9.1/gems/asciidoctor-1.5.1/lib/asciidoctor/extensions.rb:507:in `call'
        from /var/lib/gems/1.9.1/gems/asciidoctor-1.5.1/lib/asciidoctor/extensions.rb:507:in `block in activate'
        from /var/lib/gems/1.9.1/gems/asciidoctor-1.5.1/lib/asciidoctor/extensions.rb:500:in `each'

But I have seen that this method currently exists in document class, so what it is missing?

Thanks.


Reply | Threaded
Open this post in threaded view
|

Re: Loading custom extension from CLI

mojavelinux
Administrator
Alex,

This is happening because you are naming the block parameter document when it's really a registry. To access the document from the register block, you can either use the instance variable, @document:

[source,ruby]
----
Asciidoctor::Extensions.register do
  if @document.basebackend? 'html'
    block_macro :emoji, EmojifyBlockMacro
  end
end
----

We don't currently support document as an argument to the block, but we could if we check for an arity of 2. See https://github.com/asciidoctor/asciidoctor/blob/master/lib/asciidoctor/extensions.rb#L503. That would allow:

[source,ruby]
----
Asciidoctor::Extensions.register do |registry, document|
  if document.basebackend? 'html'
    block_macro :emoji, EmojifyBlockMacro
  end
end
----

Please note the second example is not currently supported.

Cheers,

-Dan

On Wed, Oct 29, 2014 at 3:33 AM, asotobu [via Asciidoctor :: Discussion] <[hidden email]> wrote:
Hi I am trying to run a very simple Ruby extension I have developed from CLI, but it don't work. Probably it is because of something related of Ruby and not Asciidoctor.

[source, ruby]
.extension.rb
----
require 'asciidoctor'
require 'asciidoctor/extensions'

class EmojifyBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor
  def process parent, target, attributes

    size = (attributes.has_key? 'size') ? attributes['size'] : '24'

    source = %(
    <img height="#{size}" src="http://www.tortue.me/emoji/#{target}.png" width="#{size}" />
    )
    Asciidoctor::Block.new parent, :pass, :content_model => :raw, :source => source
  end
end

Asciidoctor::Extensions.register do |document|
  if document.basebackend? 'html'
    block_macro :emoji, EmojifyBlockMacro
  end
end
----

Then when I run from the same directory of extension.rb next line:

asciidoctor test.adoc -r ./extension.rb -o test.html

Next exception id thrown:

extension.rb:17:in `block in <top (required)>': undefined method `basebackend?' for #<Asciidoctor::Extensions::Registry:0x00000002777228> (NoMethodError)
        from /var/lib/gems/1.9.1/gems/asciidoctor-1.5.1/lib/asciidoctor/extensions.rb:507:in `call'
        from /var/lib/gems/1.9.1/gems/asciidoctor-1.5.1/lib/asciidoctor/extensions.rb:507:in `block in activate'
        from /var/lib/gems/1.9.1/gems/asciidoctor-1.5.1/lib/asciidoctor/extensions.rb:500:in `each'

But I have seen that this method currently exists in document class, so what it is missing?

Thanks.





If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/Loading-custom-extension-from-CLI-tp2398.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML



--
Reply | Threaded
Open this post in threaded view
|

Re: Loading custom extension from CLI

asotobu
Ok, I have followed (copy/paste) the example in http://asciidoctor.org/docs/user-manual/#block-macro-processor-example maybe we could improve it.

Thank you so much for your time.

2014-10-29 23:25 GMT+01:00 mojavelinux [via Asciidoctor :: Discussion] <[hidden email]>:
Alex,

This is happening because you are naming the block parameter document when it's really a registry. To access the document from the register block, you can either use the instance variable, @document:

[source,ruby]
----
Asciidoctor::Extensions.register do
  if @document.basebackend? 'html'
    block_macro :emoji, EmojifyBlockMacro
  end
end
----

We don't currently support document as an argument to the block, but we could if we check for an arity of 2. See https://github.com/asciidoctor/asciidoctor/blob/master/lib/asciidoctor/extensions.rb#L503. That would allow:

[source,ruby]
----
Asciidoctor::Extensions.register do |registry, document|
  if document.basebackend? 'html'
    block_macro :emoji, EmojifyBlockMacro
  end
end
----

Please note the second example is not currently supported.

Cheers,

-Dan

On Wed, Oct 29, 2014 at 3:33 AM, asotobu [via Asciidoctor :: Discussion] <[hidden email]> wrote:
Hi I am trying to run a very simple Ruby extension I have developed from CLI, but it don't work. Probably it is because of something related of Ruby and not Asciidoctor.

[source, ruby]
.extension.rb
----
require 'asciidoctor'
require 'asciidoctor/extensions'

class EmojifyBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor
  def process parent, target, attributes

    size = (attributes.has_key? 'size') ? attributes['size'] : '24'

    source = %(
    <img height="#{size}" src="http://www.tortue.me/emoji/#{target}.png" width="#{size}" />
    )
    Asciidoctor::Block.new parent, :pass, :content_model => :raw, :source => source
  end
end

Asciidoctor::Extensions.register do |document|
  if document.basebackend? 'html'
    block_macro :emoji, EmojifyBlockMacro
  end
end
----

Then when I run from the same directory of extension.rb next line:

asciidoctor test.adoc -r ./extension.rb -o test.html

Next exception id thrown:

extension.rb:17:in `block in <top (required)>': undefined method `basebackend?' for #<Asciidoctor::Extensions::Registry:0x00000002777228> (NoMethodError)
        from /var/lib/gems/1.9.1/gems/asciidoctor-1.5.1/lib/asciidoctor/extensions.rb:507:in `call'
        from /var/lib/gems/1.9.1/gems/asciidoctor-1.5.1/lib/asciidoctor/extensions.rb:507:in `block in activate'
        from /var/lib/gems/1.9.1/gems/asciidoctor-1.5.1/lib/asciidoctor/extensions.rb:500:in `each'

But I have seen that this method currently exists in document class, so what it is missing?

Thanks.





If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/Loading-custom-extension-from-CLI-tp2398.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML



--



If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/Loading-custom-extension-from-CLI-tp2398p2401.html
To unsubscribe from Loading custom extension from CLI, click here.
NAML



--
+----------------------------------------------------------+
  Alex Soto Bueno - Computer Engineer
  www.lordofthejars.com
+----------------------------------------------------------+
Reply | Threaded
Open this post in threaded view
|

Re: Loading custom extension from CLI

mojavelinux
Administrator

The extension examples in the user manual came from the 0.1.4 release notes, so they likely all need reviewing. Sorry for misleading you.

-Dan

Reply | Threaded
Open this post in threaded view
|

Re: Loading custom extension from CLI

asotobu
No worries :) Sorry for disturbing you with my basic questions hehehe.

2014-10-30 8:51 GMT+01:00 mojavelinux [via Asciidoctor :: Discussion] <[hidden email]>:

The extension examples in the user manual came from the 0.1.4 release notes, so they likely all need reviewing. Sorry for misleading you.

-Dan




If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/Loading-custom-extension-from-CLI-tp2398p2404.html
To unsubscribe from Loading custom extension from CLI, click here.
NAML



--
+----------------------------------------------------------+
  Alex Soto Bueno - Computer Engineer
  www.lordofthejars.com
+----------------------------------------------------------+