Fixes Add method StructuralNode.setLevel()
MODIFY
CHANGELOG.adoc
CHANGELOG.adoc
@@ -13,6 +13,10 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[
== Unreleased
+Improvement::
+
+ * Add method StructuralNode.setLevel() (@Mogztter) (#931)
+
== 2.3.1 (2020-06-17)
Bug Fixes::

MODIFY
asciidoctorj-api/src/main/java/org/asciidoctor/ast/StructuralNode.java
asciidoctorj-api/src/main/java/org/asciidoctor/ast/StructuralNode.java
@@ -87,8 +87,11 @@ public interface StructuralNode extends ContentNode {
Object getContent();
String convert();
List<StructuralNode> findBy(Map<Object, Object> selector);
+
int getLevel();
+ void setLevel(int level);
+
/**
* Returns the content model.
*

MODIFY
asciidoctorj-core/src/main/java/org/asciidoctor/jruby/ast/impl/StructuralNodeImpl.java
asciidoctorj-core/src/main/java/org/asciidoctor/jruby/ast/impl/StructuralNodeImpl.java
@@ -97,6 +97,11 @@ public class StructuralNodeImpl extends ContentNodeImpl implements StructuralNod
return getInt("level");
}
+ @Override
+ public void setLevel(int level) {
+ setInt("level", level);
+ }
+
@Override
public Cursor getSourceLocation() {
IRubyObject object = getRubyProperty("source_location");

MODIFY
asciidoctorj-core/src/main/java/org/asciidoctor/jruby/internal/RubyObjectWrapper.java
asciidoctorj-core/src/main/java/org/asciidoctor/jruby/internal/RubyObjectWrapper.java
@@ -86,10 +86,15 @@ public class RubyObjectWrapper {
if (result instanceof RubyNil) {
return 0;
} else {
- return (int) ((RubyNumeric) result).getLongValue();
+ return (int) ((RubyNumeric) result).getIntValue();
}
}
+ public void setInt(String propertyName, int value) {
+ setRubyProperty(propertyName, runtime.newFixnum(value));
+ }
+
+
public <T> List<T> getList(String propertyName, Class<T> elementClass, Object... args) {
IRubyObject result = getRubyProperty(propertyName, args);
if (result instanceof RubyNil) {

ADD
asciidoctorj-core/src/test/java/org/asciidoctor/converter/LevelConverter.java
+package org.asciidoctor.converter;
+
+import org.asciidoctor.ast.ContentNode;
+import org.asciidoctor.ast.Document;
+import org.asciidoctor.ast.Section;
+
+import java.util.Map;
+
+public class LevelConverter extends StringConverter {
+
+ public LevelConverter(String backend, Map<String, Object> opts) { // <2>
+ super(backend, opts);
+ }
+
+ @Override
+ public String convert(
+ ContentNode node, String transform, Map<Object, Object> o) {
+
+ if (node instanceof Document) {
+ Document document = (Document) node;
+ return document.getContent().toString();
+ } else if (node instanceof Section) {
+ Section section = (Section) node;
+ return new StringBuilder()
+ .append("== ").append(section.getLevel()).append(" ").append(section.getTitle()).append(" ==")
+ .toString();
+ }
+ return null;
+ }
+
+}

MODIFY
asciidoctorj-core/src/test/java/org/asciidoctor/converter/WhenConverterIsRegistered.java
asciidoctorj-core/src/test/java/org/asciidoctor/converter/WhenConverterIsRegistered.java
@@ -5,6 +5,10 @@ import org.asciidoctor.MemoryLogHandler;
import org.asciidoctor.OptionsBuilder;
import org.asciidoctor.SafeMode;
import org.asciidoctor.arquillian.api.Unshared;
+import org.asciidoctor.ast.Document;
+import org.asciidoctor.ast.Section;
+import org.asciidoctor.ast.StructuralNode;
+import org.asciidoctor.extension.Treeprocessor;
import org.asciidoctor.jruby.internal.JRubyAsciidoctor;
import org.asciidoctor.util.ClasspathResources;
import org.jboss.arquillian.junit.Arquillian;
@@ -116,6 +120,35 @@ public class WhenConverterIsRegistered {
assertEquals(TextConverter.class, asciidoctor.javaConverterRegistry().converters().get("test2"));
}
+ @Test
+ public void shouldReceiveChangedLevels() {
+ asciidoctor.javaConverterRegistry().register(LevelConverter.class, "test3");
+ asciidoctor.javaExtensionRegistry().treeprocessor(new Treeprocessor() {
+ @Override
+ public Document process(Document document) {
+ processNode(document);
+ return document;
+ }
+
+ void processNode(StructuralNode block) {
+ if (block instanceof Section && block.getTitle().equals("Test2")) {
+ block.setLevel(42);
+ }
+ for (StructuralNode node: block.getBlocks()) {
+ processNode(node);
+ }
+ }
+ });
+ String result = asciidoctor.convert("= Test\n" +
+ "\n" +
+ "== Test" +
+ "\n" +
+ "== Test2\n", OptionsBuilder.options().backend("test3").headerFooter(false));
+
+ assertEquals("== 1 Test ==\n" +
+ "== 42 Test2 ==", result);
+ }
+
@Test
public void shouldRegisterConverterViaConverterRegistryExecutor() throws Exception {
ClassLoader oldTCCL = Thread.currentThread().getContextClassLoader();

Fixes #931. Add method StructuralNode.setLevel()