Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow class and class instance to be registered as extensions #399

Closed
mojavelinux opened this issue Dec 21, 2017 · 2 comments
Closed

Allow class and class instance to be registered as extensions #399

mojavelinux opened this issue Dec 21, 2017 · 2 comments

Comments

@mojavelinux
Copy link
Member

mojavelinux commented Dec 21, 2017

Currently, the extension registry makes an assumption that all extensions are defined using the block / proc form. However, all the extension registration methods also accept a class and class instance. The argument should only be wrapped in toBlock if it is in fact a block. Otherwise, it should be passed through as is.

Here's an example:

const Asciidoctor = require('asciidoctor.js')()
const Opal = global.Opal

const IncludeProcessor = (() => {
  const superclass = Asciidoctor.$$const.Extensions.$$const.IncludeProcessor
  const scope = Opal.klass(Opal.Object, superclass, 'MyIncludeProcessor', function () {})

  Opal.defn(scope, '$initialize', function initialize () {
    Opal.send(this, Opal.find_super_dispatcher(this, 'initialize', initialize))
  })

  Opal.defn(scope, '$process', function (doc, reader, target, attrs) {
    reader.pushInclude(['included content'], target, target, 1, attrs)
  })

  return scope
})()

const registry = Asciidoctor.Extensions.create()
registry.$include_processor(IncludeProcessor.$new())

Asciidoctor.convert('include::whatever.adoc[]', {
  safe: 'safe',
  extension_registry: registry
})
@mojavelinux
Copy link
Member Author

The benefit of defining a formal class is that it avoids the overhead of creating a block for call to Asciidoctor.

@ggrossetie
Copy link
Member

I think we need to work on the API to define a formal class because the code is way more complicated.
Apart from that 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants