From 944f13bfacd9e8b12fb8bed5d53dedd4d7c7215d Mon Sep 17 00:00:00 2001 From: Robert Panzer Date: Sat, 13 Aug 2016 17:49:23 +0200 Subject: [PATCH 1/2] Add missing method implementations to Java AST classes and fixed some misalignments. Replace all occurrences of ".context " with ".context.to_sym " --- asciidoctorj-core/build.gradle | 5 ++- .../asciidoctor/ast/AbstractBlockImpl.java | 16 ++++++++ .../java/org/asciidoctor/ast/BlockImpl.java | 8 +++- .../java/org/asciidoctor/ast/ListImpl.java | 17 +++++++- .../org/asciidoctor/ast/ListItemImpl.java | 15 ++++++- .../java/org/asciidoctor/ast/ListNode.java | 2 +- .../java/org/asciidoctor/ast/Section.java | 2 +- .../java/org/asciidoctor/ast/SectionImpl.java | 16 ++++---- .../TouchEverythingTreeprocessor.java | 39 +++++++++++++++++++ .../WhenJavaExtensionIsRegistered.java | 20 +++++++++- .../test/resources/sample-with-sections.ad | 16 +++++++- 11 files changed, 136 insertions(+), 20 deletions(-) create mode 100644 asciidoctorj-core/src/test/java/org/asciidoctor/extension/TouchEverythingTreeprocessor.java diff --git a/asciidoctorj-core/build.gradle b/asciidoctorj-core/build.gradle index d7f446cec..ca4f828e3 100644 --- a/asciidoctorj-core/build.gradle +++ b/asciidoctorj-core/build.gradle @@ -29,9 +29,10 @@ jrubyPrepareGems << { from gemFiles eachFile { // See https://github.com/asciidoctor/asciidoctorj/issues/497 - if (it.path ==~ /gems\/asciidoctor-.+\/lib\/asciidoctor\/abstract_block.rb/) { + if (it.path ==~ /gems\/asciidoctor-.+\/lib\/asciidoctor\/.*\.rb/) { it.filter { line -> - line.replaceAll(/block.context == :section/, 'block.context.to_sym == :section') + line.replaceAll(/\.context /, '.context.to_sym ') + .replaceAll(/\.context\)/, '.context.to_sym)') } } } 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 99a173d5b..146ad7a78 100644 --- a/asciidoctorj-core/src/main/java/org/asciidoctor/ast/AbstractBlockImpl.java +++ b/asciidoctorj-core/src/main/java/org/asciidoctor/ast/AbstractBlockImpl.java @@ -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(); @@ -66,6 +70,10 @@ public List getBlocks() { return rubyBlocks; } + public boolean isBlocks() { + return RubyUtils.invokeRubyMethod(delegate, "blocks?", new Object[0], Boolean.class); + } + @Override public Object content() { return getContent(); @@ -96,6 +104,14 @@ public AbstractBlock delegate() { return delegate; } + 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); + } + @Override public List findBy(Map selector) { diff --git a/asciidoctorj-core/src/main/java/org/asciidoctor/ast/BlockImpl.java b/asciidoctorj-core/src/main/java/org/asciidoctor/ast/BlockImpl.java index b001da86e..e701044c5 100644 --- a/asciidoctorj-core/src/main/java/org/asciidoctor/ast/BlockImpl.java +++ b/asciidoctorj-core/src/main/java/org/asciidoctor/ast/BlockImpl.java @@ -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; @@ -21,4 +21,8 @@ public List lines() { public String source() { return blockDelegate.source(); } + + public String getBlockname() { + return getContext(); + } } diff --git a/asciidoctorj-core/src/main/java/org/asciidoctor/ast/ListImpl.java b/asciidoctorj-core/src/main/java/org/asciidoctor/ast/ListImpl.java index 13bd3e38f..ec7d9cecb 100644 --- a/asciidoctorj-core/src/main/java/org/asciidoctor/ast/ListImpl.java +++ b/asciidoctorj-core/src/main/java/org/asciidoctor/ast/ListImpl.java @@ -1,5 +1,6 @@ package org.asciidoctor.ast; +import org.asciidoctor.internal.RubyUtils; import org.jruby.Ruby; import java.util.List; @@ -19,8 +20,15 @@ public List 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 @@ -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); + } } diff --git a/asciidoctorj-core/src/main/java/org/asciidoctor/ast/ListItemImpl.java b/asciidoctorj-core/src/main/java/org/asciidoctor/ast/ListItemImpl.java index f6eb643b0..464bfe4d4 100644 --- a/asciidoctorj-core/src/main/java/org/asciidoctor/ast/ListItemImpl.java +++ b/asciidoctorj-core/src/main/java/org/asciidoctor/ast/ListItemImpl.java @@ -1,5 +1,6 @@ package org.asciidoctor.ast; +import org.asciidoctor.internal.RubyUtils; import org.jruby.Ruby; public class ListItemImpl extends AbstractBlockImpl implements ListItem { @@ -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); } } diff --git a/asciidoctorj-core/src/main/java/org/asciidoctor/ast/ListNode.java b/asciidoctorj-core/src/main/java/org/asciidoctor/ast/ListNode.java index f958a968e..d1f86ede7 100644 --- a/asciidoctorj-core/src/main/java/org/asciidoctor/ast/ListNode.java +++ b/asciidoctorj-core/src/main/java/org/asciidoctor/ast/ListNode.java @@ -4,7 +4,7 @@ public interface ListNode extends AbstractBlock { java.util.List getItems(); - boolean isItem(); + boolean hasItems(); @Deprecated public String render(); diff --git a/asciidoctorj-core/src/main/java/org/asciidoctor/ast/Section.java b/asciidoctorj-core/src/main/java/org/asciidoctor/ast/Section.java index 8304f6c0d..a552b782a 100644 --- a/asciidoctorj-core/src/main/java/org/asciidoctor/ast/Section.java +++ b/asciidoctorj-core/src/main/java/org/asciidoctor/ast/Section.java @@ -6,5 +6,5 @@ public interface Section extends AbstractBlock { int number(); String sectname(); boolean special(); - int numbered(); + boolean numbered(); } 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 0e4acc565..45f1deadb 100644 --- a/asciidoctorj-core/src/main/java/org/asciidoctor/ast/SectionImpl.java +++ b/asciidoctorj-core/src/main/java/org/asciidoctor/ast/SectionImpl.java @@ -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() { @@ -51,15 +51,15 @@ 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 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(); } } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/TouchEverythingTreeprocessor.java b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/TouchEverythingTreeprocessor.java new file mode 100644 index 000000000..7fc5cb555 --- /dev/null +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/TouchEverythingTreeprocessor.java @@ -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 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); + } + } + } +} 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 2b50a87cc..06acc4156 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenJavaExtensionIsRegistered.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/extension/WhenJavaExtensionIsRegistered.java @@ -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()); } diff --git a/asciidoctorj-core/src/test/resources/sample-with-sections.ad b/asciidoctorj-core/src/test/resources/sample-with-sections.ad index c8ca20e83..dd3f2fe60 100644 --- a/asciidoctorj-core/src/test/resources/sample-with-sections.ad +++ b/asciidoctorj-core/src/test/resources/sample-with-sections.ad @@ -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 \ No newline at end of file +And even more text + +|=== +| A | Table + +| is | also +| a | nice +| thing | ! +|=== From 9d258c0e82771a1c801d9f01627479ee553aeb2a Mon Sep 17 00:00:00 2001 From: Robert Panzer Date: Mon, 15 Aug 2016 13:08:36 +0200 Subject: [PATCH 2/2] Removed greedy replacement of .context to .context.to_sym again --- asciidoctorj-core/build.gradle | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/asciidoctorj-core/build.gradle b/asciidoctorj-core/build.gradle index ca4f828e3..d7f446cec 100644 --- a/asciidoctorj-core/build.gradle +++ b/asciidoctorj-core/build.gradle @@ -29,10 +29,9 @@ jrubyPrepareGems << { from gemFiles eachFile { // See https://github.com/asciidoctor/asciidoctorj/issues/497 - if (it.path ==~ /gems\/asciidoctor-.+\/lib\/asciidoctor\/.*\.rb/) { + if (it.path ==~ /gems\/asciidoctor-.+\/lib\/asciidoctor\/abstract_block.rb/) { it.filter { line -> - line.replaceAll(/\.context /, '.context.to_sym ') - .replaceAll(/\.context\)/, '.context.to_sym)') + line.replaceAll(/block.context == :section/, 'block.context.to_sym == :section') } } }