Skip to content

Commit

Permalink
Merge pull request #934 from robertpanzer/fix-931-setLevel
Browse files Browse the repository at this point in the history
Fixes #931. Add method StructuralNode.setLevel()
  • Loading branch information
robertpanzer authored Jul 3, 2020
2 parents 93b47c0 + 2d723d8 commit 82a7ecb
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public int getLevel() {
return getInt("level");
}

@Override
public void setLevel(int level) {
setInt("level", level);
}

@Override
public Cursor getSourceLocation() {
IRubyObject object = getRubyProperty("source_location");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,15 @@ public int getInt(String propertyName, Object... args) {
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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
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;
Expand Down Expand Up @@ -116,6 +120,35 @@ public void shouldReturnRegisteredConverter() {
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();
Expand Down

0 comments on commit 82a7ecb

Please sign in to comment.