Skip to content

Commit

Permalink
Merge pull request #1084 from robertpanzer/macro-apis
Browse files Browse the repository at this point in the history
Fix Macro APIs to return Structural or Phrase Nodes instead of java.l…
  • Loading branch information
robertpanzer authored Aug 20, 2022
2 parents 6346baa + 3576af4 commit 3d1d047
Show file tree
Hide file tree
Showing 34 changed files with 122 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.asciidoctor.extension;

import org.asciidoctor.ast.ContentNode;
import org.asciidoctor.ast.PhraseNode;

import java.util.HashMap;
import java.util.Map;

public abstract class InlineMacroProcessor extends MacroProcessor<ContentNode> {
public abstract class InlineMacroProcessor extends MacroProcessor<PhraseNode> {

/**
* This value is used as the config option key when defining a regular expression that should be
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.asciidoctor.extension;

import org.asciidoctor.ast.ContentNode;
import org.asciidoctor.ast.StructuralNode;

import java.util.HashMap;
import java.util.Map;

public abstract class MacroProcessor<T extends ContentNode> extends BaseProcessor {
public abstract class MacroProcessor<R extends ContentNode> extends BaseProcessor {

protected String name;

Expand All @@ -30,6 +31,6 @@ public Map<Object, Object> options() {
return new HashMap<>();
}

public abstract Object process(T parent, String target, Map<String, Object> attributes);
public abstract R process(StructuralNode parent, String target, Map<String, Object> attributes);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.asciidoctor.jruby.extension.processorproxies;

import org.asciidoctor.ast.StructuralNode;
import org.asciidoctor.extension.InlineMacroProcessor;
import org.asciidoctor.jruby.ast.impl.NodeConverter;
import org.asciidoctor.jruby.internal.*;
Expand Down Expand Up @@ -67,7 +68,7 @@ public IRubyObject initialize(ThreadContext context, IRubyObject[] args) throws
// If options contains a String with a Regexp create the RubyRegexp from it
RubyHash rubyConfig = RubyHashUtil.convertMapToRubyHashWithSymbols(getRuntime(), getProcessor().getConfig());
Object regexp = getProcessor().getConfig().get("regexp");
if (regexp != null && regexp instanceof CharSequence) {
if (regexp instanceof CharSequence) {
RubySymbol regexpSymbol = RubySymbol.newSymbol(getRuntime(), "regexp");
rubyConfig.put(regexpSymbol, convertRegexp(getRuntime(), (CharSequence) regexp));
}
Expand Down Expand Up @@ -121,7 +122,7 @@ public IRubyObject initialize(ThreadContext context, IRubyObject[] args) throws
@JRubyMethod(name = "process", required = 3)
public IRubyObject process(ThreadContext context, IRubyObject parent, IRubyObject target, IRubyObject attributes) {
Object o = getProcessor().process(
NodeConverter.createASTNode(parent),
(StructuralNode) NodeConverter.createASTNode(parent),
RubyUtils.rubyToJava(getRuntime(), target, String.class),
new RubyAttributesMapDecorator((RubyHash) attributes));
return convertProcessorResult(o);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AnnotatedBlockMacroProcessor extends BlockMacroProcessor {
public static final String RESULT = 'This content is added by this macro!'

@Override
Object process(StructuralNode parent, String target, Map<String, Object> attributes) {
StructuralNode process(StructuralNode parent, String target, Map<String, Object> attributes) {
createBlock(parent, 'paragraph', RESULT)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.asciidoctor.extension

import groovy.transform.CompileStatic
import org.asciidoctor.ast.ContentNode
import org.asciidoctor.ast.PhraseNode
import org.asciidoctor.ast.StructuralNode

@CompileStatic
@Name('man')
Expand All @@ -13,7 +14,7 @@ class AnnotatedLongInlineMacroProcessor extends InlineMacroProcessor {
public static final String SECTION = 'section'

@Override
Object process(ContentNode parent, String target, Map<String, Object> attributes) {
PhraseNode process(StructuralNode parent, String target, Map<String, Object> attributes) {
assert attributes[SECTION] == '7' || ( attributes[SECTION] == '8' || attributes[SUBSECTION] == '1')

Map<String, Object> options = new HashMap<String, Object>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.asciidoctor.extension

import groovy.transform.CompileStatic
import org.asciidoctor.ast.ContentNode
import org.asciidoctor.ast.PhraseNode
import org.asciidoctor.ast.StructuralNode

@CompileStatic
@Name('man')
Expand All @@ -13,7 +14,7 @@ class AnnotatedRegexpInlineMacroProcessor extends InlineMacroProcessor {
}

@Override
Object process(ContentNode parent, String target, Map<String, Object> attributes) {
PhraseNode process(StructuralNode parent, String target, Map<String, Object> attributes) {
Map<String, Object> options = new HashMap<String, Object>()
options['type'] = ':link'
options['target'] = "${target}.html"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class GithubContributorsBlockMacro extends BlockMacroProcessor {
}

@Override
Object process(StructuralNode parent, String target, Map<String, Object> attributes) {
StructuralNode process(StructuralNode parent, String target, Map<String, Object> attributes) {
URL url = new URL("http://api.github.com/repos/${target}/contributors")
URLConnection connection = url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress('localhost', TestHttpServer.instance.localPort)))
String content = connection.inputStream.text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ListCreatorBlockMacro extends BlockMacroProcessor {
}

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

def attrs = new HashMap<String, Object>()
attrs['start'] = '42'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class SectionCreatorBlockMacro extends BlockMacroProcessor {
public static final String CONTENT = 'This is just some text'

@Override
Object process(StructuralNode parent, String target, Map<String, Object> attributes) {
StructuralNode process(StructuralNode parent, String target, Map<String, Object> attributes) {
Section section = createSection(parent)
section.title = target

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ testmacro::countera[]
static class TestBlockMacroProcessor extends BlockMacroProcessor {

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

String text = "This is macro call ${parent.document.getAndIncrementCounter(target)} for ${target}"
// Have to do this to interact successfully with counters also used in the document
Expand All @@ -143,7 +143,7 @@ testmacro::countera[]
static class TestBlockMacroWithInitialCounterProcessor extends BlockMacroProcessor {

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

String text = "This is macro call ${parent.document.getAndIncrementCounter(target, 42)} for ${target}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ testmacro::target[]

asciidoctor.javaExtensionRegistry().blockMacro(new BlockMacroProcessor('testmacro'){
@Override
Object process(StructuralNode parent, String target, Map<String, Object> attributes) {
StructuralNode process(StructuralNode parent, String target, Map<String, Object> attributes) {
createBlock(parent, 'paragraph', expectedContent, [:])
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private String document(String blockName, String text) {

public static class AbstractTestProcessor extends BlockMacroProcessor {
@Override
public Object process(StructuralNode parent, String target, Map<String, Object> attributes) {
public StructuralNode process(StructuralNode parent, String target, Map<String, Object> attributes) {
return createBlock(parent, "paragraph", target.toUpperCase());
}
}
Expand All @@ -48,7 +48,7 @@ public TestProcessorWithName() {
}

@Override
public Object process(StructuralNode parent, String target, Map<String, Object> attributes) {
public StructuralNode process(StructuralNode parent, String target, Map<String, Object> attributes) {
return createBlock(parent, "paragraph", target.toUpperCase());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import org.asciidoctor.Asciidoctor;
import org.asciidoctor.OptionsBuilder;
import org.asciidoctor.ast.ContentNode;
import org.asciidoctor.ast.PhraseNode;
import org.asciidoctor.ast.StructuralNode;
import org.asciidoctor.jruby.internal.AsciidoctorCoreException;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
Expand Down Expand Up @@ -31,7 +32,7 @@ private String document(String blockName, String text) {

public static class AbstractTestProcessor extends InlineMacroProcessor {
@Override
public Object process(ContentNode parent, String target, Map<String, Object> attributes) {
public PhraseNode process(StructuralNode parent, String target, Map<String, Object> attributes) {
String transformed = target.chars()
.mapToObj(c -> Character.isUpperCase(c) ? " " + (char) c : Character.toString((char) c))
.collect(joining())
Expand All @@ -49,7 +50,7 @@ public TestProcessorWithName() {
}

@Override
public Object process(ContentNode parent, String target, Map<String, Object> attributes) {
public PhraseNode process(StructuralNode parent, String target, Map<String, Object> attributes) {
String transformed = target.chars()
.mapToObj(c -> Character.isUpperCase(c) ? " " + (char) c : Character.toString((char) c))
.collect(joining())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.asciidoctor.extension;

import org.asciidoctor.ast.ContentNode;
import org.asciidoctor.ast.PhraseNode;
import org.asciidoctor.ast.StructuralNode;

import java.util.HashMap;
Expand All @@ -17,8 +17,8 @@ public ManpageMacro(String macroName, Map<String, Object> config) {
}

@Override
public Object process(ContentNode parent, String target,
Map<String, Object> attributes) {
public PhraseNode process(StructuralNode parent, String target,
Map<String, Object> attributes) {
Map<String, Object> options = new HashMap<String, Object>();
options.put("type", ":link");
options.put("target", target + ".html");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.asciidoctor.extension;

import org.asciidoctor.ast.ContentNode;
import org.asciidoctor.ast.PhraseNode;
import org.asciidoctor.ast.StructuralNode;

import java.util.HashMap;
Expand All @@ -17,7 +17,7 @@ public SayMacro(String macroName, Map<String, Object> config) {
}

@Override
public Object process(ContentNode parent, String target, Map<String, Object> attributes) {
public PhraseNode process(StructuralNode parent, String target, Map<String, Object> attributes) {
String text = "*" + target + "*";
Map<String, Object> phraseNodeAttributes = new HashMap<>();
phraseNodeAttributes.put("subs", ":normal");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.asciidoctor.extension;

import org.asciidoctor.Asciidoctor;
import org.asciidoctor.ast.ContentNode;
import org.asciidoctor.ast.PhraseNode;
import org.asciidoctor.ast.StructuralNode;
import org.junit.Test;

import java.util.Collections;
Expand All @@ -20,7 +21,7 @@ public void inlineMacroAttributes() {
asciidoctor.javaExtensionRegistry().inlineMacro(new InlineMacroProcessor("example") {

@Override
public Object process(ContentNode parent, String target, Map<String, Object> attributes) {
public PhraseNode process(StructuralNode parent, String target, Map<String, Object> attributes) {
return createPhraseNode(parent, "quoted", attributes.toString(), attributes, new HashMap<>());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.asciidoctor.integrationguide.extension;

import org.asciidoctor.Asciidoctor;
import org.asciidoctor.OptionsBuilder;
import org.asciidoctor.Options;
import org.asciidoctor.util.ClasspathResources;
import org.hamcrest.Matcher;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.junit.Test;
Expand Down Expand Up @@ -39,8 +38,8 @@ public void should_render_comments_as_notes() {
//tag::include[]
asciidoctor.javaExtensionRegistry().preprocessor(CommentPreprocessor.class); // <1>

String result1 = asciidoctor.convertFile(comment_adoc, OptionsBuilder.options().toFile(false));
String result2 = asciidoctor.convertFile(comment_with_note_adoc, OptionsBuilder.options().toFile(false));
String result1 = asciidoctor.convertFile(comment_adoc, Options.builder().toFile(false).build());
String result2 = asciidoctor.convertFile(comment_with_note_adoc, Options.builder().toFile(false).build());

assertThat(result1, is(result2)); // <2>
//end::include[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.asciidoctor.integrationguide.extension;

import org.asciidoctor.ast.ContentNode;
import org.asciidoctor.ast.PhraseNode;
import org.asciidoctor.ast.StructuralNode;
import org.asciidoctor.extension.InlineMacroProcessor;
import org.asciidoctor.extension.Name;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -14,14 +16,11 @@
public class ContextMenuInlineMacroProcessor extends InlineMacroProcessor {

@Override
public Object process(ContentNode parent, String target, Map<String, Object> attributes) {
public PhraseNode process(StructuralNode parent, String target, Map<String, Object> attributes) {
String[] items = target.split("\\|");
Map<String, Object> attrs = new HashMap<String, Object>();
Map<String, Object> attrs = new HashMap<>();
attrs.put("menu", "Right click"); // <1>
List<String> submenus = new ArrayList<String>();
for (int i = 0; i < items.length - 1; i++) {
submenus.add(items[i]);
}
List<String> submenus = Arrays.asList(items).subList(0, items.length - 1);
attrs.put("submenus", submenus);
attrs.put("menuitem", items[items.length - 1]);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.asciidoctor.integrationguide.extension;

import org.asciidoctor.Asciidoctor;
import org.asciidoctor.OptionsBuilder;
import org.asciidoctor.Options;
import org.asciidoctor.SafeMode;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
Expand All @@ -22,7 +22,7 @@ public void should_create_context_menu() {

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

String result = asciidoctor.convert("rightclick:New|Class[]", OptionsBuilder.options().toFile(false).safe(SafeMode.UNSAFE));
String result = asciidoctor.convert("rightclick:New|Class[]", Options.builder().toFile(false).safe(SafeMode.UNSAFE).build());


assertThat(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.asciidoctor.integrationguide.extension;

import org.asciidoctor.Asciidoctor;
import org.asciidoctor.OptionsBuilder;
import org.asciidoctor.Options;
import org.asciidoctor.util.ClasspathResources;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
Expand Down Expand Up @@ -35,9 +35,10 @@ public void should_render_comments_as_notes() {

String result =
asciidoctor.convertFile(doc,
OptionsBuilder.options()
Options.builder()
.headerFooter(true) // <2>
.toFile(false));
.toFile(false)
.build());

assertThat(result, containsString(CopyrightFooterPostprocessor.COPYRIGHT_NOTICE));
//end::include[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class GistBlockMacroPositionalAttributesProcessor extends BlockMacroProcessor {

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

String script;
String provider = (String) attributes.get("provider");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class GistBlockMacroProcessor extends BlockMacroProcessor { // <2>

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

String content = new StringBuilder()
Expand All @@ -23,7 +23,7 @@ public Object process( // <3>
.append("</div>")
.append("</div>").toString();

return createBlock(parent, "pass", content); // <5>
return createBlock(parent, "pass", content); // <5>
}

}
Expand Down
Loading

0 comments on commit 3d1d047

Please sign in to comment.