Added more tests for Ruby extensions
MODIFY
asciidoctorj-core/src/main/java/org/asciidoctor/converter/JavaConverterRegistry.java
asciidoctorj-core/src/main/java/org/asciidoctor/converter/JavaConverterRegistry.java
package org.asciidoctor.converter;
-import org.asciidoctor.internal.AsciidoctorModule;
-import org.asciidoctor.internal.RubyUtils;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyClass;
-import org.jruby.RubyString;
-import org.jruby.runtime.builtin.IRubyObject;
import java.util.HashMap;
import java.util.Map;
@@ -27,16 +23,12 @@ public class JavaConverterRegistry {
if (converterForAnnotation != null) {
// Backend annotation present => Register with name given in annotation
String backend = !ConverterFor.UNDEFINED.equals(converterForAnnotation.format()) ? converterForAnnotation.format() : converterForAnnotation.value();
- rubyRuntime.getModule("Asciidoctor")
- .defineOrGetModuleUnder("Converter")
- .getClass("Factory")
+ getConverterFactory()
.callMethod("register", clazz, rubyRuntime.newArray(rubyRuntime.newString(backend)));
} else if (backends.length == 0) {
// No backend annotation and no backend defined => register as default backend
- rubyRuntime.getModule("Asciidoctor")
- .defineOrGetModuleUnder("Converter")
- .getClass("Factory")
+ getConverterFactory()
.callMethod("register", clazz);
}
if (backends.length > 0) {
@@ -45,17 +37,13 @@ public class JavaConverterRegistry {
for (String backend: backends) {
rubyBackendNames.add(rubyRuntime.newString(backend));
}
- rubyRuntime.getModule("Asciidoctor")
- .defineOrGetModuleUnder("Converter")
- .getClass("Factory")
+ getConverterFactory()
.callMethod("register", clazz, rubyBackendNames);
}
}
public Class<?> resolve(String backend) {
- RubyClass rubyClass = (RubyClass) rubyRuntime.getModule("Asciidoctor")
- .defineOrGetModuleUnder("Converter")
- .getClass("Factory")
+ RubyClass rubyClass = (RubyClass) getConverterFactory()
.callMethod("resolve", rubyRuntime.newString(backend));
Class<?> clazz = rubyClass.getReifiedClass();
@@ -69,16 +57,18 @@ public class JavaConverterRegistry {
}
public void unregisterAll() {
- rubyRuntime.getModule("Asciidoctor")
- .defineOrGetModuleUnder("Converter")
- .getClass("Factory")
+ getConverterFactory()
.callMethod("unregister_all");
}
- public Map<String, Class<?>> converters() {
- final RubyArray rubyKeys = (RubyArray) rubyRuntime.getModule("Asciidoctor")
+ private RubyClass getConverterFactory() {
+ return rubyRuntime.getModule("Asciidoctor")
.defineOrGetModuleUnder("Converter")
- .getClass("Factory")
+ .getClass("Factory");
+ }
+
+ public Map<String, Class<?>> converters() {
+ final RubyArray rubyKeys = (RubyArray) getConverterFactory()
.callMethod("converters")
.callMethod(rubyRuntime.getCurrentContext(), "keys");

MODIFY
asciidoctorj-core/src/main/java/org/asciidoctor/extension/JavaExtensionRegistry.java
asciidoctorj-core/src/main/java/org/asciidoctor/extension/JavaExtensionRegistry.java
@@ -26,14 +26,14 @@ public class JavaExtensionRegistry {
public JavaExtensionRegistry docinfoProcessor(Class<? extends DocinfoProcessor> docInfoProcessor) {
RubyClass rubyClass = DocinfoProcessorProxy.register(rubyRuntime, docInfoProcessor);
- rubyRuntime.getModule("AsciidoctorModule")
+ getAsciidoctorModule()
.callMethod( "docinfo_processor", rubyClass);
return this;
}
public JavaExtensionRegistry docinfoProcessor(DocinfoProcessor docInfoProcessor) {
RubyClass rubyClass = DocinfoProcessorProxy.register(rubyRuntime, docInfoProcessor);
- rubyRuntime.getModule("AsciidoctorModule")
+ getAsciidoctorModule()
.callMethod( "docinfo_processor", rubyClass);
return this;
}
@@ -50,7 +50,7 @@ public class JavaExtensionRegistry {
public JavaExtensionRegistry preprocessor(Class<? extends Preprocessor> preprocessor) {
RubyClass rubyClass = PreprocessorProxy.register(rubyRuntime, preprocessor);
- rubyRuntime.getModule("AsciidoctorModule")
+ getAsciidoctorModule()
.callMethod( "preprocessor", rubyClass);
return this;
}
@@ -58,7 +58,7 @@ public class JavaExtensionRegistry {
public JavaExtensionRegistry preprocessor(Preprocessor preprocessor) {
RubyClass rubyClass = PreprocessorProxy.register(rubyRuntime, preprocessor);
- rubyRuntime.getModule("AsciidoctorModule")
+ getAsciidoctorModule()
.callMethod( "preprocessor", rubyClass);
return this;
}
@@ -85,14 +85,14 @@ public class JavaExtensionRegistry {
public JavaExtensionRegistry postprocessor(Class<? extends Postprocessor> postprocessor) {
RubyClass rubyClass = PostprocessorProxy.register(rubyRuntime, postprocessor);
- rubyRuntime.getModule("AsciidoctorModule")
+ getAsciidoctorModule()
.callMethod( "postprocessor", rubyClass);
return this;
}
public JavaExtensionRegistry postprocessor(Postprocessor postprocessor) {
RubyClass rubyClass = PostprocessorProxy.register(rubyRuntime, postprocessor);
- rubyRuntime.getModule("AsciidoctorModule")
+ getAsciidoctorModule()
.callMethod( "postprocessor", rubyClass);
return this;
}
@@ -110,28 +110,28 @@ public class JavaExtensionRegistry {
public JavaExtensionRegistry includeProcessor(
Class<? extends IncludeProcessor> includeProcessor) {
RubyClass rubyClass = IncludeProcessorProxy.register(rubyRuntime, includeProcessor);
- rubyRuntime.getModule("AsciidoctorModule")
+ getAsciidoctorModule()
.callMethod( "include_processor", rubyClass);
return this;
}
public JavaExtensionRegistry includeProcessor(IncludeProcessor includeProcessor) {
RubyClass rubyClass = IncludeProcessorProxy.register(rubyRuntime, includeProcessor);
- rubyRuntime.getModule("AsciidoctorModule")
+ getAsciidoctorModule()
.callMethod( "include_processor", rubyClass);
return this;
}
public JavaExtensionRegistry treeprocessor(Treeprocessor treeprocessor) {
RubyClass rubyClass = TreeprocessorProxy.register(rubyRuntime, treeprocessor);
- rubyRuntime.getModule("AsciidoctorModule")
+ getAsciidoctorModule()
.callMethod( "treeprocessor", rubyClass);
return this;
}
public JavaExtensionRegistry treeprocessor(Class<? extends Treeprocessor> abstractTreeProcessor) {
RubyClass rubyClass = TreeprocessorProxy.register(rubyRuntime, abstractTreeProcessor);
- rubyRuntime.getModule("AsciidoctorModule")
+ getAsciidoctorModule()
.callMethod( "treeprocessor", rubyClass);
return this;
}
@@ -170,7 +170,7 @@ public class JavaExtensionRegistry {
public JavaExtensionRegistry block(String blockName,
Class<? extends BlockProcessor> blockProcessor) {
RubyClass rubyClass = BlockProcessorProxy.register(rubyRuntime, blockProcessor);
- rubyRuntime.getModule("AsciidoctorModule")
+ getAsciidoctorModule()
.callMethod( "block_processor", rubyClass, rubyRuntime.newString(blockName));
return this;
}
@@ -183,7 +183,7 @@ public class JavaExtensionRegistry {
public JavaExtensionRegistry block(BlockProcessor blockProcessor) {
RubyClass rubyClass = BlockProcessorProxy.register(rubyRuntime, blockProcessor);
- rubyRuntime.getModule("AsciidoctorModule")
+ getAsciidoctorModule()
.callMethod( "block_processor", rubyClass, rubyRuntime.newString(blockProcessor.getName()));
return this;
}
@@ -191,7 +191,7 @@ public class JavaExtensionRegistry {
public JavaExtensionRegistry block(String blockName,
BlockProcessor blockProcessor) {
RubyClass rubyClass = BlockProcessorProxy.register(rubyRuntime, blockProcessor);
- rubyRuntime.getModule("AsciidoctorModule")
+ getAsciidoctorModule()
.callMethod( "block_processor", rubyClass, rubyRuntime.newString(blockName));
return this;
}
@@ -199,7 +199,7 @@ public class JavaExtensionRegistry {
public JavaExtensionRegistry blockMacro(String blockName,
Class<? extends BlockMacroProcessor> blockMacroProcessor) {
RubyClass rubyClass = BlockMacroProcessorProxy.register(rubyRuntime, blockMacroProcessor);
- rubyRuntime.getModule("AsciidoctorModule")
+ getAsciidoctorModule()
.callMethod( "block_macro", rubyClass, rubyRuntime.newString(blockName));
return this;
}
@@ -207,7 +207,7 @@ public class JavaExtensionRegistry {
public JavaExtensionRegistry blockMacro(Class<? extends BlockMacroProcessor> blockMacroProcessor) {
String name = getName(blockMacroProcessor);
RubyClass rubyClass = BlockMacroProcessorProxy.register(rubyRuntime, blockMacroProcessor);
- rubyRuntime.getModule("AsciidoctorModule")
+ getAsciidoctorModule()
.callMethod( "block_macro", rubyClass, rubyRuntime.newString(name));
return this;
}
@@ -235,14 +235,14 @@ public class JavaExtensionRegistry {
public JavaExtensionRegistry blockMacro(BlockMacroProcessor blockMacroProcessor) {
RubyClass rubyClass = BlockMacroProcessorProxy.register(rubyRuntime, blockMacroProcessor);
- rubyRuntime.getModule("AsciidoctorModule")
+ getAsciidoctorModule()
.callMethod( "block_macro", rubyClass, rubyRuntime.newString(blockMacroProcessor.getName()));
return this;
}
public JavaExtensionRegistry inlineMacro(InlineMacroProcessor inlineMacroProcessor) {
RubyClass rubyClass = InlineMacroProcessorProxy.register(rubyRuntime, inlineMacroProcessor);
- rubyRuntime.getModule("AsciidoctorModule")
+ getAsciidoctorModule()
.callMethod( "inline_macro", rubyClass, rubyRuntime.newString(inlineMacroProcessor.getName()));
return this;
}
@@ -250,7 +250,7 @@ public class JavaExtensionRegistry {
public JavaExtensionRegistry inlineMacro(String blockName,
Class<? extends InlineMacroProcessor> inlineMacroProcessor) {
RubyClass rubyClass = InlineMacroProcessorProxy.register(rubyRuntime, inlineMacroProcessor);
- rubyRuntime.getModule("AsciidoctorModule")
+ getAsciidoctorModule()
.callMethod( "inline_macro", rubyClass, rubyRuntime.newString(blockName));
return this;
}
@@ -258,7 +258,7 @@ public class JavaExtensionRegistry {
public JavaExtensionRegistry inlineMacro(Class<? extends InlineMacroProcessor> inlineMacroProcessor) {
String name = getName(inlineMacroProcessor);
RubyClass rubyClass = InlineMacroProcessorProxy.register(rubyRuntime, inlineMacroProcessor);
- rubyRuntime.getModule("AsciidoctorModule")
+ getAsciidoctorModule()
.callMethod( "inline_macro", rubyClass, rubyRuntime.newString(name));
return this;
}
@@ -290,4 +290,8 @@ public class JavaExtensionRegistry {
}
return nameAnnotation.value();
}
+
+ private RubyModule getAsciidoctorModule() {
+ return rubyRuntime.getModule("AsciidoctorModule");
+ }
}

DELETE
asciidoctorj-core/src/main/java/org/asciidoctor/internal/AsciidoctorModule.java
-package org.asciidoctor.internal;
-
-import java.util.Map;
-
-import org.asciidoctor.ast.Document;
-import org.asciidoctor.extension.*;
-import org.jruby.RubyArray;
-import org.jruby.RubyClass;
-
-
-public interface AsciidoctorModule {
-
- void preprocessor(String preprocessorClassName);
- void preprocessor(RubyClass preprocessorClassName);
- void preprocessor(Preprocessor preprocessor);
-
- void postprocessor(String postprocessorClassName);
- void postprocessor(RubyClass postprocessorClassName);
- void postprocessor(Postprocessor postprocessor);
-
- void treeprocessor(String treeprocessor);
- void treeprocessor(RubyClass treeprocessorClassName);
- void treeprocessor(Treeprocessor treeprocessorClassName);
-
- void include_processor(String includeProcessorClassName);
- void include_processor(RubyClass includeProcessorClassName);
- void include_processor(IncludeProcessor includeProcessor);
-
- void block_processor(String blockClassName, Object blockName);
- void block_processor(RubyClass blockClass, Object blockName);
- void block_processor(BlockProcessor blockInstance, Object blockName);
-
- void block_macro(String blockMacroClassName, Object blockName);
- void block_macro(Class<BlockMacroProcessor> blockMacroClass, Object blockName);
- void block_macro(BlockMacroProcessor blockMacroInstance, Object blockName);
- void block_macro(RubyClass blockClassName, Object blockSymbol);
-
- void inline_macro(String blockClassName, Object blockSymbol);
- void inline_macro(RubyClass blockClassName, Object blockSymbol);
- void inline_macro(InlineMacroProcessor blockClassName, Object blockSymbol);
-
- void docinfo_processor(String docInfoClassName);
- void docinfo_processor(RubyClass docInfoClassName);
- void docinfo_processor(DocinfoProcessor docInfoClassName);
-
- void register_extension_group(final String groupName, ExtensionGroupImpl extensionGroup);
-
- 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);
-
- Document load_file(String filename, Map<String, Object> options);
- Document load(String content, Map<String, Object> options);
-
- void register_converter(RubyClass converter);
- void register_converter(RubyClass converter, String[] backends);
- RubyClass resolve_converter(String backend);
- RubyArray converters();
- void unregister_all_converters();
-
- String asciidoctorRuntimeEnvironmentVersion();
-
-}

MODIFY
asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenRubyExtensionGroupIsRegistered.java
asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenRubyExtensionGroupIsRegistered.java
package org.asciidoctor.extension;
import org.asciidoctor.Asciidoctor;
+import org.asciidoctor.SafeMode;
import org.asciidoctor.arquillian.api.Unshared;
import org.asciidoctor.util.ClasspathResources;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
+import org.jsoup.nodes.Element;
+import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.junit.runner.RunWith;
-import java.util.Arrays;
+import java.util.Iterator;
+import static java.util.Collections.singletonList;
import static org.asciidoctor.OptionsBuilder.options;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
@RunWith(Arquillian.class)
public class WhenRubyExtensionGroupIsRegistered {
@@ -193,11 +198,33 @@ public class WhenRubyExtensionGroupIsRegistered {
}
+ @Test
+ public void ruby_treeprocessor_should_be_registered() {
+
+ this.asciidoctor.createGroup()
+ .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/shell-session-tree-processor.rb"))
+ .rubyTreeprocessor("ShellSessionTreeProcessor")
+ .register();
+
+ String content = this.asciidoctor.render(
+ " $ echo \"Hello, World!\"\n" +
+ " > Hello, World!\n" +
+ "\n" +
+ " $ gem install asciidoctor",
+ options().toFile(false).get());
+
+ final Document document = Jsoup.parse(content);
+ final TextNode commandElement = document.getElementsByClass("command").get(0).textNodes().get(0);
+ assertThat(commandElement.getWholeText(), is("echo \"Hello, World!\""));
+ final TextNode commandElement2 = document.getElementsByClass("command").get(1).textNodes().get(0);
+ assertThat(commandElement2.getWholeText(), is("gem install asciidoctor"));
+ }
+
@Test
public void ruby_postprocessor_should_be_registered() {
final String rubyExtPath = classpath.getResource("ruby-extensions").getAbsolutePath();
- final Asciidoctor asciidoctor = Asciidoctor.Factory.create(Arrays.asList(rubyExtPath));
+ final Asciidoctor asciidoctor = Asciidoctor.Factory.create(singletonList(rubyExtPath));
asciidoctor.createGroup()
.requireRubyLibrary("xml-entity-postprocessor.rb")
.rubyPostprocessor("XmlEntityPostprocessor")
@@ -211,4 +238,98 @@ public class WhenRubyExtensionGroupIsRegistered {
assertThat(content, containsString("Read &#167;2 and it&#39;ll all be clear."));
}
+ @Test
+ public void ruby_includeprocessor_should_be_registered() {
+ asciidoctor.createGroup()
+ .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/response-include-processor.rb"))
+ .rubyIncludeProcessor("ResponseIncludeProcessor")
+ .register();
+
+ String content = asciidoctor.render(
+ "The response to everything is\n" +
+ "\n" +
+ "include::response[]" +
+ "",
+ options().toFile(false).safe(SafeMode.SAFE).get());
+
+ final Document document = Jsoup.parse(content);
+ assertThat(
+ document.getElementsByClass("paragraph").get(1).getElementsByTag("p").get(0).toString(),
+ is("<p>42</p>"));
+ }
+
+
+
+ @Test
+ public void ruby_preprocessor_should_be_registered() {
+
+ this.asciidoctor.createGroup()
+ .loadRubyClass(getClass().getResourceAsStream("/ruby-extensions/front-matter-preprocessor.rb"))
+ .rubyPreprocessor("FrontMatterPreprocessor")
+ .register();
+
+ String content = this.asciidoctor.render(
+ "---\n" +
+ "tags: [announcement, website]\n" +
+ "---\n" +
+ "= Document Title\n" +
+ "\n" +
+ "content\n" +
+ "\n" +
+ "[subs=\"attributes,specialcharacters\"]\n" +
+ ".Captured front matter\n" +
+ "....\n" +
+ "---\n" +
+ "{front-matter}\n" +
+ "---\n" +
+ "....",
+ options().toFile(false).get());
+
+ final Document document = Jsoup.parse(content);
+ final Element contentElement = document.getElementsByClass("content").get(0);
+ final Element literalElement = contentElement.getElementsByTag("pre").get(0);
+ assertThat(literalElement.toString().replace("\r", ""),
+ containsString("---\n" +
+ "tags: [announcement, website]\n" +
+ "---"));
+ }
+
+ @Test
+ public void ruby_docinfoprocessor_should_be_registered() {
+
+ final String rubyExtPath = classpath.getResource("ruby-extensions").getAbsolutePath();
+ final Asciidoctor asciidoctor = Asciidoctor.Factory.create(singletonList(rubyExtPath));
+ asciidoctor.createGroup()
+ .requireRubyLibrary("view-result-docinfoprocessor.rb")
+ .rubyDocinfoProcessor("ViewResultDocinfoProcessor")
+ .register();
+
+ String content = asciidoctor.render(
+ "= View Result Sample \n" +
+ " \n" +
+ ".This will have a link next to it\n" +
+ "---- \n" +
+ "* always displayed \n" +
+ "* always displayed 2 \n" +
+ "---- \n" +
+ " \n" +
+ "[.result] \n" +
+ "==== \n" +
+ "* hidden till clicked \n" +
+ "* hidden till clicked 2 \n" +
+ "==== ",
+ options().toFile(false).safe(SafeMode.SAFE).headerFooter(true).get());
+
+ final Document document = Jsoup.parse(content);
+ final Iterator<Element> elems = document.getElementsByTag("style").iterator();
+ boolean found = false;
+ while (elems.hasNext()) {
+ final Element styleElem = elems.next();
+ if (styleElem.toString().contains(".listingblock a.view-result")) {
+ found = true;
+ }
+ }
+ assertTrue("Could not find style element that should have been added by docinfo processor:\n" + document, found);
+ }
+
}

MODIFY
asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenRubyExtensionIsRegistered.java
asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenRubyExtensionIsRegistered.java
package org.asciidoctor.extension;
import org.asciidoctor.Asciidoctor;
+import org.asciidoctor.SafeMode;
import org.asciidoctor.arquillian.api.Unshared;
import org.asciidoctor.util.ClasspathResources;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
+import org.jsoup.nodes.Element;
+import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.junit.runner.RunWith;
-import java.util.Arrays;
+import java.util.Iterator;
+import static java.util.Collections.singletonList;
import static org.asciidoctor.OptionsBuilder.options;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
@RunWith(Arquillian.class)
public class WhenRubyExtensionIsRegistered {
@@ -130,7 +135,8 @@ public class WhenRubyExtensionIsRegistered {
public void ruby_inline_macro_processor_should_be_registered_with_implicit_macro_name() {
RubyExtensionRegistry rubyExtensionRegistry = this.asciidoctor.rubyExtensionRegistry();
- rubyExtensionRegistry.loadClass(getClass().getResourceAsStream("/ruby-extensions/man-inline-macro.rb")).inlineMacro("ManInlineMacro");
+ rubyExtensionRegistry.loadClass(getClass().getResourceAsStream("/ruby-extensions/man-inline-macro.rb"))
+ .inlineMacro("ManInlineMacro");
String content = asciidoctor.render(
"= Man Inline Macro Extension\n" +
@@ -145,12 +151,53 @@ public class WhenRubyExtensionIsRegistered {
}
+ @Test
+ public void ruby_treeprocessor_should_be_registered() {
+
+ final String rubyExtPath = classpath.getResource("ruby-extensions").getAbsolutePath();
+ final Asciidoctor asciidoctor = Asciidoctor.Factory.create(singletonList(rubyExtPath));
+ asciidoctor.rubyExtensionRegistry()
+ .requireLibrary("shell-session-tree-processor.rb")
+ .treeprocessor("ShellSessionTreeProcessor");
+
+ String content = asciidoctor.render(
+ " $ echo \"Hello, World!\"\n" +
+ " > Hello, World!\n" +
+ "\n" +
+ " $ gem install asciidoctor",
+ options().toFile(false).get());
+
+ final Document document = Jsoup.parse(content);
+ final TextNode commandElement = document.getElementsByClass("command").get(0).textNodes().get(0);
+ assertThat(commandElement.getWholeText(), is("echo \"Hello, World!\""));
+ final TextNode commandElement2 = document.getElementsByClass("command").get(1).textNodes().get(0);
+ assertThat(commandElement2.getWholeText(), is("gem install asciidoctor"));
+ }
+
+ @Test
+ public void ruby_includeprocessor_should_be_registered() {
+ asciidoctor.rubyExtensionRegistry()
+ .loadClass(getClass().getResourceAsStream("/ruby-extensions/response-include-processor.rb"))
+ .includeProcessor("ResponseIncludeProcessor");
+
+ String content = asciidoctor.render(
+ "The response to everything is\n" +
+ "\n" +
+ "include::response[]" +
+ "",
+ options().toFile(false).safe(SafeMode.SAFE).get());
+
+ final Document document = Jsoup.parse(content);
+ assertThat(
+ document.getElementsByClass("paragraph").get(1).getElementsByTag("p").get(0).toString(),
+ is("<p>42</p>"));
+ }
+
@Test
public void ruby_postprocessor_should_be_registered() {
final String rubyExtPath = classpath.getResource("ruby-extensions").getAbsolutePath();
- final Asciidoctor asciidoctor = Asciidoctor.Factory.create(Arrays.asList(rubyExtPath));
- RubyExtensionRegistry rubyExtensionRegistry = this.asciidoctor.rubyExtensionRegistry();
+ final Asciidoctor asciidoctor = Asciidoctor.Factory.create(singletonList(rubyExtPath));
asciidoctor.rubyExtensionRegistry()
.requireLibrary("xml-entity-postprocessor.rb")
.postprocessor("XmlEntityPostprocessor");
@@ -159,7 +206,78 @@ public class WhenRubyExtensionIsRegistered {
"Read &sect;2 and it&apos;ll all be clear.",
options().toFile(false).get());
- System.out.println(content);
assertThat(content, containsString("Read &#167;2 and it&#39;ll all be clear."));
}
+
+ @Test
+ public void ruby_preprocessor_should_be_registered() {
+
+ final String rubyExtPath = classpath.getResource("ruby-extensions").getAbsolutePath();
+ final Asciidoctor asciidoctor = Asciidoctor.Factory.create(singletonList(rubyExtPath));
+ asciidoctor.rubyExtensionRegistry()
+ .requireLibrary("front-matter-preprocessor.rb")
+ .preprocessor("FrontMatterPreprocessor");
+
+ String content = asciidoctor.render(
+ "---\n" +
+ "tags: [announcement, website]\n" +
+ "---\n" +
+ "= Document Title\n" +
+ "\n" +
+ "content\n" +
+ "\n" +
+ "[subs=\"attributes,specialcharacters\"]\n" +
+ ".Captured front matter\n" +
+ "....\n" +
+ "---\n" +
+ "{front-matter}\n" +
+ "---\n" +
+ "....",
+ options().toFile(false).get());
+
+ final Document document = Jsoup.parse(content);
+ final Element contentElement = document.getElementsByClass("content").get(0);
+ final Element literalElement = contentElement.getElementsByTag("pre").get(0);
+ assertThat(literalElement.toString().replace("\r", ""),
+ containsString("---\n" +
+ "tags: [announcement, website]\n" +
+ "---"));
+ }
+
+ @Test
+ public void ruby_docinfoprocessor_should_be_registered() {
+
+ final String rubyExtPath = classpath.getResource("ruby-extensions").getAbsolutePath();
+ final Asciidoctor asciidoctor = Asciidoctor.Factory.create(singletonList(rubyExtPath));
+ asciidoctor.rubyExtensionRegistry()
+ .requireLibrary("view-result-docinfoprocessor.rb")
+ .docinfoProcessor("ViewResultDocinfoProcessor");
+
+ String content = asciidoctor.render(
+ "= View Result Sample \n" +
+ " \n" +
+ ".This will have a link next to it\n" +
+ "---- \n" +
+ "* always displayed \n" +
+ "* always displayed 2 \n" +
+ "---- \n" +
+ " \n" +
+ "[.result] \n" +
+ "==== \n" +
+ "* hidden till clicked \n" +
+ "* hidden till clicked 2 \n" +
+ "==== ",
+ options().toFile(false).safe(SafeMode.SAFE).headerFooter(true).get());
+
+ final Document document = Jsoup.parse(content);
+ final Iterator<Element> elems = document.getElementsByTag("style").iterator();
+ boolean found = false;
+ while (elems.hasNext()) {
+ final Element styleElem = elems.next();
+ if (styleElem.toString().contains(".listingblock a.view-result")) {
+ found = true;
+ }
+ }
+ assertTrue("Could not find style element that should have been added by docinfo processor:\n" + document, found);
+ }
}

ADD
asciidoctorj-core/src/test/resources/ruby-extensions/front-matter-preprocessor.rb
+require 'asciidoctor'
+require 'asciidoctor/extensions'
+
+class FrontMatterPreprocessor < Asciidoctor::Extensions::Preprocessor
+ def process document, reader
+ lines = reader.lines # get raw lines
+ return reader if lines.empty?
+ front_matter = []
+ if lines.first.chomp == '---'
+ original_lines = lines.dup
+ lines.shift
+ while !lines.empty? && lines.first.chomp != '---'
+ front_matter << lines.shift
+ end
+
+ if (first = lines.first).nil? || first.chomp != '---'
+ lines = original_lines
+ else
+ lines.shift
+ document.attributes['front-matter'] = front_matter.join.chomp
+ # advance the reader by the number of lines taken
+ (front_matter.length + 2).times { reader.advance }
+ end
+ end
+ reader
+ end
+end
\ No newline at end of file

ADD
asciidoctorj-core/src/test/resources/ruby-extensions/response-include-processor.rb
+require 'asciidoctor'
+require 'asciidoctor/extensions'
+
+class ResponseIncludeProcessor < Asciidoctor::Extensions::IncludeProcessor
+ def handles? target
+ target == 'response'
+ end
+
+ def process doc, reader, target, attributes
+ reader.push_include '42', target, target, 1, attributes
+ reader
+ end
+end
\ No newline at end of file

ADD
asciidoctorj-core/src/test/resources/ruby-extensions/shell-session-tree-processor.rb
+class ShellSessionTreeProcessor < Asciidoctor::Extensions::TreeProcessor
+ def process document
+ return unless document.blocks?
+ process_blocks document
+ nil
+ end
+
+ def process_blocks node
+ node.blocks.each_with_index do |block, i|
+ if block.context == :literal &&
+ (((first_line = block.lines.first).start_with? '$ ') ||
+ (first_line.start_with? '> '))
+ node.blocks[i] = convert_to_terminal_listing block
+ else
+ process_blocks block if block.blocks?
+ end
+ end
+ end
+
+ def convert_to_terminal_listing block
+ attrs = block.attributes
+ attrs['role'] = 'terminal'
+ prompt_attr = (attrs.has_key? 'prompt') ?
+ %( data-prompt="#{block.sub_specialchars attrs['prompt']}") : nil
+ lines = block.lines.map do |line|
+ line = block.sub_specialchars line.chomp
+ if line.start_with? '$ '
+ %(<span class="command"#{prompt_attr}>#{line[2..-1]}</span>)
+ elsif line.start_with? '&gt; '
+ %(<span class="output">#{line[5..-1]}</span>)
+ else
+ line
+ end
+ end
+ create_listing_block block.document, lines * "\n", attrs, subs: nil
+ end
+end

ADD
asciidoctorj-core/src/test/resources/ruby-extensions/view-result-docinfoprocessor.rb
+require 'asciidoctor/extensions' unless RUBY_ENGINE == 'opal'
+
+# An extension that automatically hides blocks marked with the style
+# "result" and adds a link to the previous element that has the style
+# "title" that allows displaying the "result".
+#
+# Usage
+#
+# = View Result Sample
+#
+# .This will have a link next to it
+# ----
+# * always displayed
+# * always displayed 2
+# ----
+#
+# [.result]
+# ====
+# * hidden till clicked
+# * hidden till clicked 2
+# ====
+#
+#
+class ViewResultDocinfoProcessor < Asciidoctor::Extensions::DocinfoProcessor
+ use_dsl
+ at_location :head
+
+ def process doc
+ styles = %(<style>
+.listingblock a.view-result {
+ float: right;
+ font-weight: normal;
+ text-decoration: none;
+ font-size: 0.9em;
+ line-height: 1.4;
+ margin-top: 0.15em;
+}
+</style>)
+
+ scripts = %(<script>
+function toggle_result_block(e) {
+ var resultNode = e.target.parentNode.parentNode.nextElementSibling;
+ resultNode.style.display = getComputedStyle(resultNode).display == 'none' ? '' : 'none';
+ return false;
+}
+document.addEventListener('DOMContentLoaded', function() {
+ [].forEach.call(document.querySelectorAll('.result'), function(resultNode) {
+ resultNode.style.display = 'none';
+ var viewLink = document.createElement('a');
+ viewLink.className = 'view-result';
+ viewLink.href = '#';
+ viewLink.appendChild(document.createTextNode('[ view result ]'));
+ resultNode.previousElementSibling.querySelector('.title').appendChild(viewLink);
+ viewLink.addEventListener('click', toggle_result_block);
+ });
+});
+</script>);
+puts "I'm called"
+ [styles, scripts].join "\n"
+ end
+end
\ No newline at end of file

Added more tests for Ruby extensions