Skip to content

Commit

Permalink
Merge pull request #1072 from asciidoctor/docs-extension-attributes
Browse files Browse the repository at this point in the history
Adding docs for accessing named and positional attributes in BlockPro…
  • Loading branch information
robertpanzer authored Jan 5, 2022
2 parents 8351743 + 8914ebd commit 5765549
Show file tree
Hide file tree
Showing 69 changed files with 909 additions and 1,506 deletions.
89 changes: 0 additions & 89 deletions asciidoctorj-documentation/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,95 +22,6 @@ compileTestJava {
}
}

task copyBaseExamplesToDocs(type: Copy) {
group = 'documentation'

from('src/test/java') {
include '**/AsciidoctorInterface.java'
include '**/OptionsTest.java'
include '**/SimpleAsciidoctorRendering.java'

include '**/converter/TextConverter.java'
include '**/converter/TextConverterTest.java'
include '**/converter/TextConverterRegistry.java'
}
from('src/test/resources/') {
include 'converterregistry/META-INF/services/org.asciidoctor.jruby.converter.spi.ConverterRegistry'
}

into rootProject.file('docs/modules/ROOT/examples')
}

task copyExtensionsExamplesToDocs(type: Copy) {
group = 'documentation'

from('src/test/java') {
include '**/extension/CommentPreprocessor.java'
include '**/extension/ContextMenuInlineMacroProcessor.java'
include '**/extension/CopyrightFooterPostprocessor.java'
include '**/extension/GistBlockMacroProcessor.java'
include '**/extension/ImageInlineMacroProcessor.java'
include '**/extension/IssueInlineMacroProcessor.java'
include '**/extension/KeyboardInlineMacroProcessor.java'
include '**/extension/LoggingBlockMacroProcessor.java'
include '**/extension/LsIncludeProcessor.java'
include '**/extension/RobotsDocinfoProcessor.java'
include '**/extension/TerminalCommandExtension.java'
include '**/extension/TerminalCommandTreeprocessor.java'
include '**/extension/YellBlockProcessor.java'

}
from('src/test/resources/') {
include 'ast-demo.adoc'
include 'ast-demo-result.txt'
include 'comment.adoc'
include 'comment-with-note.adoc'
include 'gist-macro.adoc'
include 'logging-macro.adoc'
include 'issue-inline-macro.adoc'
include 'treeprocessorcontent.adoc'
include 'treeprocessorresult.adoc'
include 'yell-block.adoc'
include 'yell-block-result.txt'

include 'extensionregistry/META-INF/services/org.asciidoctor.jruby.extension.spi.ExtensionRegistry'
}

into rootProject.file('docs/modules/extensions/examples')
}

task copySyntaxHighlightingExamplesToDocs(type: Copy) {
group = 'documentation'

from('src/test/java') {
include '**/syntaxhighlighter/HighlightJsExtension.java'
include '**/syntaxhighlighter/HighlightJsHighlighter.java'
include '**/syntaxhighlighter/HighlightJsHighlighterTest.java'
include '**/syntaxhighlighter/HighlightJsWithLanguageHighlighter.java'
include '**/syntaxhighlighter/HighlightJsWithOfflineStylesHighlighter.java'
include '**/syntaxhighlighter/PrismJsHighlighter.java'
include '**/syntaxhighlighter/PrismJsHighlighterTest.java'
include '**/syntaxhighlighter/threeparams/HighlightJsHighlighter.java'
}
from('src/test/resources/') {
include 'sources.adoc'
include 'syntax-highlighting-order.adoc'
include 'syntax-highlighting-order-output.txt'
include 'syntaxhighlighterregistry/META-INF/services/org.asciidoctor.jruby.syntaxhighlighter.spi.SyntaxHighlighterRegistry'
}

into rootProject.file('docs/modules/syntax-highlighting/examples')
}

task copyAllExamplesToDocs {
group = 'documentation'

dependsOn copyBaseExamplesToDocs
dependsOn copyExtensionsExamplesToDocs
dependsOn copySyntaxHighlightingExamplesToDocs
}

build.dependsOn copyBaseExamplesToDocs

test.enabled = testClasses.enabled = JavaVersion.current().isJava7Compatible()

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.asciidoctor.integrationguide.extension;

//tag::include[]
import org.asciidoctor.ast.StructuralNode;
import org.asciidoctor.extension.BlockMacroProcessor;
import org.asciidoctor.extension.Name;
import org.asciidoctor.extension.PositionalAttributes;

import java.util.Map;

@Name("gist")
@PositionalAttributes({"provider", "repo"}) // <1>
public class GistBlockMacroPositionalAttributesProcessor extends BlockMacroProcessor {

@Override
public Object process(StructuralNode parent, String target, Map<String, Object> attributes) {

String script;
String provider = (String) attributes.get("provider");
if (provider == null || "github".equals(provider)) { // <2>
script = String.format("<script src=\"https://gist.github.com/%s.js\"/></script>", target);
} else if ("gitlab".equals(provider)) {
String repo = (String) attributes.get("repo");
if (repo == null) {
script = String.format("<script src=\"https://gitlab.com/-/snippets/%s.js\"></script>", target);
} else {
script = String.format("<script src=\"https://gitlab.com/%s/-/snippets/%s.js\"></script>", repo, target);
}
} else {
throw new IllegalArgumentException("Unknown provider " + provider);
}

String content = new StringBuilder()
.append("<div class=\"openblock gist\">")
.append("<div class=\"content\">")
.append(script)
.append("</div>")
.append("</div>").toString();

return createBlock(parent, "pass", content);
}

}
//end::include[]
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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;
Expand Down Expand Up @@ -32,7 +33,7 @@ public void should_create_script_element_for_block_macro() {
//tag::include[]
asciidoctor.javaExtensionRegistry().blockMacro(GistBlockMacroProcessor.class); // <1>

String result = asciidoctor.convertFile(gistmacro_adoc, OptionsBuilder.options().toFile(false));
String result = asciidoctor.convertFile(gistmacro_adoc, Options.builder().toFile(false).build());

assertThat(
result,
Expand All @@ -41,5 +42,31 @@ public void should_create_script_element_for_block_macro() {
//end::include[]
}

@Test
public void should_create_script_element_for_block_macro_with_positional_attributes() {

File gistmacro_adoc = classpathResources.getResource("gist-macro-attributes.adoc");
asciidoctor.javaExtensionRegistry().blockMacro(GistBlockMacroPositionalAttributesProcessor.class);

String result = asciidoctor.convertFile(gistmacro_adoc, Options.builder().toFile(false).build());

assertThat(
result,
containsString(
"<script src=\"https://gist.github.com/myaccount/1234abcd.js\"/></script>"));
assertThat(
result,
containsString(
"<script src=\"https://gitlab.com/-/snippets/2228798.js\"></script>"));
assertThat(
result,
containsString(
"<script src=\"https://gitlab.com/gitlab-org/gitlab-foss/-/snippets/1717978.js\"></script>"));
assertThat(
result,
containsString(
"<script src=\"https://gitlab.com/gitlab-org/gitlab-foss/-/snippets/1717979.js\"></script>"));
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.asciidoctor.integrationguide.extension;

//tag::include[]
import org.asciidoctor.ast.ContentNode;
import org.asciidoctor.extension.InlineMacroProcessor;
import org.asciidoctor.extension.Name;
import org.asciidoctor.extension.PositionalAttributes;

//end::include[]
import java.util.HashMap;
import java.util.Map;

//tag::include[]
@Name("issue")
@PositionalAttributes({"repo"}) // <1>
public class IssueInlineMacroPositionalAttributesProcessor extends InlineMacroProcessor {

@Override
public Object process(ContentNode parent, String target, Map<String, Object> attributes) {

String href =
new StringBuilder()
.append("https://github.com/")
.append(attributes.containsKey("repo") ?
attributes.get("repo") :
parent.getDocument().getAttribute("repo"))
.append("/issues/")
.append(target).toString();

Map<String, Object> options = new HashMap<>();
options.put("type", ":link");
options.put("target", href);
return createPhraseNode(parent, "anchor", target, attributes, options); // <4>
}

}
//end::include[]
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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;
Expand Down Expand Up @@ -47,5 +48,27 @@ public void should_create_anchor_elements_for_inline_macros() {
//end::include[]
}

@Test
public void should_create_anchor_elements_for_inline_macros_with_positional_attributes() {

File issueinlinemacro_adoc = //...
classpathResources.getResource("issue-inline-macro-positional.adoc");

asciidoctor.javaExtensionRegistry().inlineMacro(IssueInlineMacroPositionalAttributesProcessor.class);

String result = asciidoctor.convertFile(issueinlinemacro_adoc, Options.builder().toFile(false).build());

assertThat(
result,
containsString(
"<a href=\"https://github.com/asciidoctor/asciidoctorj/issues/334\"")); // <2>

assertThat(
result,
containsString( // <2>
"<a href=\"https://github.com/asciidoctor/asciidoctorj-groovy-dsl/issues/3\""));

}


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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;
Expand Down Expand Up @@ -38,4 +39,28 @@ public void should_invoke_block_processor() throws Exception {
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!!!!!"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
== Gists

gist::myaccount/1234abcd[]

gist::2228798[gitlab]

gist::1717978[gitlab,gitlab-org/gitlab-foss]

gist::1717979[gitlab,repo=gitlab-org/gitlab-foss]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
= InlineMacroProcessor Test Document
:repo: asciidoctor/asciidoctorj-groovy-dsl

You might want to take a look at the issue issue:334[asciidoctor/asciidoctorj] and issue:3[].
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[yell,loudness=3]
I really mean it
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[yell,5]
I really mean it
Loading

0 comments on commit 5765549

Please sign in to comment.