Skip to content

Commit

Permalink
fixes issue #90 by allowing register Ruby and Java extensions from As…
Browse files Browse the repository at this point in the history
…ciidoctorJ
  • Loading branch information
lordofthejars committed Oct 26, 2013
1 parent ec5a8f8 commit fb4da9b
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 32 deletions.
12 changes: 10 additions & 2 deletions src/main/java/org/asciidoctor/Asciidoctor.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import java.util.Collection;
import java.util.Map;

import org.asciidoctor.extension.ExtensionRegistry;
import org.asciidoctor.extension.JavaExtensionRegistry;
import org.asciidoctor.extension.RubyExtensionRegistry;
import org.asciidoctor.internal.DocumentRuby;
import org.asciidoctor.internal.JRubyAsciidoctor;

Expand Down Expand Up @@ -304,7 +305,13 @@ String[] renderFiles(Collection<File> asciidoctorFiles,
* Creates an extension registry ready to be used for registering all processors
* @return Extension Registry object.
*/
ExtensionRegistry extensionRegistry();
JavaExtensionRegistry javaExtensionRegistry();

/**
* Creates an Ruby extension registry ready to be used for registering all processors
* @return Extension Registry object.
*/
RubyExtensionRegistry rubyExtensionRegistry();

/**
* Factory for creating a new instance of Asciidoctor interface.
Expand Down Expand Up @@ -340,4 +347,5 @@ public static Asciidoctor create(String gemPath) {
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import org.asciidoctor.internal.RubyUtils;
import org.jruby.Ruby;

public class ExtensionRegistry {
public class JavaExtensionRegistry {

private AsciidoctorModule asciidoctorModule;
private Ruby rubyRuntime;

public ExtensionRegistry(AsciidoctorModule asciidoctorModule, Ruby rubyRuntime) {
public JavaExtensionRegistry(AsciidoctorModule asciidoctorModule, Ruby rubyRuntime) {
super();
this.asciidoctorModule = asciidoctorModule;
this.rubyRuntime = rubyRuntime;
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/org/asciidoctor/extension/RubyExtensionRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.asciidoctor.extension;

import org.asciidoctor.internal.AsciidoctorModule;
import org.asciidoctor.internal.RubyUtils;
import org.jruby.Ruby;

public class RubyExtensionRegistry {

private AsciidoctorModule asciidoctorModule;
private Ruby rubyRuntime;

public RubyExtensionRegistry(AsciidoctorModule asciidoctorModule,
Ruby rubyRuntime) {
super();
this.asciidoctorModule = asciidoctorModule;
this.rubyRuntime = rubyRuntime;
}

public void preprocessor(String preprocessor) {
this.asciidoctorModule.preprocessor(preprocessor);
}

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

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

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

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

public void blockMacro(String blockName, String blockMacroProcessor) {

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

public void inlineMacro(String blockName, String inlineMacroProcessor) {

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

}
20 changes: 8 additions & 12 deletions src/main/java/org/asciidoctor/internal/JRubyAsciidoctor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,12 @@
import java.util.Map;

import org.asciidoctor.Asciidoctor;
import org.asciidoctor.Attributes;
import org.asciidoctor.DirectoryWalker;
import org.asciidoctor.DocumentHeader;
import org.asciidoctor.Options;
import org.asciidoctor.OptionsBuilder;
import org.asciidoctor.extension.BlockMacroProcessor;
import org.asciidoctor.extension.BlockProcessor;
import org.asciidoctor.extension.ExtensionRegistry;
import org.asciidoctor.extension.IncludeProcessor;
import org.asciidoctor.extension.InlineMacroProcessor;
import org.asciidoctor.extension.Postprocessor;
import org.asciidoctor.extension.Preprocessor;
import org.asciidoctor.extension.Treeprocessor;
import org.asciidoctor.extension.JavaExtensionRegistry;
import org.asciidoctor.extension.RubyExtensionRegistry;
import org.jruby.CompatVersion;
import org.jruby.Ruby;
import org.jruby.RubyHash;
Expand Down Expand Up @@ -290,10 +283,13 @@ public String[] renderFiles(Collection<File> asciidoctorFiles,
}

@Override
public ExtensionRegistry extensionRegistry() {
return new ExtensionRegistry(asciidoctorModule, rubyRuntime);
public JavaExtensionRegistry javaExtensionRegistry() {
return new JavaExtensionRegistry(asciidoctorModule, rubyRuntime);
}


@Override
public RubyExtensionRegistry rubyExtensionRegistry() {
return new RubyExtensionRegistry(asciidoctorModule, rubyRuntime);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.asciidoctor.OptionsBuilder.options;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;

Expand All @@ -20,7 +21,7 @@
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class WhenExtensionIsRegistered {
public class WhenJavaExtensionIsRegistered {

@Rule
public TemporaryFolder testFolder = new TemporaryFolder();
Expand All @@ -30,9 +31,9 @@ public class WhenExtensionIsRegistered {
@Test
public void a_preprocessor_should_be_executed_before_document_is_rendered() {

ExtensionRegistry extensionRegistry = this.asciidoctor.extensionRegistry();
JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry();

extensionRegistry.preprocessor(FrontMatterPreprocessorExtension.class);
javaExtensionRegistry.preprocessor(FrontMatterPreprocessorExtension.class);

String content = asciidoctor.renderFile(new File(
"target/test-classes/render-with-front-matter.adoc"),
Expand All @@ -50,9 +51,9 @@ public void a_preprocessor_should_be_executed_before_document_is_rendered() {
@Test
public void a_postprocessor_should_be_executed_after_document_is_rendered() throws IOException {

ExtensionRegistry extensionRegistry = this.asciidoctor.extensionRegistry();
JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry();

extensionRegistry.postprocessor(CustomFooterPostProcessor.class);
javaExtensionRegistry.postprocessor(CustomFooterPostProcessor.class);

Options options = options().inPlace(false)
.toFile(new File(testFolder.getRoot(), "rendersample.html"))
Expand All @@ -72,9 +73,9 @@ public void a_postprocessor_should_be_executed_after_document_is_rendered() thro
@Test
public void a_include_processor_should_be_executed_when_include_macro_is_found() {

ExtensionRegistry extensionRegistry = this.asciidoctor.extensionRegistry();
JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry();

extensionRegistry.includeProcessor(UriIncludeProcessor.class);
javaExtensionRegistry.includeProcessor(UriIncludeProcessor.class);

String content = asciidoctor.renderFile(new File(
"target/test-classes/sample-with-uri-include.ad"),
Expand All @@ -85,16 +86,16 @@ public void a_include_processor_should_be_executed_when_include_macro_is_found()
Element contentElement = doc.getElementsByAttributeValue("class",
"ruby language-ruby").first();

assertThat(contentElement.text(), is("source 'https://rubygems.org'gemspec# enable this group to use Guard for continuous testing# after removing comments, run `bundle install` then `guard` #group :guardtest do# gem 'guard'# gem 'guard-test'# gem 'libnotify'# gem 'listen', :github => 'guard/listen'#end"));
assertThat(contentElement.text(), startsWith("source 'https://rubygems.org"));

}

@Test
public void a_treeprocessor_should_be_executed_in_document() {

ExtensionRegistry extensionRegistry = this.asciidoctor.extensionRegistry();
JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry();

extensionRegistry.treeprocessor(TerminalCommandTreeprocessor.class);
javaExtensionRegistry.treeprocessor(TerminalCommandTreeprocessor.class);

String content = asciidoctor.renderFile(new File(
"target/test-classes/sample-with-terminal-command.ad"),
Expand All @@ -115,9 +116,9 @@ public void a_treeprocessor_should_be_executed_in_document() {
@Test
public void a_block_macro_extension_should_be_executed_when_macro_is_detected() {

ExtensionRegistry extensionRegistry = this.asciidoctor.extensionRegistry();
JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry();

extensionRegistry.blockMacro("gist", GistMacro.class);
javaExtensionRegistry.blockMacro("gist", GistMacro.class);

String content = asciidoctor.renderFile(new File(
"target/test-classes/sample-with-gist-macro.ad"),
Expand All @@ -132,9 +133,9 @@ public void a_block_macro_extension_should_be_executed_when_macro_is_detected()
@Test
public void an_inline_macro_extension_should_be_executed_when_an_inline_macro_is_detected() {

ExtensionRegistry extensionRegistry = this.asciidoctor.extensionRegistry();
JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry();

extensionRegistry.inlineMacro("man", ManpageMacro.class);
javaExtensionRegistry.inlineMacro("man", ManpageMacro.class);

String content = asciidoctor.renderFile(new File(
"target/test-classes/sample-with-man-link.ad"),
Expand All @@ -149,8 +150,8 @@ public void an_inline_macro_extension_should_be_executed_when_an_inline_macro_is
@Test
public void a_block_processor_should_be_executed_when_registered_block_is_found_in_document() throws IOException {

ExtensionRegistry extensionRegistry = this.asciidoctor.extensionRegistry();
extensionRegistry.block("yell", YellBlock.class);
JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry();
javaExtensionRegistry.block("yell", YellBlock.class);

String content = asciidoctor.renderFile(new File(
"target/test-classes/sample-with-yell-block.ad"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.asciidoctor.extension;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.io.File;

import org.asciidoctor.Asciidoctor;
import org.asciidoctor.Options;
import org.asciidoctor.internal.IOUtils;
import org.asciidoctor.internal.JRubyAsciidoctor;
import org.asciidoctor.internal.JRubyRuntimeContext;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.junit.Test;

public class WhenRubyExtensionIsRegistered {

private Asciidoctor asciidoctor = JRubyAsciidoctor.create();

@Test
public void ruby_extension_should_be_registered() {

loadRubyClassExtension();

RubyExtensionRegistry rubyExtensionRegistry = this.asciidoctor.rubyExtensionRegistry();
rubyExtensionRegistry.block("rubyyell", "YellRubyBlock");

String content = asciidoctor.renderFile(new File(
"target/test-classes/sample-with-ruby-yell-block.ad"),
new Options());
Document doc = Jsoup.parse(content, "UTF-8");
Elements elements = doc.getElementsByClass("paragraph");
assertThat(elements.size(), is(2));
assertThat(elements.get(1).text(), is("THE TIME IS NOW! GET A MOVE ON!"));

}

private void loadRubyClassExtension() {
String script = IOUtils.readFull(Class.class.getResourceAsStream("/YellRubyBlock.rb"));
JRubyRuntimeContext.get().evalScriptlet(script);
}

}
12 changes: 12 additions & 0 deletions src/test/resources/YellRubyBlock.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'asciidoctor'
require 'asciidoctor/extensions'

class YellRubyBlock < Asciidoctor::Extensions::BlockProcessor
option :contexts, [:paragraph]
option :content_model, :simple

def process parent, reader, attributes
lines = reader.lines.map {|line| line.upcase.gsub(/\.( |$)/, '!\\1') }
Asciidoctor::Block.new parent, :paragraph, :source => lines, :attributes => attributes
end
end
6 changes: 6 additions & 0 deletions src/test/resources/sample-with-ruby-yell-block.ad
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
= Block Yell Example

content

[rubyyell]
The time is now. Get a move on.

0 comments on commit fb4da9b

Please sign in to comment.