Adding docs for accessing named and positional attributes in BlockProcessors
MODIFY
asciidoctorj-documentation/build.gradle
asciidoctorj-documentation/build.gradle
@@ -58,6 +58,8 @@ task copyExtensionsExamplesToDocs(type: Copy) {
include '**/extension/TerminalCommandExtension.java'
include '**/extension/TerminalCommandTreeprocessor.java'
include '**/extension/YellBlockProcessor.java'
+ include '**/extension/YellBlockProcessorWithAttributes.java'
+ include '**/extension/YellBlockProcessorWithPositionalAttributes.java'
}
from('src/test/resources/') {
@@ -71,6 +73,8 @@ task copyExtensionsExamplesToDocs(type: Copy) {
include 'treeprocessorcontent.adoc'
include 'treeprocessorresult.adoc'
include 'yell-block.adoc'
+ include 'yell-block-attributes.adoc'
+ include 'yell-block-positional.adoc'
include 'yell-block-result.txt'
include 'extensionregistry/META-INF/services/org.asciidoctor.jruby.extension.spi.ExtensionRegistry'

MODIFY
asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/YellBlockProcessorTest.java
asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/YellBlockProcessorTest.java
package org.asciidoctor.integrationguide.extension;
import org.asciidoctor.Asciidoctor;
+import org.asciidoctor.Options;
import org.asciidoctor.OptionsBuilder;
import org.asciidoctor.util.ClasspathResources;
import org.jboss.arquillian.junit.Arquillian;
@@ -38,4 +39,28 @@ public class YellBlockProcessorTest {
assertThat(result, containsString("I REALLY MEAN IT")); // <2>
//end::include[]
}
+
+ @Test
+ public void should_invoke_block_processor_with_attributes() throws Exception {
+ File yellblock_adoc = //...
+ classpathResources.getResource("yell-block-attributes.adoc");
+
+ asciidoctor.javaExtensionRegistry().block(YellBlockProcessorWithAttributes.class);
+
+ String result = asciidoctor.convertFile(yellblock_adoc, Options.builder().toFile(false).build());
+
+ assertThat(result, containsString("I REALLY MEAN IT!!!"));
+ }
+
+ @Test
+ public void should_invoke_block_processor_with_positional_attributes() throws Exception {
+ File yellblock_adoc = //...
+ classpathResources.getResource("yell-block-positional.adoc");
+
+ asciidoctor.javaExtensionRegistry().block(YellBlockProcessorWithPositionalAttributes.class);
+
+ String result = asciidoctor.convertFile(yellblock_adoc, Options.builder().toFile(false).build());
+
+ assertThat(result, containsString("I REALLY MEAN IT!!!!!"));
+ }
}

ADD
asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/YellBlockProcessorWithAttributes.java
+package org.asciidoctor.integrationguide.extension;
+
+import org.asciidoctor.ast.ContentModel;
+import org.asciidoctor.ast.StructuralNode;
+import org.asciidoctor.extension.BlockProcessor;
+import org.asciidoctor.extension.Contexts;
+import org.asciidoctor.extension.Name;
+import org.asciidoctor.extension.Reader;
+
+import java.util.Map;
+import java.util.stream.IntStream;
+
+import static java.util.stream.Collectors.joining;
+
+//tag::include[]
+@Name("yell")
+@Contexts({Contexts.PARAGRAPH})
+@ContentModel(ContentModel.SIMPLE)
+public class YellBlockProcessorWithAttributes extends BlockProcessor {
+
+ @Override
+ public Object process(
+ StructuralNode parent, Reader reader, Map<String, Object> attributes) {
+
+ String content = reader.read();
+ String yellContent = content.toUpperCase();
+
+ String loudness = (String) attributes.get("loudness"); // <1>
+ if (loudness != null) {
+ yellContent += IntStream.range(0, Integer.parseInt(loudness))
+ .mapToObj(i -> "!")
+ .collect(joining());
+ }
+
+ return createBlock(parent, "paragraph", yellContent, attributes);
+ }
+
+}
+//end::include[]

ADD
asciidoctorj-documentation/src/test/java/org/asciidoctor/integrationguide/extension/YellBlockProcessorWithPositionalAttributes.java
+package org.asciidoctor.integrationguide.extension;
+
+import org.asciidoctor.ast.ContentModel;
+import org.asciidoctor.ast.StructuralNode;
+import org.asciidoctor.extension.*;
+
+import java.util.Map;
+import java.util.stream.IntStream;
+
+import static java.util.stream.Collectors.joining;
+
+//tag::include[]
+@Name("yell")
+@PositionalAttributes({"loudness"}) // <1>
+@Contexts({Contexts.PARAGRAPH})
+@ContentModel(ContentModel.SIMPLE)
+public class YellBlockProcessorWithPositionalAttributes extends BlockProcessor {
+
+ @Override
+ public Object process(
+ StructuralNode parent, Reader reader, Map<String, Object> attributes) {
+
+ String content = reader.read();
+ String yellContent = content.toUpperCase();
+
+ String loudness = (String) attributes.get("loudness"); // <2>
+ if (loudness != null) {
+ yellContent += IntStream.range(0, Integer.parseInt(loudness))
+ .mapToObj(i -> "!")
+ .collect(joining());
+ }
+
+ return createBlock(parent, "paragraph", yellContent, attributes);
+ }
+
+}
+//end::include[]

ADD
asciidoctorj-documentation/src/test/resources/yell-block-attributes.adoc
+[yell,loudness=3]
+I really mean it

ADD
asciidoctorj-documentation/src/test/resources/yell-block-positional.adoc
+[yell,5]
+I really mean it

ADD
docs/modules/extensions/examples/org/asciidoctor/integrationguide/extension/YellBlockProcessorWithAttributes.java
+package org.asciidoctor.integrationguide.extension;
+
+import org.asciidoctor.ast.ContentModel;
+import org.asciidoctor.ast.StructuralNode;
+import org.asciidoctor.extension.BlockProcessor;
+import org.asciidoctor.extension.Contexts;
+import org.asciidoctor.extension.Name;
+import org.asciidoctor.extension.Reader;
+
+import java.util.Map;
+import java.util.stream.IntStream;
+
+import static java.util.stream.Collectors.joining;
+
+//tag::include[]
+@Name("yell")
+@Contexts({Contexts.PARAGRAPH})
+@ContentModel(ContentModel.SIMPLE)
+public class YellBlockProcessorWithAttributes extends BlockProcessor {
+
+ @Override
+ public Object process(
+ StructuralNode parent, Reader reader, Map<String, Object> attributes) {
+
+ String content = reader.read();
+ String yellContent = content.toUpperCase();
+
+ String loudness = (String) attributes.get("loudness"); // <1>
+ if (loudness != null) {
+ yellContent += IntStream.range(0, Integer.parseInt(loudness))
+ .mapToObj(i -> "!")
+ .collect(joining());
+ }
+
+ return createBlock(parent, "paragraph", yellContent, attributes);
+ }
+
+}
+//end::include[]

ADD
docs/modules/extensions/examples/org/asciidoctor/integrationguide/extension/YellBlockProcessorWithPositionalAttributes.java
+package org.asciidoctor.integrationguide.extension;
+
+import org.asciidoctor.ast.ContentModel;
+import org.asciidoctor.ast.StructuralNode;
+import org.asciidoctor.extension.*;
+
+import java.util.Map;
+import java.util.stream.IntStream;
+
+import static java.util.stream.Collectors.joining;
+
+//tag::include[]
+@Name("yell")
+@PositionalAttributes({"loudness"}) // <1>
+@Contexts({Contexts.PARAGRAPH})
+@ContentModel(ContentModel.SIMPLE)
+public class YellBlockProcessorWithPositionalAttributes extends BlockProcessor {
+
+ @Override
+ public Object process(
+ StructuralNode parent, Reader reader, Map<String, Object> attributes) {
+
+ String content = reader.read();
+ String yellContent = content.toUpperCase();
+
+ String loudness = (String) attributes.get("loudness"); // <2>
+ if (loudness != null) {
+ yellContent += IntStream.range(0, Integer.parseInt(loudness))
+ .mapToObj(i -> "!")
+ .collect(joining());
+ }
+
+ return createBlock(parent, "paragraph", yellContent, attributes);
+ }
+
+}
+//end::include[]

ADD
docs/modules/extensions/examples/yell-block-attributes.adoc
+[yell,loudness=3]
+I really mean it

ADD
docs/modules/extensions/examples/yell-block-positional.adoc
+[yell,5]
+I really mean it

Adding docs for accessing named and positional attributes in BlockProcessors