-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes issue #90 by allowing register Ruby and Java extensions from As…
…ciidoctorJ
- Loading branch information
1 parent
ec5a8f8
commit fb4da9b
Showing
8 changed files
with
154 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/org/asciidoctor/extension/RubyExtensionRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/test/java/org/asciidoctor/extension/WhenRubyExtensionIsRegistered.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |