Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files Browse the repository at this point in the history
Create mutable copies of options passed to Processor.create...()
robertpanzer committed Jun 18, 2023

Verified

This commit was signed with the committer’s verified signature.
krassowski Michał Krassowski
1 parent e2994e0 commit e59b263
Showing 4 changed files with 30 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -159,30 +159,33 @@ public Cell createTableCell(Column column, String text, Map<String, Object> attr
public Block createBlock(StructuralNode parent, String context, String content, Map<String, Object> attributes,
Map<Object, Object> options) {

options.put(Options.SOURCE, content);
options.put(Options.ATTRIBUTES, attributes);
Map<Object, Object> optionsCopy = new HashMap<>(options);
optionsCopy.put(Options.SOURCE, content);
optionsCopy.put(Options.ATTRIBUTES, attributes);

return createBlock(parent, context, options);
return createBlock(parent, context, optionsCopy);
}

@Override
public Block createBlock(StructuralNode parent, String context, List<String> content, Map<String, Object> attributes,
Map<Object, Object> options) {

options.put(Options.SOURCE, content);
options.put(Options.ATTRIBUTES, new HashMap<>(attributes));
return createBlock(parent, context, options);
Map<Object, Object> optionsCopy = new HashMap<>(options);
optionsCopy.put(Options.SOURCE, content);
optionsCopy.put(Options.ATTRIBUTES, new HashMap<>(attributes));
return createBlock(parent, context, optionsCopy);
}

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

Ruby rubyRuntime = JRubyRuntimeContext.get(parent);

options.put(Options.ATTRIBUTES, attributes);
Map<Object, Object> optionsCopy = new HashMap<>(options);
optionsCopy.put(Options.ATTRIBUTES, attributes);

RubyHash convertMapToRubyHashWithSymbols = RubyHashUtil.convertMapToRubyHashWithSymbolsIfNecessary(rubyRuntime,
options);
optionsCopy);

RubyArray rubyText = rubyRuntime.newArray();
rubyText.addAll(text);
@@ -200,9 +203,10 @@ public PhraseNode createPhraseNode(ContentNode parent, String context, String te

Ruby rubyRuntime = JRubyRuntimeContext.get(parent);

options.put(Options.ATTRIBUTES, RubyHashUtil.convertMapToRubyHashWithStrings(rubyRuntime, attributes));
Map<String, Object> optionsCopy = new HashMap<>(options);
optionsCopy.put(Options.ATTRIBUTES, RubyHashUtil.convertMapToRubyHashWithStrings(rubyRuntime, attributes));

RubyHash convertedOptions = RubyHashUtil.convertMapToRubyHashWithSymbols(rubyRuntime, options);
RubyHash convertedOptions = RubyHashUtil.convertMapToRubyHashWithSymbols(rubyRuntime, optionsCopy);

IRubyObject[] parameters = {
((ContentNodeImpl) parent).getRubyObject(),
@@ -306,8 +310,9 @@ public org.asciidoctor.ast.List createList(StructuralNode parent, String context
Map<String, Object> attributes,
Map<Object, Object> options) {

options.put(Options.ATTRIBUTES, new HashMap<>(attributes));
return createList(parent, context, options);
HashMap<Object, Object> optionsCopy = new HashMap<>(options);
optionsCopy.put(Options.ATTRIBUTES, new HashMap<>(attributes));
return createList(parent, context, optionsCopy);
}

@Override
@@ -330,14 +335,14 @@ public org.asciidoctor.ast.List createList(StructuralNode parent, String context
public ListItem createListItem(final org.asciidoctor.ast.List parent, final String text) {
Ruby rubyRuntime = JRubyRuntimeContext.get(parent);

return (ListItem) NodeConverter.createASTNode(rubyRuntime, NodeConverter.NodeType.LIST_ITEM_CLASS, ListImpl.class.cast(parent).getRubyObject(), rubyRuntime.newString(text));
return (ListItem) NodeConverter.createASTNode(rubyRuntime, NodeConverter.NodeType.LIST_ITEM_CLASS, ((ListImpl) parent).getRubyObject(), rubyRuntime.newString(text));
}

@Override
public ListItem createListItem(final org.asciidoctor.ast.DescriptionList parent, final String text) {
Ruby rubyRuntime = JRubyRuntimeContext.get(parent);

return (ListItem) NodeConverter.createASTNode(rubyRuntime, NodeConverter.NodeType.LIST_ITEM_CLASS, DescriptionListImpl.class.cast(parent).getRubyObject(), rubyRuntime.newString(text));
return (ListItem) NodeConverter.createASTNode(rubyRuntime, NodeConverter.NodeType.LIST_ITEM_CLASS, ((DescriptionListImpl) parent).getRubyObject(), rubyRuntime.newString(text));
}

/**
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
package org.asciidoctor.jruby.internal;

import java.io.InputStream;

import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubySymbol;
import org.jruby.javasupport.JavaClass;
import org.jruby.runtime.builtin.IRubyObject;

import java.io.InputStream;

public class RubyUtils {

public static <T> T rubyToJava(Ruby runtime, IRubyObject rubyObject, Class<T> returnType) {
return (T) org.jruby.javasupport.JavaEmbedUtils.rubyToJava(runtime, rubyObject, returnType);
}

public static RubySymbol toSymbol(Ruby rubyRuntime, String key) {
RubySymbol newSymbol = RubySymbol.newSymbol(rubyRuntime, key);
return newSymbol;
}

public static RubyClass toRubyClass(Ruby rubyRuntime, Class<?> rubyClass) {
return JavaClass.get(rubyRuntime, rubyClass).getProxyClass();
return RubySymbol.newSymbol(rubyRuntime, key);
}

public static void requireLibrary(Ruby rubyRuntime, String require) {
@@ -32,7 +25,7 @@ public static void loadRubyClass(Ruby rubyRuntime, InputStream rubyClassDefiniti
rubyRuntime.evalScriptlet(script);
}

public static final void setGlobalVariable(Ruby rubyRuntime, String variableName, Object variableValue) {
public static void setGlobalVariable(Ruby rubyRuntime, String variableName, Object variableValue) {
String script = String.format("$%s = %s", variableName, variableValue);
rubyRuntime.evalScriptlet(script);
}
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@
import org.asciidoctor.ast.PhraseNode;
import org.asciidoctor.ast.StructuralNode;

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

public class ManpageMacro extends InlineMacroProcessor {
@@ -19,9 +18,9 @@ public ManpageMacro(String macroName, Map<String, Object> config) {
@Override
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");
Map<String, Object> options = Map.of(
"type", ":link",
"target", target + ".html");
return createPhraseNode(parent, "anchor", target, attributes, options);
}

Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
import org.asciidoctor.ast.Document;
import org.asciidoctor.ast.StructuralNode;

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

@@ -32,7 +32,7 @@ private void processBlock(StructuralNode block) {

for (int i = 0; i < blocks.size(); i++) {
final StructuralNode currentBlock = blocks.get(i);
if (currentBlock instanceof StructuralNode) {
if (currentBlock != null) {
if ("paragraph".equals(currentBlock.getContext())) {
List<String> lines = ((Block) currentBlock).getLines();
if (lines.size() > 0 && lines.get(0).startsWith("$")) {
@@ -57,15 +57,15 @@ public Block convertToTerminalListing(Block block) {
for (String line : lines) {
if (line.startsWith("$")) {
resultLines.append("<span class=\"command\">")
.append(line.substring(2, line.length()))
.append(line.substring(2))
.append("</span>");
} else {
resultLines.append(line);
}
}

return createBlock(this.document, "listing",
List.of(resultLines.toString()), attributes, new HashMap<>());
List.of(resultLines.toString()), attributes, Collections.emptyMap());
}

}

0 comments on commit e59b263

Please sign in to comment.