Skip to content

Commit

Permalink
Merge pull request asciidoctor#449 from robertpanzer/fix-448
Browse files Browse the repository at this point in the history
Fixes asciidoctor#448. Change parent node passed to InlineMacroProcessors to Con…
  • Loading branch information
robertpanzer committed Apr 16, 2016
2 parents 92b2405 + 532f50c commit 24872b9
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.asciidoctor.extension;

import org.asciidoctor.ast.StructuralNode;

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

public abstract class BlockMacroProcessor extends MacroProcessor {
public abstract class BlockMacroProcessor extends MacroProcessor<StructuralNode> {

public BlockMacroProcessor() {
this(null, new HashMap<String, Object>());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.asciidoctor.extension;

import org.asciidoctor.ast.ContentNode;

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

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

/**
* 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,11 @@
package org.asciidoctor.extension;

import org.asciidoctor.ast.ContentNode;

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

import org.asciidoctor.ast.StructuralNode;

public abstract class MacroProcessor extends Processor {
public abstract class MacroProcessor<T extends ContentNode> extends Processor {

protected String name;

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.asciidoctor.Options;
import org.asciidoctor.ast.*;
import org.asciidoctor.ast.impl.ColumnImpl;
import org.asciidoctor.ast.impl.ContentNodeImpl;
import org.asciidoctor.ast.impl.DocumentImpl;
import org.asciidoctor.ast.impl.RowImpl;
import org.asciidoctor.ast.impl.StructuralNodeImpl;
Expand Down Expand Up @@ -236,15 +237,15 @@ public Section createSection(StructuralNode parent, int level, boolean numbered,
return createSection(parent, Integer.valueOf(level), numbered, options);
}

public PhraseNode createPhraseNode(StructuralNode parent, String context, List<String> text) {
public PhraseNode createPhraseNode(ContentNode parent, String context, List<String> text) {
return createPhraseNode(parent, context, text, new HashMap<String, Object>());
}

public PhraseNode createPhraseNode(StructuralNode parent, String context, List<String> text, Map<String, Object> attributes) {
public PhraseNode createPhraseNode(ContentNode parent, String context, List<String> text, Map<String, Object> attributes) {
return createPhraseNode(parent, context, text, attributes, new HashMap<Object, Object>());
}

public PhraseNode createPhraseNode(StructuralNode parent, String context, List<String> text, Map<String, Object> attributes, Map<Object, Object> options) {
public PhraseNode createPhraseNode(ContentNode parent, String context, List<String> text, Map<String, Object> attributes, Map<Object, Object> options) {

Ruby rubyRuntime = JRubyRuntimeContext.get(parent);

Expand All @@ -257,22 +258,22 @@ public PhraseNode createPhraseNode(StructuralNode parent, String context, List<S
rubyText.addAll(text);

IRubyObject[] parameters = {
((StructuralNodeImpl) parent).getRubyObject(),
((ContentNodeImpl) parent).getRubyObject(),
RubyUtils.toSymbol(rubyRuntime, context),
rubyText,
convertMapToRubyHashWithSymbols };
return (PhraseNode) NodeConverter.createASTNode(rubyRuntime, INLINE_CLASS, parameters);
}

public PhraseNode createPhraseNode(StructuralNode parent, String context, String text) {
public PhraseNode createPhraseNode(ContentNode parent, String context, String text) {
return createPhraseNode(parent, context, text, new HashMap<String, Object>());
}

public PhraseNode createPhraseNode(StructuralNode parent, String context, String text, Map<String, Object> attributes) {
public PhraseNode createPhraseNode(ContentNode parent, String context, String text, Map<String, Object> attributes) {
return createPhraseNode(parent, context, text, attributes, new HashMap<String, Object>());
}

public PhraseNode createPhraseNode(StructuralNode parent, String context, String text, Map<String, Object> attributes, Map<String, Object> options) {
public PhraseNode createPhraseNode(ContentNode parent, String context, String text, Map<String, Object> attributes, Map<String, Object> options) {

Ruby rubyRuntime = JRubyRuntimeContext.get(parent);

Expand All @@ -281,7 +282,7 @@ public PhraseNode createPhraseNode(StructuralNode parent, String context, String
RubyHash convertedOptions = RubyHashUtil.convertMapToRubyHashWithSymbols(rubyRuntime, options);

IRubyObject[] parameters = {
((StructuralNodeImpl) parent).getRubyObject(),
((ContentNodeImpl) parent).getRubyObject(),
RubyUtils.toSymbol(rubyRuntime, context),
text == null ? rubyRuntime.getNil() : rubyRuntime.newString(text),
convertedOptions };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.asciidoctor.extension.processorproxies;

import org.asciidoctor.ast.StructuralNode;
import org.asciidoctor.ast.NodeConverter;
import org.asciidoctor.extension.InlineMacroProcessor;
import org.asciidoctor.internal.RubyHashMapDecorator;
Expand Down Expand Up @@ -114,7 +113,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(
(StructuralNode) NodeConverter.createASTNode(parent),
NodeConverter.createASTNode(parent),
RubyUtils.rubyToJava(getRuntime(), target, String.class),
RubyUtils.rubyToJava(getRuntime(), attributes, Map.class));
return convertProcessorResult(o);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.asciidoctor.extension

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

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

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

Object process(ContentNode 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,7 @@
package org.asciidoctor.extension

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

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

@Override
Object process(StructuralNode parent, String target, Map<String, Object> attributes) {
Object process(ContentNode 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
@@ -0,0 +1,65 @@
package org.asciidoctor.extension

import org.asciidoctor.Asciidoctor
import org.asciidoctor.OptionsBuilder
import org.jboss.arquillian.spock.ArquillianSputnik
import org.jboss.arquillian.test.api.ArquillianResource
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
import org.junit.runner.RunWith
import spock.lang.Issue
import spock.lang.Specification

@Issue('https://github.com/asciidoctor/asciidoctorj/issues/448')
@RunWith(ArquillianSputnik)
class WhenAnInlineMacroProcessorProcessesATable extends Specification {

public static final String UTF8 = 'UTF-8'
public static final String HREF = 'href'
public static final String ANCHOR_TAG = 'a'
public static final String TARGET = 'gittutorial.html'

@ArquillianResource
private Asciidoctor asciidoctor

private static final String INLINE_MACRO_DOCUMENT = '''= Test document
|===
| You can find infos on man:gittutorial[7] or man:git[8, 1].
|===
'''

private static final String INLINE_MACRO_DOCUMENT_ASCIIDOC_STYLE = '''= Test document
|===
a| You can find infos on man:gittutorial[7] or man:git[8, 1].
|===
'''


def "a InlineMacroProcessor should be able to process table cells"() {
when:
asciidoctor.javaExtensionRegistry().inlineMacro(AnnotatedLongInlineMacroProcessor)
String result = asciidoctor.convert(INLINE_MACRO_DOCUMENT, OptionsBuilder.options().headerFooter(false))

then:
Document doc = Jsoup.parse(result, UTF8)
Element link = doc.getElementsByTag(ANCHOR_TAG).first()
link.attr(HREF) == TARGET
}

def "a InlineMacroProcessor should be able to process table cells with asciidoc style"() {
when:
asciidoctor.javaExtensionRegistry().inlineMacro(AnnotatedLongInlineMacroProcessor)
String result = asciidoctor.convert(INLINE_MACRO_DOCUMENT_ASCIIDOC_STYLE, OptionsBuilder.options().headerFooter(false))

then:
Document doc = Jsoup.parse(result, UTF8)
Element link = doc.getElementsByTag(ANCHOR_TAG).first()
link.attr(HREF) == TARGET
}

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

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

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

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

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

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

@Override
public Object process(StructuralNode parent, String target, Map<String, Object> attributes) {
public Object process(ContentNode parent, String target, Map<String, Object> attributes) {
String[] items = target.split("\\|");
Map<String, Object> attrs = new HashMap<String, Object>();
attrs.put("menu", "Right click"); // <1>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package org.asciidoctor.integrationguide.extension;

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//tag::include[]
@Name("foo")
public class ImageInlineMacroProcessor extends InlineMacroProcessor {

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

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

//tag::include[]
import org.asciidoctor.ast.StructuralNode;

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

Expand All @@ -13,7 +14,7 @@ public class IssueInlineMacroProcessor extends InlineMacroProcessor { // <2>

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

String href =
new StringBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.asciidoctor.integrationguide.extension;

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

Expand All @@ -13,7 +13,7 @@
public class KeyboardInlineMacroProcessor extends InlineMacroProcessor {

@Override
public Object process(StructuralNode parent, String target, Map<String, Object> attributes) {
public Object process(ContentNode parent, String target, Map<String, Object> attributes) {
Map<String, Object> attrs = new HashMap<String, Object>();
attrs.put("keys", Arrays.asList("Ctrl", target)); // <1>
return createPhraseNode(parent, "kbd", (String) null, attrs) // <2>
Expand Down
11 changes: 6 additions & 5 deletions docs/integrator-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,8 @@ The InlineMacroProcessor for these macros looks like this:
.An InlineMacroProcessor that replaces issue macros with links
[source,java,indent=0]
----
import org.asciidoctor.ast.StructuralNode;
import org.asciidoctor.ast.ContentNode;
import org.asciidoctor.extension.InlineMacroProcessor;
import org.asciidoctor.extension.Name;
Expand All @@ -862,7 +863,7 @@ public class IssueInlineMacroProcessor extends InlineMacroProcessor { // <2>
@Override
public Object process( // <3>
StructuralNode parent, String target, Map<String, Object> attributes) {
ContentNode parent, String target, Map<String, Object> attributes) {
String href =
new StringBuilder()
Expand Down Expand Up @@ -933,7 +934,7 @@ The example assumes that the macro is called with the macro name `ctrl` and a ke
public class KeyboardInlineMacroProcessor extends InlineMacroProcessor {
@Override
public Object process(StructuralNode parent, String target, Map<String, Object> attributes) {
public Object process(ContentNode parent, String target, Map<String, Object> attributes) {
Map<String, Object> attrs = new HashMap<String, Object>();
attrs.put("keys", Arrays.asList("Ctrl", target)); // <1>
return createPhraseNode(parent, "kbd", (String) null, attrs) // <2>
Expand All @@ -956,7 +957,7 @@ The following processor would render the macro `rightclick:New|Class[]` like thi
public class ContextMenuInlineMacroProcessor extends InlineMacroProcessor {
@Override
public Object process(StructuralNode parent, String target, Map<String, Object> attributes) {
public Object process(ContentNode parent, String target, Map<String, Object> attributes) {
String[] items = target.split("\\|");
Map<String, Object> attrs = new HashMap<String, Object>();
attrs.put("menu", "Right click"); // <1>
Expand Down Expand Up @@ -989,7 +990,7 @@ That means the MacroProcessor should replace the macro `foo:1234` to an image el
public class ImageInlineMacroProcessor extends InlineMacroProcessor {
@Override
public Object process(StructuralNode parent, String target, Map<String, Object> attributes) {
public Object process(ContentNode parent, String target, Map<String, Object> attributes) {
Map<String, Object> options = new HashMap<String, Object>();
options.put("type", "image"); // <1>
Expand Down

0 comments on commit 24872b9

Please sign in to comment.