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

It should be possible to register an instance in the javaConverterRegistry #932

Open
ggrossetie opened this issue Jun 26, 2020 · 3 comments · May be fixed by #933
Open

It should be possible to register an instance in the javaConverterRegistry #932

ggrossetie opened this issue Jun 26, 2020 · 3 comments · May be fixed by #933

Comments

@ggrossetie
Copy link
Member

Asciidoctor (Ruby) allows to register both class and instance. Registering an instance can be useful when you want to pass additional options.

Consider the following example:

class GoogleSlidesConverterRegistry : ConverterRegistry {
  override fun register(asciidoctor: Asciidoctor) {
    val googleSlidesConverter = GoogleSlidesConverter("googleslides", mapOf("credentials-path" to "/path/to/credentials.json"))
    asciidoctor.javaConverterRegistry().register(googleSlidesConverter, "googleslides")
  }
}

In this example, the converter needs a credentials file to authenticate with the Google Slides API.

@robertpanzer
Copy link
Member

Agreed, we might want to add this too.
Until then you could pass the options to convert() and then get them from the Document:

        asciidoctor.convert(document, OptionsBuilder.options()
                .option('credentials-path', '/path/to/credentials.json'').backend('googleslides')

And then in the Converter:

    @Override
    String convert(ContentNode node, String transform, Map<Object, Object> opts) {
      String credentials = node.getDocument().getOptions().get('credentials-path')
      ...
    }

I think this solution would also have some advantages in multithreaded environments when different threads/contexts would require different credentials.

@robertpanzer robertpanzer linked a pull request Jun 28, 2020 that will close this issue
5 tasks
@ggrossetie
Copy link
Member Author

Thanks Robert for your reply.

Another alternative would be to retrieve the Document (and therefore the options) from the constructor. I didn't try but reading the Ruby code I believe that Asciidoctor (Ruby) passes the Document as an options when the converter is instantiated/created (see lib/asciidoctor/document.rb#L1128-L1144)

So in theory, the following should work:

class GoogleSlidesConverter extends AbstractConverter {
  public GoogleSlidesConverter(String backend, Map<Object, Object> opts) {
    String credentials = ((Document) opts.get("document")).getOptions().get("credentials-path")
  }
}

@robertpanzer
Copy link
Member

Yes, that should work too.

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

Successfully merging a pull request may close this issue.

2 participants