Skip to content

Commit

Permalink
Fixes asciidoctor#359. Explicit unregistration of extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpanzer committed Jul 19, 2017
1 parent 7e1f30d commit ccf7bfa
Show file tree
Hide file tree
Showing 9 changed files with 415 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,13 @@ String[] convertFiles(Collection<File> asciidoctorFiles,
* Unregister all registered extensions.
*/
void unregisterAllExtensions();


/**
* Unregisters the extension represented by the given registration name.
* @param registrationName
*/
void unregisterExtension(String registrationName);

/**
* 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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,18 @@ public RubyExtensionRegistry preprocessor(String preprocessor) {
return this;
}

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

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

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

Expand All @@ -43,34 +53,69 @@ public RubyExtensionRegistry docinfoProcessor(String docinfoProcessor) {
return this;
}

public RubyExtensionRegistry docinfoProcessor(String docinfoProcessor, String registrationName) {
this.asciidoctorModule.docinfo_processor(docinfoProcessor, registrationName);
return this;
}

public RubyExtensionRegistry includeProcessor(String includeProcessor) {
this.asciidoctorModule.include_processor(includeProcessor);
return this;
}

public RubyExtensionRegistry includeProcessor(String includeProcessor, String registrationName) {
this.asciidoctorModule.include_processor(includeProcessor, registrationName);
return this;
}

public RubyExtensionRegistry treeprocessor(String treeProcessor) {
this.asciidoctorModule.treeprocessor(treeProcessor);
return this;
}

public RubyExtensionRegistry treeprocessor(String treeProcessor, String registrationName) {
this.asciidoctorModule.treeprocessor(treeProcessor, registrationName);
return this;
}

public RubyExtensionRegistry block(String blockName, String blockProcessor) {
this.asciidoctorModule.block_processor(
blockProcessor, RubyUtils.toSymbol(rubyRuntime, blockName));
return this;
}

public RubyExtensionRegistry block(String blockName, String blockProcessor, String registrationName) {
this.asciidoctorModule.block_processor(
blockProcessor, RubyUtils.toSymbol(rubyRuntime, blockName), registrationName);
return this;
}

public RubyExtensionRegistry blockMacro(String blockName, String blockMacroProcessor) {

this.asciidoctorModule.block_macro(
blockMacroProcessor, RubyUtils.toSymbol(rubyRuntime, blockName));
return this;
}

public RubyExtensionRegistry blockMacro(String blockName, String blockMacroProcessor, String registrationName) {

this.asciidoctorModule.block_macro(
blockMacroProcessor, RubyUtils.toSymbol(rubyRuntime, blockName), registrationName);
return this;
}

public RubyExtensionRegistry inlineMacro(String blockName, String inlineMacroProcessor) {

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

public RubyExtensionRegistry inlineMacro(String blockName, String inlineMacroProcessor, String registrationName) {

this.asciidoctorModule.inline_macro(
inlineMacroProcessor, RubyUtils.toSymbol(rubyRuntime, blockName), registrationName);
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 registrationName);

Object convert(String content, Map<String, Object> options);
Object convertFile(String filename, Map<String, Object> options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,11 @@ public void unregisterAllExtensions() {
this.asciidoctorModule.unregister_all_extensions();
}

@Override
public void unregisterExtension(String registrationName) {
this.asciidoctorModule.unregister_extension(registrationName);
}

@Override
public void shutdown() {
this.rubyRuntime.tearDown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module Extensions
include_package 'org.asciidoctor.extension'
# Treeprocessor was renamed in to TreeProcessor in https://github.com/asciidoctor/asciidoctor/commit/f1dd816ade9db457b899581841e4cf7b788aa26d
# This is necessary to run against both Asciidoctor 1.5.5 and 1.5.6
TreeProcessor = Treeprocessor
if !defined? TreeProcessor
TreeProcessor = Treeprocessor
end
end
end

Expand All @@ -19,50 +21,54 @@ def unregister_all_extensions()
Asciidoctor::Extensions.unregister_all
end

def docinfo_processor(extensionName)
Asciidoctor::Extensions.register do
def unregister_extension name
Asciidoctor::Extensions.unregister name
end

def docinfo_processor(extensionName, registrationName = nil)
Asciidoctor::Extensions.register (registrationName != nil ? registrationName.to_sym : nil) do
docinfo_processor extensionName
end
end

def treeprocessor(extensionName)
Asciidoctor::Extensions.register do
def treeprocessor(extensionName, registrationName = nil)
Asciidoctor::Extensions.register (registrationName != nil ? registrationName.to_sym : nil) do
treeprocessor extensionName
end
end

def include_processor(extensionName)
Asciidoctor::Extensions.register do
def include_processor(extensionName, registrationName = nil)
Asciidoctor::Extensions.register (registrationName != nil ? registrationName.to_sym : nil) do
include_processor extensionName
end
end

def preprocessor(extensionName)
Asciidoctor::Extensions.register do
def preprocessor(extensionName, registrationName = nil)
Asciidoctor::Extensions.register (registrationName != nil ? registrationName.to_sym : nil) do
preprocessor extensionName
end
end

def postprocessor(extensionName)
Asciidoctor::Extensions.register do
def postprocessor(extensionName, registrationName = nil)
Asciidoctor::Extensions.register (registrationName != nil ? registrationName.to_sym : nil) do
postprocessor extensionName
end
end

def block_processor(extensionName, blockSymbol)
Asciidoctor::Extensions.register do
def block_processor(extensionName, blockSymbol, registrationName = nil)
Asciidoctor::Extensions.register (registrationName != nil ? registrationName.to_sym : nil) do
block extensionName, blockSymbol
end
end

def block_macro(extensionName, blockSymbol)
Asciidoctor::Extensions.register do
def block_macro(extensionName, blockSymbol, registrationName = nil)
Asciidoctor::Extensions.register (registrationName != nil ? registrationName.to_sym : nil) do
block_macro extensionName, blockSymbol
end
end

def inline_macro(extensionName, blockSymbol)
Asciidoctor::Extensions.register do
def inline_macro(extensionName, blockSymbol, registrationName = nil)
Asciidoctor::Extensions.register (registrationName != nil ? registrationName.to_sym : nil) do
inline_macro extensionName, blockSymbol
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.asciidoctor.Asciidoctor;
import org.asciidoctor.AttributesBuilder;
Expand Down Expand Up @@ -700,4 +701,69 @@ public void a_block_processor_instance_should_be_executed_when_registered_block_

}

@Test
public void should_unregister_postprocessor() throws IOException {

// Given: A registered Postprocessor
JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry();
final String registrationName = UUID.randomUUID().toString();
javaExtensionRegistry.postprocessor(CustomFooterPostProcessor.class, registrationName);

// When: I render a document
Options options = options().inPlace(false).toFile(new File(testFolder.getRoot(), "rendersample.html"))
.safe(SafeMode.UNSAFE).get();

asciidoctor.renderFile(classpath.getResource("rendersample.asciidoc"), options);

// Then: it is invoked
File renderedFile = new File(testFolder.getRoot(), "rendersample.html");
org.jsoup.nodes.Document doc = Jsoup.parse(renderedFile, "UTF-8");
Element footer = doc.getElementById("footer-text");
assertThat(footer.text(), containsString("Copyright Acme, Inc."));

// When: I unregister the Postprocessor and render again with the same Asciidoctor instance
asciidoctor.unregisterExtension(registrationName);

Options options2 = options().inPlace(false).toFile(new File(testFolder.getRoot(), "rendersample2.html"))
.safe(SafeMode.UNSAFE).get();
asciidoctor.renderFile(classpath.getResource("rendersample.asciidoc"), options2);
File renderedFile2 = new File(testFolder.getRoot(), "rendersample2.html");
org.jsoup.nodes.Document doc2 = Jsoup.parse(renderedFile2, "UTF-8");

Element footer2 = doc2.getElementById("footer-text");
assertThat(footer2.text(), not(containsString("Copyright Acme, Inc.")));
}

@Test
public void should_unregister_block_processor()
throws IOException {

JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry();
final String registrationName = UUID.randomUUID().toString();

Map<String, Object> config = new HashMap<String, Object>();
config.put("contexts", Arrays.asList(":paragraph"));
config.put("content_model", ":simple");
YellBlock yellBlock = new YellBlock("yell", config);
javaExtensionRegistry.block(yellBlock, registrationName);
String content = asciidoctor.renderFile(
classpath.getResource("sample-with-yell-block.ad"),
options().toFile(false).get());
Document doc = Jsoup.parse(content, "UTF-8");
Elements elements = doc.getElementsByClass("paragraph");
assertThat(elements.size(), is(1));
assertThat(elements.get(0).text(), is("THE TIME IS NOW. GET A MOVE ON."));

asciidoctor.unregisterExtension(registrationName);
String contentWithoutBlock = asciidoctor.renderFile(
classpath.getResource("sample-with-yell-block.ad"),
options().toFile(false).get());
Document docWithoutBlock = Jsoup.parse(contentWithoutBlock, "UTF-8");
Elements elementsWithoutBlock = docWithoutBlock.getElementsByClass("paragraph");
assertThat(elementsWithoutBlock.size(), is(1));
assertThat(elementsWithoutBlock.get(0).text(), not(is("THE TIME IS NOW. GET A MOVE ON.")));

}


}
Loading

0 comments on commit ccf7bfa

Please sign in to comment.