Skip to content

Commit

Permalink
Fixes #359. Explicit unregistration of extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpanzer committed Aug 1, 2017
1 parent 1dd23bb commit 373b13d
Show file tree
Hide file tree
Showing 10 changed files with 859 additions and 56 deletions.
24 changes: 24 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,30 @@ String content = asciidoctor.convertFile(new File(

You can unregister all extensions by calling the `org.asciidoctor.Asciidoctor.unregisterAllExtensions()` method.

Additionally, since AsciidoctorJ 1.5.6.1 it is possible to unregister specific extensions when using the ExtensionGroup API.
This allows to register and unregister a group of extensions as a whole:

[source,java]
----
BlockProcessor extension1 = ...
Treeprocessor extension2 = ...
ExtensionGroup extensionGroup =
asciidoctor.createGroup()
.block(extension1)
.treeprocessor(extension2);
asciidoctor.convert(...) // <1>
extensionGroup.register();
asciidoctor.convert(); // <2>
extensionGroup.register();
asciidoctor.convert(); // <3>
----
<1> Convert a document without the extensions active because they were not registered yet.
<2> Convert with extensions active after registration.
<3> Convert without extensions active after the extensions were unregistered.

=== Ruby extensions

You can even register extensions written in Ruby using AsciidoctorJ.
Expand Down
16 changes: 14 additions & 2 deletions asciidoctorj-core/src/main/java/org/asciidoctor/Asciidoctor.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.asciidoctor.ast.DocumentRuby;
import org.asciidoctor.ast.StructuredDocument;
import org.asciidoctor.converter.JavaConverterRegistry;
import org.asciidoctor.extension.ExtensionGroup;
import org.asciidoctor.extension.JavaExtensionRegistry;
import org.asciidoctor.extension.RubyExtensionRegistry;
import org.asciidoctor.internal.JRubyAsciidoctor;
Expand Down Expand Up @@ -633,12 +634,23 @@ String[] convertFiles(Collection<File> asciidoctorFiles,
* @return Converter Registry object.
*/
JavaConverterRegistry javaConverterRegistry();


/**
* Creates an ExtensionGroup that can be used to register and unregister a group of extensions.
* @return
*/
ExtensionGroup createGroup();

/**
* Creates an ExtensionGroup that can be used to register and unregister a group of extensions.
* @return
*/
ExtensionGroup createGroup(String groupName);
/**
* Unregister all registered extensions.
*/
void unregisterAllExtensions();

/**
* This method frees all resources consumed by asciidoctorJ module. Keep in mind that if this method is called, instance becomes unusable and you should create another instance.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.asciidoctor.extension;


import java.io.InputStream;

/**
* An ExtensionGroup allows to collectively register and unregister extensions.
* All extensions are registered lazily and are not effective before a call to {@link #register()}.
*
* <p>Example:
* <code><pre>
* ExtensionGroup group = asciidoctor.createGroup();
* group.block(myBlock).preprocessor(mypreprocessor);
*
* // Convert with extensions
* group.register();
* asciidoctor.convert(...);
* group.unregister();
*
* // Convert without extensions
* asciidoctor.convert(...);
* </pre></code></p>
*/
public interface ExtensionGroup {

public void register();

public void unregister();

public ExtensionGroup docinfoProcessor(Class<? extends DocinfoProcessor> docInfoProcessor);
public ExtensionGroup docinfoProcessor(DocinfoProcessor docInfoProcessor);
public ExtensionGroup docinfoProcessor(String docInfoProcessor);

public ExtensionGroup preprocessor(Class<? extends Preprocessor> preprocessor);
public ExtensionGroup preprocessor(Preprocessor preprocessor);
public ExtensionGroup preprocessor(String preprocessor);

public ExtensionGroup postprocessor(String postprocessor);
public ExtensionGroup postprocessor(Class<? extends Postprocessor> postprocessor);
public ExtensionGroup postprocessor(Postprocessor postprocesor);

public ExtensionGroup includeProcessor(String includeProcessor);
public ExtensionGroup includeProcessor(Class<? extends IncludeProcessor> includeProcessor);
public ExtensionGroup includeProcessor(IncludeProcessor includeProcessor);

public ExtensionGroup treeprocessor(Treeprocessor treeprocessor);
public ExtensionGroup treeprocessor(Class<? extends Treeprocessor> treeProcessor);
public ExtensionGroup treeprocessor(String treeProcessor);

public ExtensionGroup block(String blockName, String blockProcessor);
public ExtensionGroup block(String blockName, Class<? extends BlockProcessor> blockProcessor);
public ExtensionGroup block(BlockProcessor blockProcessor);
public ExtensionGroup block(String blockName, BlockProcessor blockProcessor);

public ExtensionGroup blockMacro(String blockName, Class<? extends BlockMacroProcessor> blockMacroProcessor);
public ExtensionGroup blockMacro(String blockName, String blockMacroProcessor);
public ExtensionGroup blockMacro(BlockMacroProcessor blockMacroProcessor);

public ExtensionGroup inlineMacro(InlineMacroProcessor inlineMacroProcessor);
public ExtensionGroup inlineMacro(String blockName, Class<? extends InlineMacroProcessor> inlineMacroProcessor);
public ExtensionGroup inlineMacro(String blockName, String inlineMacroProcessor);

public ExtensionGroup requireRubyLibrary(String requiredLibrary);
public ExtensionGroup loadRubyClass(InputStream rubyClassStream);

public ExtensionGroup rubyPreprocessor(String preprocessor);
public ExtensionGroup rubyPostprocessor(String postprocessor);
public ExtensionGroup rubyDocinfoProcessor(String docinfoProcessor);
public ExtensionGroup rubyIncludeProcessor(String includeProcessor);
public ExtensionGroup rubyTreeprocessor(String treeProcessor);
public ExtensionGroup rubyBlock(String blockName, String blockProcessor);
public ExtensionGroup rubyBlockMacro(String blockName, String blockMacroProcessor);
public ExtensionGroup rubyInlineMacro(String blockName, String inlineMacroProcessor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class RubyExtensionRegistry {

private AsciidoctorModule asciidoctorModule;

private Ruby rubyRuntime;

public RubyExtensionRegistry(AsciidoctorModule asciidoctorModule,
Expand All @@ -33,8 +34,8 @@ public RubyExtensionRegistry preprocessor(String preprocessor) {
return this;
}

public RubyExtensionRegistry postprocessor(String postprocesor) {
this.asciidoctorModule.postprocessor(postprocesor);
public RubyExtensionRegistry postprocessor(String postprocessor) {
this.asciidoctorModule.postprocessor(postprocessor);
return this;
}

Expand Down Expand Up @@ -72,5 +73,4 @@ public RubyExtensionRegistry inlineMacro(String blockName, String inlineMacroPro
inlineMacroProcessor, RubyUtils.toSymbol(rubyRuntime, blockName));
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,63 @@
public interface AsciidoctorModule {

void preprocessor(String preprocessorClassName);
void preprocessor(String preprocessorClassName, String registrationName);
void preprocessor(RubyClass preprocessorClassName);
void preprocessor(Preprocessor preprocessor);

void preprocessor(RubyClass preprocessorClassName, String registrationName);
void preprocessor(Preprocessor preprocessor);
void preprocessor(Preprocessor preprocessor, String registrationName);

void postprocessor(String postprocessorClassName);
void postprocessor(String postprocessorClassName, String registrationName);
void postprocessor(RubyClass postprocessorClassName);
void postprocessor(RubyClass postprocessorClassName, String registrationName);
void postprocessor(Postprocessor postprocessor);

void postprocessor(Postprocessor postprocessor, String registrationName);

void treeprocessor(String treeprocessor);
void treeprocessor(String treeprocessor, String registrationName);
void treeprocessor(RubyClass treeprocessorClassName);
void treeprocessor(RubyClass treeprocessorClassName, String registrationName);
void treeprocessor(Treeprocessor treeprocessorClassName);

void treeprocessor(Treeprocessor treeprocessorClassName, String registrationName);

void include_processor(String includeProcessorClassName);
void include_processor(String includeProcessorClassName, String registrationName);
void include_processor(RubyClass includeProcessorClassName);
void include_processor(RubyClass includeProcessorClassName, String registrationName);
void include_processor(IncludeProcessor includeProcessor);

void include_processor(IncludeProcessor includeProcessor, String registrationName);

void block_processor(String blockClassName, Object blockName);
void block_processor(String blockClassName, Object blockName, String registrationName);
void block_processor(RubyClass blockClass, Object blockName);
void block_processor(RubyClass blockClass, Object blockName, String registrationName);
void block_processor(BlockProcessor blockInstance, Object blockName);

void block_processor(BlockProcessor blockInstance, Object blockName, String registrationName);

void block_macro(String blockMacroClassName, Object blockName);
void block_macro(String blockMacroClassName, Object blockName, String registrationName);
void block_macro(Class<BlockMacroProcessor> blockMacroClass, Object blockName);
void block_macro(Class<BlockMacroProcessor> blockMacroClass, Object blockName, String registrationName);
void block_macro(BlockMacroProcessor blockMacroInstance, Object blockName);

void block_macro(BlockMacroProcessor blockMacroInstance, Object blockName, String registrationName);

void inline_macro(String blockClassName, Object blockSymbol);
void inline_macro(String blockClassName, Object blockSymbol, String registrationName);
void inline_macro(RubyClass blockClassName, Object blockSymbol);
void inline_macro(RubyClass blockClassName, Object blockSymbol, String registrationName);
void inline_macro(InlineMacroProcessor blockClassName, Object blockSymbol);

void inline_macro(InlineMacroProcessor blockClassName, Object blockSymbol, String registrationName);

void docinfo_processor(String docInfoClassName);
void docinfo_processor(String docInfoClassName, String registrationName);
void docinfo_processor(RubyClass docInfoClassName);
void docinfo_processor(RubyClass docInfoClassName, String registrationName);
void docinfo_processor(DocinfoProcessor docInfoClassName);

void docinfo_processor(DocinfoProcessor docInfoClassName, String registrationName);

void unregister_all_extensions();
void unregister_extension(String groupName);

Object convert(String content, Map<String, Object> options);
Object convertFile(String filename, Map<String, Object> options);
Expand Down
Loading

0 comments on commit 373b13d

Please sign in to comment.