Skip to content

Commit

Permalink
Fix to allow extensions from non-"com." packages to be registered
Browse files Browse the repository at this point in the history
Resolves #250, including test which fails without this fix
  • Loading branch information
ktoso authored and lordofthejars committed Feb 7, 2015
1 parent 88654d6 commit 7a14705
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 57 deletions.
Expand Up @@ -18,93 +18,90 @@ public JavaExtensionRegistry(AsciidoctorModule asciidoctorModule, Ruby rubyRunti
public void docinfoProcessor(Class<? extends DocinfoProcessor> docInfoProcessor) {
// this may change in future to external class to deal with dynamic
// imports
this.rubyRuntime.evalScriptlet("java_import " + getImportLine(docInfoProcessor));
javaImport(rubyRuntime, docInfoProcessor);
this.asciidoctorModule.docinfo_processor(RubyUtils.toRubyClass(rubyRuntime, docInfoProcessor));
}

public void docinfoProcessor(DocinfoProcessor docInfoProcessor) {
// this may change in future to external class to deal with dynamic
// imports
this.rubyRuntime.evalScriptlet("java_import " + getImportLine(docInfoProcessor.getClass()));
javaImport(rubyRuntime, docInfoProcessor.getClass());
this.asciidoctorModule.docinfo_processor(docInfoProcessor);
}

public void docinfoProcessor(String docInfoProcessor) {
// this may change in future to external class to deal with dynamic
// imports
this.rubyRuntime.evalScriptlet("java_import " + docInfoProcessor);
javaImport(rubyRuntime, docInfoProcessor);
this.asciidoctorModule.docinfo_processor(getClassName(docInfoProcessor));
}

public void preprocessor(Class<? extends Preprocessor> preprocessor) {
// this may change in future to external class to deal with dynamic
// imports
this.rubyRuntime.evalScriptlet("java_import " + getImportLine(preprocessor));
javaImport(rubyRuntime, preprocessor);
this.asciidoctorModule.preprocessor(RubyUtils.toRubyClass(rubyRuntime, preprocessor));
}

public void preprocessor(Preprocessor preprocessor) {
// this may change in future to external class to deal with dynamic
// imports
this.rubyRuntime.evalScriptlet("java_import " + getImportLine(preprocessor.getClass()));
javaImport(rubyRuntime, preprocessor.getClass());
this.asciidoctorModule.preprocessor(preprocessor);
}

public void preprocessor(String preprocessor) {
// this may change in future to external class to deal with dynamic
// imports
this.rubyRuntime.evalScriptlet("java_import " + preprocessor);
javaImport(rubyRuntime, preprocessor);
this.asciidoctorModule.preprocessor(getClassName(preprocessor));
}

public void postprocessor(String postprocessor) {
// this may change in future to external class to deal with dynamic
// imports
this.rubyRuntime.evalScriptlet("java_import " + postprocessor);
javaImport(rubyRuntime, postprocessor);
this.asciidoctorModule.postprocessor(getClassName(postprocessor));
}

public void postprocessor(Class<? extends Postprocessor> postprocessor) {
// this may change in future to external class to deal with dynamic
// imports
this.rubyRuntime.evalScriptlet("java_import " + getImportLine(postprocessor));
javaImport(rubyRuntime, postprocessor);
this.asciidoctorModule.postprocessor(RubyUtils.toRubyClass(rubyRuntime, postprocessor));
}

public void postprocessor(Postprocessor postprocesor) {
// this may change in future to external class to deal with dynamic
// imports
this.rubyRuntime.evalScriptlet("java_import " + getImportLine(postprocesor.getClass()));
javaImport(rubyRuntime, postprocesor.getClass());
this.asciidoctorModule.postprocessor(postprocesor);
}

public void includeProcessor(
String includeProcessor) {
public void includeProcessor(String includeProcessor) {
// this may change in future to external class to deal with dynamic
// imports
this.rubyRuntime.evalScriptlet("java_import "
+ includeProcessor);
javaImport(rubyRuntime, includeProcessor);
this.asciidoctorModule.include_processor(getClassName(includeProcessor));
}

public void includeProcessor(
Class<? extends IncludeProcessor> includeProcessor) {
// this may change in future to external class to deal with dynamic
// imports
this.rubyRuntime.evalScriptlet("java_import "
+ getImportLine(includeProcessor));
this.asciidoctorModule.include_processor(RubyUtils.toRubyClass(rubyRuntime, includeProcessor));
javaImport(rubyRuntime, includeProcessor);
this.asciidoctorModule.include_processor(RubyUtils.toRubyClass(rubyRuntime, includeProcessor));
}

public void includeProcessor(IncludeProcessor includeProcessor) {
String importLine = getImportLine(includeProcessor.getClass());
this.rubyRuntime.evalScriptlet("java_import " + importLine);
this.asciidoctorModule.include_processor(includeProcessor);
String importLine = getImportLine(includeProcessor.getClass());
javaImport(rubyRuntime, importLine);
this.asciidoctorModule.include_processor(includeProcessor);
}

public void treeprocessor(Treeprocessor treeprocessor) {
this.rubyRuntime.evalScriptlet("java_import " + getImportLine(treeprocessor.getClass()));
this.asciidoctorModule.treeprocessor(treeprocessor);
javaImport(rubyRuntime, treeprocessor.getClass());
this.asciidoctorModule.treeprocessor(treeprocessor);
}

public void treeprocessor(Class<? extends Treeprocessor> treeProcessor) {
Expand All @@ -118,17 +115,15 @@ public void treeprocessor(Class<? extends Treeprocessor> treeProcessor) {
public void treeprocessor(String treeProcessor) {
// this may change in future to external class to deal with dynamic
// imports
this.rubyRuntime
.evalScriptlet("java_import " + treeProcessor);
javaImport(rubyRuntime, treeProcessor);
this.asciidoctorModule.treeprocessor(getClassName(treeProcessor));
}

public void block(String blockName,
String blockProcessor) {
// this may change in future to external class to deal with dynamic
// imports
this.rubyRuntime.evalScriptlet("java_import "
+ blockProcessor);
javaImport(rubyRuntime, blockProcessor);

this.asciidoctorModule.block_processor(
getClassName(blockProcessor),
Expand All @@ -139,8 +134,7 @@ public void block(String blockName,
Class<? extends BlockProcessor> blockProcessor) {
// this may change in future to external class to deal with dynamic
// imports
this.rubyRuntime.evalScriptlet("java_import "
+ getImportLine(blockProcessor));
javaImport(rubyRuntime, blockProcessor);

this.asciidoctorModule.block_processor(
RubyUtils.toRubyClass(rubyRuntime, blockProcessor),
Expand All @@ -155,8 +149,7 @@ public void block(String blockName,
BlockProcessor blockProcessor) {
// this may change in future to external class to deal with dynamic
// imports
this.rubyRuntime.evalScriptlet("java_import "
+ getImportLine(blockProcessor.getClass()));
javaImport(rubyRuntime, blockProcessor.getClass());

this.asciidoctorModule.block_processor(
blockProcessor,
Expand All @@ -167,8 +160,7 @@ public void blockMacro(String blockName,
Class<? extends BlockMacroProcessor> blockMacroProcessor) {
// this may change in future to external class to deal with dynamic
// imports
this.rubyRuntime.evalScriptlet("java_import "
+ getImportLine(blockMacroProcessor));
javaImport(rubyRuntime, blockMacroProcessor);
this.asciidoctorModule.block_macro(
blockMacroProcessor.getSimpleName(),
RubyUtils.toSymbol(rubyRuntime, blockName));
Expand All @@ -178,30 +170,25 @@ public void blockMacro(String blockName,
String blockMacroProcessor) {
// this may change in future to external class to deal with dynamic
// imports
this.rubyRuntime.evalScriptlet("java_import "
+ blockMacroProcessor);
javaImport(rubyRuntime, blockMacroProcessor);
this.asciidoctorModule.block_macro(
getClassName(blockMacroProcessor),
RubyUtils.toSymbol(rubyRuntime, blockName));
}

public void blockMacro(
BlockMacroProcessor blockMacroProcessor) {
public void blockMacro(BlockMacroProcessor blockMacroProcessor) {
// this may change in future to external class to deal with dynamic
// imports
this.rubyRuntime.evalScriptlet("java_import "
+ getImportLine(blockMacroProcessor.getClass()));
javaImport(rubyRuntime, blockMacroProcessor.getClass());
this.asciidoctorModule.block_macro(
blockMacroProcessor,
RubyUtils.toSymbol(rubyRuntime, blockMacroProcessor.getName()));
}

public void inlineMacro(
InlineMacroProcessor inlineMacroProcessor) {
public void inlineMacro(InlineMacroProcessor inlineMacroProcessor) {
// this may change in future to external class to deal with dynamic
// imports
this.rubyRuntime.evalScriptlet("java_import "
+ getImportLine(inlineMacroProcessor.getClass()));
javaImport(rubyRuntime, inlineMacroProcessor.getClass());

this.asciidoctorModule.inline_macro(
inlineMacroProcessor,
Expand All @@ -212,34 +199,36 @@ public void inlineMacro(String blockName,
Class<? extends InlineMacroProcessor> inlineMacroProcessor) {
// this may change in future to external class to deal with dynamic
// imports
this.rubyRuntime.evalScriptlet("java_import "
+ getImportLine(inlineMacroProcessor));
javaImport(rubyRuntime, inlineMacroProcessor);

this.asciidoctorModule.inline_macro(
RubyUtils.toRubyClass(rubyRuntime, inlineMacroProcessor),
RubyUtils.toSymbol(rubyRuntime, blockName));
}

public void inlineMacro(String blockName,
String inlineMacroProcessor) {
// this may change in future to external class to deal with dynamic
// imports
this.rubyRuntime.evalScriptlet("java_import "
+ inlineMacroProcessor);
public void inlineMacro(String blockName, String inlineMacroProcessor) {
// this may change in future to external class to deal with dynamic imports
javaImport(this.rubyRuntime, inlineMacroProcessor);

this.asciidoctorModule.inline_macro(
getClassName(inlineMacroProcessor),
RubyUtils.toSymbol(rubyRuntime, blockName));
}


private void javaImport(Ruby ruby, Class<?> clazz) {
ruby.evalScriptlet(String.format("java_import '%s'", getImportLine(clazz)));
}

private void javaImport(Ruby ruby, String className) {
ruby.evalScriptlet(String.format("java_import '%s'", className));
}

private String getImportLine(Class<?> extensionClass) {

int dollarPosition = -1;
String className = extensionClass.getName();
if((dollarPosition = className.indexOf("$")) != -1) {
className = className.substring(0, dollarPosition);
}

int dollarPosition = -1;
String className = extensionClass.getName();
if ((dollarPosition = className.indexOf("$")) != -1) {
className = className.substring(0, dollarPosition);
}
return className;
}

Expand Down
@@ -0,0 +1,47 @@
package org.asciidoctor.internal;

import org.asciidoctor.Asciidoctor;
import org.asciidoctor.SafeMode;
import org.asciidoctor.ast.AbstractBlock;
import org.asciidoctor.ast.Block;
import org.asciidoctor.extension.BlockMacroProcessor;
import org.asciidoctor.extension.JavaExtensionRegistry;
import org.asciidoctor.internal.JRubyAsciidoctor;
import org.asciidoctor.util.ClasspathResources;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import unusual.extension.BoldifyPostProcessor;

import java.io.File;
import java.util.Arrays;
import java.util.Map;

import static org.asciidoctor.OptionsBuilder.options;

public class WhenLoadingExtensionFromUnusualPackage {

@Rule
public ClasspathResources classpath = new ClasspathResources();

private Asciidoctor asciidoctor = JRubyAsciidoctor.create();

@Test
public void shouldAllowLoadingUsingInstance() {
final JavaExtensionRegistry registry = asciidoctor.javaExtensionRegistry();
registry.postprocessor(new unusual.extension.BoldifyPostProcessor());
}

@Test
public void shouldAllowLoadingByClassName() {
final JavaExtensionRegistry registry = asciidoctor.javaExtensionRegistry();
registry.postprocessor(BoldifyPostProcessor.class.getCanonicalName());
}

@Test
public void shouldAllowLoadingByClass() {
final JavaExtensionRegistry registry = asciidoctor.javaExtensionRegistry();
registry.postprocessor(BoldifyPostProcessor.class);
}

}
@@ -0,0 +1,20 @@
package unusual.extension;

import org.asciidoctor.ast.Document;
import org.asciidoctor.extension.Postprocessor;

/**
* This processor is used only for checking we are able to load extensions from "unusual packages".
*
* @see org.asciidoctor.internal.WhenLoadingExtensionFromUnusualPackage
* @see <a href="https://github.com/asciidoctor/asciidoctorj/issues/250">Issue #250</a>
*/
public class BoldifyPostProcessor extends Postprocessor {
@Override public String process(Document document, String output) {
if (document.basebackend("html")) {
return output.replaceAll("bold", "<b>bold</b>");
} else {
return output;
}
}
}

0 comments on commit 7a14705

Please sign in to comment.