Skip to content

Commit

Permalink
Fixes asciidoctor#325. Added method AbstractBlock.append(AbstractBloc…
Browse files Browse the repository at this point in the history
…k) to append child blocks
  • Loading branch information
robertpanzer committed Jul 29, 2015
1 parent 363bd2b commit b8795c5
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
Expand Up @@ -16,11 +16,24 @@ public interface AbstractBlock extends AbstractNode {
*/
String style();
String getStyle();

/**
* @return The list of child blocks of this block
* @deprecated Please use {@linkplain #getBlocks()} instead
*/
List<AbstractBlock> blocks();

/**
* @return The list of child blocks of this block
*/
List<AbstractBlock> getBlocks();

/**
* Appends a new child block as the last block to this block.
* @param block The new child block added as last child to this block.
*/
void append(AbstractBlock block);

/**
* @deprecated Please use {@linkplain #getContent()} instead
*/
Expand Down
Expand Up @@ -53,6 +53,12 @@ public List<AbstractBlock> getBlocks() {
return new RubyBlockListDecorator<AbstractBlock>(rubyBlocks);
}

@Override
public void append(AbstractBlock block) {

getRubyObject().callMethod(runtime.getCurrentContext(), "<<", ((AbstractBlockImpl) block).getRubyObject());
}

@Override
public Object content() {
return getContent();
Expand Down
Expand Up @@ -16,6 +16,8 @@ public interface AbstractNode {
*/
String getId();

void setId(String id);

String getNodeName();
/**
* @deprecated Use {@linkplain #getParent()} instead.
Expand Down
Expand Up @@ -27,6 +27,11 @@ public String getId() {
return getString("id");
}

@Override
public void setId(String id) {
setString("id", id);
}

@Override
public String context() {
return getContext();
Expand Down
@@ -0,0 +1,65 @@
package org.asciidoctor.extension

import org.asciidoctor.Asciidoctor
import org.asciidoctor.OptionsBuilder
import org.asciidoctor.ast.Block
import org.asciidoctor.ast.DocumentRuby
import org.asciidoctor.ast.Section
import org.jboss.arquillian.spock.ArquillianSputnik
import org.jboss.arquillian.test.api.ArquillianResource
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.junit.runner.RunWith
import spock.lang.Specification

@RunWith(ArquillianSputnik)
class WhenAnExtensionAppendsChildBlocks extends Specification {

String document = '''= Test document
== Section 1
some text
== Section 2
more text
'''

@ArquillianResource
private Asciidoctor asciidoctor

def 'should be able to blocks via Block_append'() {

given:

final String additionalText = 'Text added by Treeprocessor'
asciidoctor.javaExtensionRegistry().treeprocessor(new Treeprocessor() {

int lastid = 0

@Override
DocumentRuby process(DocumentRuby document) {
document.blocks.findAll { block -> block instanceof Section }.each {
block ->
Block newBlock = createBlock(block, 'paragraph', additionalText, [:])
newBlock.id = "NewBlock_${lastid++}"
block.append(newBlock)
}
document
}
})

when:
String result = asciidoctor.convert(this.document, OptionsBuilder.options().headerFooter(false))

then:
Document htmlDocument = Jsoup.parse(result)
!htmlDocument.select('#NewBlock_0').empty
!htmlDocument.select('#NewBlock_1').empty
htmlDocument.select('#NewBlock_2').empty

}

}

0 comments on commit b8795c5

Please sign in to comment.