Skip to content

Commit

Permalink
Expose Section's sectnum property
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsromero committed Oct 12, 2022
1 parent c137ec5 commit c6ce846
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Improvement::
* Upgrade to JRuby 9.3.8.0 (#1116)
* Upgrade to tilt 2.0.11 (#1109)
* Upgrade to asciimath 2.0.4 (#1109)
* Expose `sectnum` property in Section interface (#1121)

Bug Fixes::

Expand Down
20 changes: 20 additions & 0 deletions asciidoctorj-api/src/main/java/org/asciidoctor/ast/Section.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,24 @@ public interface Section extends StructuralNode {
*/
boolean isNumbered();


/**
* Get the section number for the current Section.
* <p>
* The section number is a dot-separated String that uniquely describes the position of this
* Section in the document. Each entry represents a level of nesting. The value of each entry is
* the 1-based outline number of the Section amongst its numbered sibling Sections.
*/
String getSectnum();

/**
* Get the section number for the current Section.
* <p>
* The section number is a dot-separated String that uniquely describes the position of this
* Section in the document. Each entry represents a level of nesting. The value of each entry is
* the 1-based outline number of the Section amongst its numbered sibling Sections.
*
* @param delimiter the delimiter to separate the number for each level
*/
String getSectnum(String delimiter);
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,14 @@ public boolean isNumbered() {
return getBoolean("numbered");
}

@Override
public String getSectnum() {
return getString("sectnum");
}

@Override
public String getSectnum(String delimiter) {
return getString("sectnum", new Object[]{delimiter});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package org.asciidoctor.jruby.ast.impl;

import org.asciidoctor.Asciidoctor;
import org.asciidoctor.Attributes;
import org.asciidoctor.Options;
import org.asciidoctor.ast.Document;
import org.asciidoctor.ast.Section;
import org.junit.Test;

import java.util.Collections;

import static org.assertj.core.api.Assertions.assertThat;

public class SectionImplTest {

private Asciidoctor asciidoctor = Asciidoctor.Factory.create();


@Test
public void should_return_valid_sectnum_when_sectionNumbers_are_enabled() {
final String source = sectionsSample();

Document document = loadDocument(source, true);

Section node1 = findSectionNode(document, 1);
assertThat(node1.getSectnum()).isEqualTo("1.");
assertThat(node1.isNumbered()).isTrue();

Section node2 = findSectionNode(document, 2);
assertThat(node2.getSectnum()).isEqualTo("1.1.");
assertThat(node2.isNumbered()).isTrue();
}

@Test
public void should_return_invalid_sectnum_when_sectionNumbers_are_not_enabled() {
final String source = sectionsSample();

Document document = loadDocument(source, false);

Section node1 = findSectionNode(document, 1);
assertThat(node1.getSectnum()).isEqualTo(".");
assertThat(node1.isNumbered()).isFalse();

Section node2 = findSectionNode(document, 2);
assertThat(node2.getSectnum()).isEqualTo("..");
assertThat(node2.isNumbered()).isFalse();
}

@Test
public void should_return_sectnum_with_custom_delimiter_when_sectionNumbers_are_enabled() {
final String source = sectionsSample();

Document document = loadDocument(source, true);

Section node1 = findSectionNode(document, 1);
assertThat(node1.getSectnum("_")).isEqualTo("1_");

Section node2 = findSectionNode(document, 2);
assertThat(node2.getSectnum("*")).isEqualTo("1*1*");
}

@Test
public void should_return_sectnum_with_custom_delimiter_when_sectionNumbers_are_not_enabled() {
final String source = sectionsSample();

Document document = loadDocument(source, false);

Section node1 = findSectionNode(document, 1);
assertThat(node1.getSectnum("_")).isEqualTo("_");

Section node2 = findSectionNode(document, 2);
assertThat(node2.getSectnum("*")).isEqualTo("**");
}

private Document loadDocument(String source, boolean sectionNumbers) {
Attributes attributes = Attributes.builder().sectionNumbers(sectionNumbers).build();
Options options = Options.builder().attributes(attributes).build();
Document document = asciidoctor.load(source, options);
return document;
}

private Section findSectionNode(Document document, int level) {
return (Section) document.findBy(Collections.singletonMap("context", ":section"))
.stream()
.filter(n -> n.getLevel() == level)
.findFirst()
.get();
}

static String sectionsSample() {
return "= Document Title\n\n" +
"== Section A\n\n" +
"Section A paragraph.\n\n" +
"=== Section A Subsection\n\n" +
"Section A 'subsection' paragraph.\n\n";
}
}

0 comments on commit c6ce846

Please sign in to comment.