diff --git a/asciidoctorj-core/build.gradle b/asciidoctorj-core/build.gradle index c20255718..d7f446cec 100644 --- a/asciidoctorj-core/build.gradle +++ b/asciidoctorj-core/build.gradle @@ -27,6 +27,14 @@ def gemFiles = fileTree(jruby.gemInstallDir) { jrubyPrepareGems << { copy { // bundles the gems inside this artifact from gemFiles + eachFile { + // See https://github.com/asciidoctor/asciidoctorj/issues/497 + if (it.path ==~ /gems\/asciidoctor-.+\/lib\/asciidoctor\/abstract_block.rb/) { + it.filter { line -> + line.replaceAll(/block.context == :section/, 'block.context.to_sym == :section') + } + } + } into sourceSets.main.output.resourcesDir } } diff --git a/asciidoctorj-core/src/main/java/org/asciidoctor/ast/AbstractBlockImpl.java b/asciidoctorj-core/src/main/java/org/asciidoctor/ast/AbstractBlockImpl.java index ecfb9528a..99a173d5b 100644 --- a/asciidoctorj-core/src/main/java/org/asciidoctor/ast/AbstractBlockImpl.java +++ b/asciidoctorj-core/src/main/java/org/asciidoctor/ast/AbstractBlockImpl.java @@ -1,15 +1,13 @@ package org.asciidoctor.ast; -import java.util.List; -import java.util.Map; - -import org.asciidoctor.converter.ConverterProxy; import org.asciidoctor.internal.RubyHashUtil; import org.asciidoctor.internal.RubyUtils; import org.jruby.Ruby; import org.jruby.RubyArray; import org.jruby.RubyObject; -import org.jruby.javasupport.JavaEmbedUtils; + +import java.util.List; +import java.util.Map; public class AbstractBlockImpl extends AbstractNodeImpl implements AbstractBlock { @@ -44,6 +42,10 @@ public String getStyle() { return delegate.getStyle(); } + public String getCaption() { + return RubyUtils.invokeRubyMethod(delegate, "caption", new Object[0], String.class); + } + @Override public List blocks() { return getBlocks(); diff --git a/asciidoctorj-core/src/main/java/org/asciidoctor/ast/SectionImpl.java b/asciidoctorj-core/src/main/java/org/asciidoctor/ast/SectionImpl.java index c44ae27e4..0e4acc565 100644 --- a/asciidoctorj-core/src/main/java/org/asciidoctor/ast/SectionImpl.java +++ b/asciidoctorj-core/src/main/java/org/asciidoctor/ast/SectionImpl.java @@ -1,7 +1,10 @@ package org.asciidoctor.ast; +import org.asciidoctor.internal.RubyUtils; import org.jruby.Ruby; +import java.util.List; + public class SectionImpl extends AbstractBlockImpl implements Section { private Section delegate; @@ -36,4 +39,27 @@ public int numbered() { return this.delegate.number(); } + public String sectnum() { + return RubyUtils.invokeRubyMethod(delegate, "sectnum", new Object[0], String.class); + } + + public String sectnum(String delimiter) { + return RubyUtils.invokeRubyMethod(delegate, "sectnum", new Object[]{delimiter}, String.class); + } + + public String sectnum(String delimiter, boolean append) { + return RubyUtils.invokeRubyMethod(delegate, "sectnum", new Object[]{delimiter, append}, String.class); + } + + public List
getSections() { + return RubyUtils.invokeRubyMethod(delegate, "sections", new Object[0], List.class); + } + + public boolean isSections() { + return RubyUtils.invokeRubyMethod(delegate, "sections?", new Object[0], Boolean.class); + } + + public String getCaptionedTitle() { + return RubyUtils.invokeRubyMethod(delegate, "captioned_title", new Object[0], String.class); + } } diff --git a/asciidoctorj-core/src/main/java/org/asciidoctor/internal/RubyUtils.java b/asciidoctorj-core/src/main/java/org/asciidoctor/internal/RubyUtils.java index d233162cc..889b92565 100644 --- a/asciidoctorj-core/src/main/java/org/asciidoctor/internal/RubyUtils.java +++ b/asciidoctorj-core/src/main/java/org/asciidoctor/internal/RubyUtils.java @@ -7,6 +7,7 @@ import org.jruby.RubyObject; import org.jruby.RubySymbol; import org.jruby.internal.runtime.GlobalVariable.Scope; +import org.jruby.java.proxies.RubyObjectHolderProxy; import org.jruby.javasupport.JavaClass; import org.jruby.javasupport.JavaEmbedUtils; import org.jruby.runtime.GlobalVariable; @@ -41,4 +42,17 @@ public static final void setGlobalVariable(Ruby rubyRuntime, String variableName rubyRuntime.evalScriptlet(script); } + public static T invokeRubyMethod(final Object target, final String methodName, final Object[] args, final Class resultType) { + IRubyObject rubyObject = null; + if (target instanceof RubyObjectHolderProxy) { + rubyObject = RubyObjectHolderProxy.class.cast(target).__ruby_object(); + } else if (target instanceof IRubyObject) { + rubyObject = (IRubyObject) target; + } + if (rubyObject != null) { + return (T) JavaEmbedUtils.invokeMethod(rubyObject.getRuntime(), rubyObject, methodName, args, resultType); + } + throw new IllegalArgumentException("Target is a " + target.getClass() + " instead of a Ruby object."); + } + } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenJavaExtensionIsRegistered.java b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenJavaExtensionIsRegistered.java index 740db68d7..2b50a87cc 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenJavaExtensionIsRegistered.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenJavaExtensionIsRegistered.java @@ -22,6 +22,7 @@ import java.util.Map; import org.asciidoctor.Asciidoctor; +import org.asciidoctor.AttributesBuilder; import org.asciidoctor.Options; import org.asciidoctor.SafeMode; import org.asciidoctor.ast.DocumentRuby; @@ -390,6 +391,24 @@ public void a_treeprocessor_should_be_executed_in_document() { } + /** + * See https://github.com/asciidoctor/asciidoctorj/issues/497. + */ + @Test + public void when_using_a_tree_processor_a_toc_should_still_be_created() { + + JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry(); + + javaExtensionRegistry.treeprocessor(TerminalCommandTreeprocessor.class); + + String content = asciidoctor.renderFile( + classpath.getResource("sample-with-sections.ad"), + options().toFile(false) + .attributes(AttributesBuilder.attributes().tableOfContents(true)) + .get()); + } + + @Test public void a_treeprocessor_as_string_should_be_executed_in_document() { diff --git a/asciidoctorj-core/src/test/resources/sample-with-sections.ad b/asciidoctorj-core/src/test/resources/sample-with-sections.ad new file mode 100644 index 000000000..c8ca20e83 --- /dev/null +++ b/asciidoctorj-core/src/test/resources/sample-with-sections.ad @@ -0,0 +1,15 @@ += Hello World + +Some text + +== Section A + +More text + +=== Subsection A1 + +Subsections are also so important. + +== Section B + +And even more text \ No newline at end of file