Skip to content

Commit

Permalink
Merge pull request #499 from robertpanzer/fix_java_ast
Browse files Browse the repository at this point in the history
Add missing method implementations to Java AST classes and fixed some…
  • Loading branch information
robertpanzer authored Aug 15, 2016
2 parents 28da25c + 9d258c0 commit 75dc89c
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public String getTitle() {
return delegate.getTitle();
}

public boolean isTitle() {
return RubyUtils.invokeRubyMethod(delegate, "title?", new Object[0], Boolean.class);
}

@Override
public String style() {
return getStyle();
Expand Down Expand Up @@ -66,6 +70,10 @@ public List<AbstractBlock> getBlocks() {
return rubyBlocks;
}

public boolean isBlocks() {
return RubyUtils.invokeRubyMethod(delegate, "blocks?", new Object[0], Boolean.class);
}

@Override
public Object content() {
return getContent();
Expand Down Expand Up @@ -96,6 +104,14 @@ public AbstractBlock delegate() {
return delegate;
}

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);
}

@Override
public List<AbstractBlock> findBy(Map<Object, Object> selector) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.asciidoctor.ast;

import java.util.List;

import org.jruby.Ruby;

import java.util.List;

public class BlockImpl extends AbstractBlockImpl implements Block {
private Block blockDelegate;

Expand All @@ -21,4 +21,8 @@ public List<String> lines() {
public String source() {
return blockDelegate.source();
}

public String getBlockname() {
return getContext();
}
}
17 changes: 15 additions & 2 deletions asciidoctorj-core/src/main/java/org/asciidoctor/ast/ListImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.asciidoctor.ast;

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

import java.util.List;
Expand All @@ -19,8 +20,15 @@ public List<AbstractBlock> getItems() {
}

@Override
public boolean isItem() {
return isBlock();
public boolean hasItems() {
return isItems();
}

/**
* This method will be invoked by Ruby.
*/
public boolean isItems() {
return isBlocks();
}

@Override
Expand All @@ -32,4 +40,9 @@ public String render() {
public String convert() {
return listDelegate.convert();
}

public boolean isOutline() {
final String context = getContext();
return "ulist".equals(context) || "olist".equals(context);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.asciidoctor.ast;

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

public class ListItemImpl extends AbstractBlockImpl implements ListItem {
Expand All @@ -23,6 +24,18 @@ public String getText() {

@Override
public boolean hasText() {
return listDelegate.hasText();
return isText();
}

public boolean isText() {
return RubyUtils.invokeRubyMethod(delegate, "text?", new Object[0], Boolean.class);
}

public boolean isSimple() {
return RubyUtils.invokeRubyMethod(delegate, "simple?", new Object[0], Boolean.class);
}

public boolean isCompound() {
return RubyUtils.invokeRubyMethod(delegate, "compound?", new Object[0], Boolean.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public interface ListNode extends AbstractBlock {

java.util.List<AbstractBlock> getItems();

boolean isItem();
boolean hasItems();

@Deprecated
public String render();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ public interface Section extends AbstractBlock {
int number();
String sectname();
boolean special();
int numbered();
boolean numbered();
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public boolean special() {
}

@Override
public int numbered() {
return this.delegate.number();
public boolean numbered() {
return RubyUtils.invokeRubyMethod(delegate, "numbered", new Object[0], Boolean.class);
}

public String sectnum() {
Expand All @@ -51,15 +51,15 @@ 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 String getCaptionedTitle() {
return RubyUtils.invokeRubyMethod(delegate, "captioned_title", new Object[0], String.class);
}

public boolean isSections() {
return RubyUtils.invokeRubyMethod(delegate, "sections?", new Object[0], Boolean.class);
public String generateId() {
return RubyUtils.invokeRubyMethod(delegate, "generate_id", new Object[0], String.class);
}

public String getCaptionedTitle() {
return RubyUtils.invokeRubyMethod(delegate, "captioned_title", new Object[0], String.class);
public String getName() {
return getTitle();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.asciidoctor.extension;

import org.asciidoctor.ast.AbstractBlock;
import org.asciidoctor.ast.Block;
import org.asciidoctor.ast.Document;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* This Treeprocessor doesn't do anything useful but only touch every node in the
* AST so that the whole tree contains nothing but Java AST nodes instead of the Ruby originals.
* This should reveal misalignments in the between the Ruby AST classes and their Java counterparts.
*/
public class TouchEverythingTreeprocessor extends Treeprocessor {

public TouchEverythingTreeprocessor(Map<String, Object> config) {
super(config);
}

@Override
public Document process(Document document) {

touch(document);

return document;
}

public void touch(AbstractBlock block) {

if (block.getBlocks() != null) {
for (AbstractBlock abstractBlock : block.getBlocks()) {
touch(abstractBlock);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -395,15 +395,31 @@ 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() {
public void when_using_a_tree_processor_a_toc_should_still_be_created_when_rendering_to_html() {

JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry();

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

String content = asciidoctor.renderFile(
classpath.getResource("sample-with-sections.ad"),
options().toFile(false)
.backend("html")
.attributes(AttributesBuilder.attributes().tableOfContents(true))
.get());
}

@Test
public void when_using_a_tree_processor_a_toc_should_still_be_created_when_rendering_to_docbook() {

JavaExtensionRegistry javaExtensionRegistry = this.asciidoctor.javaExtensionRegistry();

javaExtensionRegistry.treeprocessor(TouchEverythingTreeprocessor.class);

String content = asciidoctor.renderFile(
classpath.getResource("sample-with-sections.ad"),
options().toFile(false)
.backend("docbook")
.attributes(AttributesBuilder.attributes().tableOfContents(true))
.get());
}
Expand Down
16 changes: 15 additions & 1 deletion asciidoctorj-core/src/test/resources/sample-with-sections.ad
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ More text

Subsections are also so important.

* And unordered lists
* are also very important

. and numbered ones
. too

== Section B

And even more text
And even more text

|===
| A | Table

| is | also
| a | nice
| thing | !
|===

0 comments on commit 75dc89c

Please sign in to comment.