Skip to content

Commit

Permalink
Added methods setSource to Block, Cell and ListItem (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpanzer authored Oct 18, 2016
1 parent 9956d1c commit f7fcb42
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 94 deletions.
12 changes: 12 additions & 0 deletions asciidoctorj-core/src/main/java/org/asciidoctor/ast/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ public interface Block extends StructuralNode {
*/
List<String> getLines();

/**
* Sets the source lines of the Block.
* @param lines The source of this Block, substitutions will still be applied.
*/
void setLines(List<String> lines);

/**
* @deprecated Please use {@link #getSource}
* @return the String containing the lines joined together or null if there are no lines
Expand All @@ -25,4 +31,10 @@ public interface Block extends StructuralNode {
*/
String getSource();

/**
* Sets the source of the ListItem.
* @param source The source of this ListItem, substitutions will still be applied.
*/
void setSource(String source);

}
6 changes: 6 additions & 0 deletions asciidoctorj-core/src/main/java/org/asciidoctor/ast/Cell.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public interface Cell extends ContentNode {
*/
String getSource();

/**
* Sets the source of the Cell.
* @param source The source of this Cell, substitutions will still be applied.
*/
void setSource(String source);

Object getContent();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,11 @@ public interface ListItem extends StructuralNode {
*/
public String getSource();

/**
* Sets the source of the ListItem.
* @param source The source of this ListItem, substitutions will still be applied.
*/
public void setSource(String source);

public boolean hasText();
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.asciidoctor.ast.impl;

import java.util.Arrays;
import java.util.List;

import org.asciidoctor.ast.Block;
import org.asciidoctor.ast.impl.StructuralNodeImpl;
import org.jruby.RubyArray;
import org.jruby.runtime.builtin.IRubyObject;

public class BlockImpl extends StructuralNodeImpl implements Block {
Expand All @@ -22,6 +24,15 @@ public List<String> getLines() {
return getList("lines", String.class);
}

@Override
public void setLines(List<String> lines) {
RubyArray newLines = getRuntime().newArray(lines.size());
for (String s: lines) {
newLines.add(getRuntime().newString(s));
}
setRubyProperty("lines", newLines);
}

@Override
public String source() {
return getSource();
Expand All @@ -32,4 +43,8 @@ public String getSource() {
return getString("source");
}

@Override
public void setSource(String source) {
setLines(Arrays.asList(source.split("\n")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public String getSource() {
return getString("@text");
}

@Override
public void setSource(String source) {
setString("@text", source);
}

@Override
public Object getContent() {
return toJava(getRubyProperty("content"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public String getSource() {
return getString("@text");
}

@Override
public void setSource(String source) {
setString("@text", source);
}

@Override
public boolean hasText() {
return getBoolean("text?");
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package org.asciidoctor

import org.asciidoctor.ast.Block
import org.asciidoctor.ast.Cell
import org.asciidoctor.ast.Document
import org.asciidoctor.ast.List
import org.asciidoctor.ast.ListItem
import org.asciidoctor.ast.Table
import org.asciidoctor.extension.Treeprocessor
import org.jboss.arquillian.spock.ArquillianSputnik
import org.jboss.arquillian.test.api.ArquillianResource
import org.jsoup.Jsoup
import org.junit.runner.RunWith
import spock.lang.Specification

/**
* Tests that the unsubstituted text can be retrieved from nodes
*/
@RunWith(ArquillianSputnik)
class WhenTheSourceShouldBeAccessed extends Specification {

public static final String SOURCE = 'This paragraph should show {foo}'
public static final String CONVERTED = 'This paragraph should show bar'
public static final String P = 'p'
public static final String TD = 'td'
@ArquillianResource
private Asciidoctor asciidoctor

def 'it should be possible to get the raw text from a paragraph'() {

given:
String document = '''
= Test
:foo: bar
== Section
This paragraph should show {foo}
'''
when:
Document doc = asciidoctor.load(document, OptionsBuilder.options().asMap())
Block block = doc.blocks[0].blocks[0]

then:
block.source == SOURCE
block.content == CONVERTED
}

def 'it should be possible to get the raw text from a list item'() {

given:
String document = '''
= Test
:foo: bar
== Section
* This list item should show {foo}
and should continue here
* This does not interest at all
'''
when:
Document doc = asciidoctor.load(document, OptionsBuilder.options().asMap())
List list = doc.blocks[0].blocks[0]
ListItem listItem = list.items[0]

then:
listItem.source == '''This list item should show {foo}
and should continue here'''
listItem.text == '''This list item should show bar
and should continue here'''
}

def 'it should be possible to get the raw text from a table cell'() {

given:
String document = '''
= Test
:foo: bar
== Section
|===
| Hello {foo}
|===
'''
when:
Document doc = asciidoctor.load(document, OptionsBuilder.options().asMap())
Table table = doc.blocks[0].blocks[0]
Cell cell = table.body[0].cells[0]

then:
cell.source == 'Hello {foo}'
cell.text == 'Hello bar'
}


def 'it should be possible to set the raw text of a paragraph'() {

given:
String document = '''
= Test
:foo: bar
== Section
xxx
'''
when:
asciidoctor.javaExtensionRegistry().treeprocessor(new Treeprocessor() {
@Override
Document process(Document doc) {
doc.blocks[0].blocks[0].source = SOURCE
doc
}
})
String html = asciidoctor.convert(document, OptionsBuilder.options().headerFooter(false).asMap())
org.jsoup.nodes.Document doc = Jsoup.parse(html)

then:
doc.select(P).text() == CONVERTED
}

def 'it should be possible to set the source of a list item'() {

given:
String document = '''
= Test
:foo: bar
== Section
* xxx
* This does not interest at all
'''
when:
asciidoctor.javaExtensionRegistry().treeprocessor(new Treeprocessor() {
@Override
Document process(Document doc) {
doc.blocks[0].blocks[0].items[0].source = SOURCE
doc
}
})
String html = asciidoctor.convert(document, OptionsBuilder.options().headerFooter(false).asMap())
org.jsoup.nodes.Document doc = Jsoup.parse(html)

then:
doc.select(P).get(0).text() == CONVERTED
doc.select(P).get(1).text() == 'This does not interest at all'
}

def 'it should be possible to set the raw text of a table cell'() {

given:
String document = '''
= Test
:foo: bar
== Section
|===
| xxx
|===
'''
when:
asciidoctor.javaExtensionRegistry().treeprocessor(new Treeprocessor() {
@Override
Document process(Document doc) {
Table table = doc.blocks[0].blocks[0]
Cell cell = table.body[0].cells[0]
cell.source = SOURCE
doc
}
})
String html = asciidoctor.convert(document, OptionsBuilder.options().headerFooter(false).asMap())
org.jsoup.nodes.Document doc = Jsoup.parse(html)

then:
doc.select(TD).text() == CONVERTED
}

}

0 comments on commit f7fcb42

Please sign in to comment.