Skip to content

Commit

Permalink
Make Asciidoctor expose interfaces exclusively
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpanzer committed Oct 22, 2018
1 parent 59fed13 commit def1e4c
Show file tree
Hide file tree
Showing 8 changed files with 628 additions and 448 deletions.
16 changes: 8 additions & 8 deletions asciidoctorj-core/src/main/java/org/asciidoctor/Asciidoctor.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package org.asciidoctor;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.asciidoctor.ast.Document;
import org.asciidoctor.ast.DocumentHeader;
import org.asciidoctor.ast.StructuredDocument;
Expand All @@ -18,6 +10,14 @@
import org.asciidoctor.internal.JRubyAsciidoctor;
import org.asciidoctor.log.LogHandler;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
*
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,82 +1,14 @@
package org.asciidoctor.converter;

import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyClass;

import java.util.HashMap;
import java.util.Map;

public class JavaConverterRegistry {

private Ruby rubyRuntime;

public JavaConverterRegistry(Ruby rubyRuntime) {
this.rubyRuntime = rubyRuntime;
}

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

RubyClass clazz = ConverterProxy.register(rubyRuntime, converterClass);

ConverterFor converterForAnnotation = converterClass.getAnnotation(ConverterFor.class);
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.newArray(rubyRuntime.newString(backend)));

} else if (backends.length == 0) {
// No backend annotation and no backend defined => register as default backend
getConverterFactory()
.callMethod("register", clazz);
}
if (backends.length > 0) {
// Always additionally register with names passed to this method
final RubyArray rubyBackendNames = new RubyArray(rubyRuntime, backends.length);
for (String backend: backends) {
rubyBackendNames.add(rubyRuntime.newString(backend));
}
getConverterFactory()
.callMethod("register", clazz, rubyBackendNames);
}
}

public Class<?> resolve(String backend) {
RubyClass rubyClass = (RubyClass) getConverterFactory()
.callMethod("resolve", rubyRuntime.newString(backend));

Class<?> clazz = rubyClass.getReifiedClass();
if (clazz != null) {
return clazz;
} else if (rubyClass.getAllocator() instanceof ConverterProxy.Allocator) {
ConverterProxy.Allocator allocator = (ConverterProxy.Allocator) rubyClass.getAllocator();
return allocator.getConverterClass();
}
return null;
}
public interface JavaConverterRegistry {

public void unregisterAll() {
getConverterFactory()
.callMethod("unregister_all");
}
<U, T extends Converter<U> & OutputFormatWriter<U>> void register(Class<T> converterClass, String... backends);

private RubyClass getConverterFactory() {
return rubyRuntime.getModule("Asciidoctor")
.defineOrGetModuleUnder("Converter")
.getClass("Factory");
}
Class<?> resolve(String backend);

public Map<String, Class<?>> converters() {
final RubyArray rubyKeys = (RubyArray) getConverterFactory()
.callMethod("converters")
.callMethod(rubyRuntime.getCurrentContext(), "keys");
void unregisterAll();

Map<String, Class<?>> converters = new HashMap<String, Class<?>>();
for (Object rubyBackend : rubyKeys) {
String backend = rubyBackend.toString();
converters.put(backend, resolve(backend));
}
return converters;
}
}
Map<String, Class<?>> converters();
}
Loading

0 comments on commit def1e4c

Please sign in to comment.