Skip to content

Commit

Permalink
Fixes #497. Patch Asciidoctor::AbstractBlock#section? in abstract_blo…
Browse files Browse the repository at this point in the history
…ck.rb to return context by string instead of Symbol because Java blocks always return strings.
  • Loading branch information
robertpanzer committed Aug 13, 2016
1 parent 77e8070 commit 1acd83a
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 5 deletions.
8 changes: 8 additions & 0 deletions asciidoctorj-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -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<AbstractBlock> blocks() {
return getBlocks();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Section> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,4 +42,17 @@ public static final void setGlobalVariable(Ruby rubyRuntime, String variableName
rubyRuntime.evalScriptlet(script);
}

public static <T> T invokeRubyMethod(final Object target, final String methodName, final Object[] args, final Class<T> 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.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {

Expand Down
15 changes: 15 additions & 0 deletions asciidoctorj-core/src/test/resources/sample-with-sections.ad
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1acd83a

Please sign in to comment.