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

Add first support for registering converter instances #933

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

public interface JavaConverterRegistry {

<U, T extends Converter<U> & OutputFormatWriter<U>> void register(T converterClass, String... backends);

<U, T extends Converter<U> & OutputFormatWriter<U>> void register(Class<T> converterClass, String... backends);

Class<?> resolve(String backend);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,38 @@

public class ConverterProxy<T> extends RubyObject {

private static final String ASCIIDOCTORJ_CONVERTER_INSTANCE = "AsciidoctorJConverterInstance";

protected static final String METHOD_NAME_INITIALIZE = "initialize";
protected static final String METHOD_NAME_CONVERT = "convert";

private final JRubyAsciidoctor asciidoctor;

private T output;

public static <U, T extends Converter<U> & OutputFormatWriter<U>> IRubyObject register(JRubyAsciidoctor asciidoctor, T converter) {
Ruby rubyRuntime = asciidoctor.getRubyRuntime();
RubyModule module = rubyRuntime.defineModule(getModuleName(converter.getClass()));
RubyClass clazz = module.getClass(converter.getClass().getSimpleName());
if (clazz == null) {
clazz = module.defineClassUnder(
converter.getClass().getSimpleName(),
rubyRuntime.getObject(),
new ConverterProxy.InstanceAllocator(asciidoctor));
includeModule(clazz, "Asciidoctor", "Converter");
includeModule(clazz, "Asciidoctor", "Converter", "BackendTraits");

clazz.defineAnnotatedMethod(ConverterProxy.class, "initializeInstance");
clazz.defineAnnotatedMethod(ConverterProxy.class, "convertInstance");

includeModule(clazz, "Asciidoctor", "Writer");
clazz.defineAnnotatedMethod(ConverterProxy.class, "write");
}
RubyHash param = RubyHash.newHash(rubyRuntime);
param.put(rubyRuntime.newSymbol(ASCIIDOCTORJ_CONVERTER_INSTANCE), converter);
return clazz.newInstance(rubyRuntime.getCurrentContext(), param, Block.NULL_BLOCK);
}

public static <U, T extends Converter<U> & OutputFormatWriter<U>> RubyClass register(JRubyAsciidoctor asciidoctor, final Class<T> converterClass) {
Ruby rubyRuntime = asciidoctor.getRubyRuntime();
RubyModule module = rubyRuntime.defineModule(getModuleName(converterClass));
Expand Down Expand Up @@ -79,6 +106,23 @@ public Class<? extends Converter> getConverterClass() {
}
}

public static class InstanceAllocator implements ObjectAllocator {
private Converter converter;
private final JRubyAsciidoctor asciidoctor;

public InstanceAllocator(JRubyAsciidoctor asciidoctor) {
this.asciidoctor = asciidoctor;
}
@Override
public IRubyObject allocate(Ruby runtime, RubyClass rubyClass) {
return new ConverterProxy(runtime, rubyClass, asciidoctor);
}

public Converter getConverter() {
return converter;
}
}

private final Class<? extends Converter> converterClass;

private Converter<T> delegate;
Expand All @@ -89,7 +133,13 @@ public ConverterProxy(Ruby runtime, RubyClass metaClass, Class<? extends Convert
this.asciidoctor = asciidoctor;
}

@JRubyMethod(required = 1, optional = 1)
public ConverterProxy(Ruby runtime, RubyClass metaClass, JRubyAsciidoctor asciidoctor) {
super(runtime, metaClass);
this.converterClass = null;
this.asciidoctor = asciidoctor;
}

@JRubyMethod(name = METHOD_NAME_INITIALIZE, required = 1, optional = 1)
public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
try {
String backend = (String) JavaEmbedUtils.rubyToJava(getRuntime(), args[0], String.class);
Expand All @@ -112,15 +162,9 @@ public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
}

return null;
} catch (InstantiationException e) {
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
// TODO: Do some proper logging in the catch clauses?
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (Throwable e) {
throw new RuntimeException(e);
}
Expand All @@ -130,15 +174,15 @@ private String getOutfileSuffix() {
String outfileSuffix = this.delegate.getOutfileSuffix();
if (outfileSuffix == null && delegate.getClass().getAnnotation(ConverterFor.class) != null) {
ConverterFor converterAnnotation = delegate.getClass().getAnnotation(ConverterFor.class);
if (converterAnnotation.suffix() != ConverterFor.UNDEFINED) {
if (!ConverterFor.UNDEFINED.equals(converterAnnotation.suffix())) {
outfileSuffix = converterAnnotation.suffix();
this.delegate.setOutfileSuffix(outfileSuffix);
}
}
return outfileSuffix;
}

@JRubyMethod(required = 1, optional = 2)
@JRubyMethod(name = METHOD_NAME_CONVERT, required = 1, optional = 2)
public IRubyObject convert(ThreadContext context, IRubyObject[] args) {
ContentNode node = NodeConverter.createASTNode(args[0]);

Expand All @@ -163,6 +207,43 @@ public IRubyObject convert(ThreadContext context, IRubyObject[] args) {
return JavaEmbedUtils.javaToRuby(getRuntime(), ret);
}

@JRubyMethod(name = METHOD_NAME_INITIALIZE, required = 1, optional = 1)
public IRubyObject initializeInstance(ThreadContext context, IRubyObject[] args) {
try {
RubyHash param = (RubyHash) args[0];
delegate = (Converter<T>) param.get(context.getRuntime().newSymbol(ASCIIDOCTORJ_CONVERTER_INSTANCE));
return null;
} catch (Throwable e) {
throw new RuntimeException(e);
}
}


@JRubyMethod(name = METHOD_NAME_CONVERT, required = 1, optional = 2)
public IRubyObject convertInstance(ThreadContext context, IRubyObject[] args) {
ContentNode node = NodeConverter.createASTNode(args[0]);

T ret = null;
if (args.length == 1) {
ret = delegate.convert(
node,
null,
Collections.emptyMap());
} else if (args.length == 2) {
ret = delegate.convert(
node,
(String) JavaEmbedUtils.rubyToJava(getRuntime(), args[1], String.class),
Collections.emptyMap());//RubyString.objAsString(context, args[1]).asJavaString());
} else if (args.length == 3) {
ret = (T) delegate.convert(
node,
(String) JavaEmbedUtils.rubyToJava(getRuntime(), args[1], String.class),
(Map) JavaEmbedUtils.rubyToJava(getRuntime(), args[2], Map.class));
}
this.output = ret;
return JavaEmbedUtils.javaToRuby(getRuntime(), ret);
}

@JRubyMethod
public IRubyObject write(ThreadContext context, IRubyObject output, IRubyObject target) throws IOException {
OutputStream out = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.jruby.RubyArray;
import org.jruby.RubyClass;
import org.jruby.RubyModule;
import org.jruby.runtime.builtin.IRubyObject;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -24,28 +25,38 @@ public JavaConverterRegistryImpl(JRubyAsciidoctor asciidoctor) {
this.rubyRuntime = asciidoctor.getRubyRuntime();
}


@Override
public <U, T extends Converter<U> & OutputFormatWriter<U>> void register(final Class<T> converterClass, String... backends) {
public <U, T extends Converter<U> & OutputFormatWriter<U>> void register(final T converter, String... backends) {
IRubyObject converterInstance = ConverterProxy.register(asciidoctor, converter);
ConverterFor converterForAnnotation = converter.getClass().getAnnotation(ConverterFor.class);
registerConverter(converterInstance, converterForAnnotation, backends);
}

@Override
public <U, T extends Converter<U> & OutputFormatWriter<U>> void register(final Class<T> converterClass, String... backends) {
RubyClass clazz = ConverterProxy.register(asciidoctor, converterClass);

ConverterFor converterForAnnotation = converterClass.getAnnotation(ConverterFor.class);
registerConverter(clazz, converterForAnnotation, backends);
}

private void registerConverter(IRubyObject converter, ConverterFor converterForAnnotation, String[] backends) {
if (converterForAnnotation != null) {
// Backend annotation present => Register with name given in annotation
String backend = !ConverterFor.UNDEFINED.equals(converterForAnnotation.format()) ? converterForAnnotation.format() : converterForAnnotation.value();
getConverterFactory()
.callMethod("register", clazz, rubyRuntime.newString(backend));
.callMethod("register", converter, rubyRuntime.newString(backend));

} else if (backends.length == 0) {
// No backend annotation and no backend defined => register as default backend
getConverterFactory()
.callMethod("register", clazz, rubyRuntime.newString("*"));
.callMethod("register", converter, rubyRuntime.newString("*"));
}
if (backends.length > 0) {
// Always additionally register with names passed to this method
for (String backend: backends) {
getConverterFactory()
.callMethod("register", clazz, rubyRuntime.newString(backend));
.callMethod("register", converter, rubyRuntime.newString(backend));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,31 @@ a|
then:
content.readLines().collect {it - ~/\s+$/ } == '''HELLO ASCIIDOCTOR TABLE

A HEADER |Second column'''.readLines()

}

def "should register a converter instance"() {

given:
String document = '''
= Hello Asciidoctor Table

[cols="2"]
|====
a|
= A header
| Second column
|====
'''
asciidoctor.javaConverterRegistry().register(new TableTestConverter(), 'tabletestconverter')

when:
String content = asciidoctor.convert(document, OptionsBuilder.options().headerFooter(false).backend('tabletestconverter'))

then:
content.readLines().collect {it - ~/\s+$/ } == '''HELLO ASCIIDOCTOR TABLE

A HEADER |Second column'''.readLines()

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class TableTestConverter extends StringConverter {
public static final String NEWLINE = '\n'
public static final String NEWLINE_2 = NEWLINE * 2

TableTestConverter() {
super('???', null)
}

TableTestConverter(String backend, Map<String, Object> opts) {
super(backend, opts)
}
Expand Down