Skip to content

Commit

Permalink
Expose Document's source and source_lines properties
Browse files Browse the repository at this point in the history
Fixes #1143
  • Loading branch information
abelsromero committed Mar 19, 2023
1 parent b190250 commit 4d64810
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 9 deletions.
6 changes: 2 additions & 4 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Improvement::
* Upgrade to tilt 2.0.11 (#1109)
* Upgrade to asciimath 2.0.4 (#1109)
* Expose `sectnum` property in Section interface (#1121)
* Replace use of deprecated 'numbered' attribute by 'sectnums' (#1123) (@abelsromero)
* Expose `source` and `source_lines` use of deprecated 'numbered' in Document interface (#1145) (@abelsromero)

Bug Fixes::

Expand All @@ -39,10 +41,6 @@ Build Improvement::
* Fix upstream tests forcing SNAPSHOT on Asciidoctor gem installation (#1123) (@abelsromero)
* Fix upstream build removing the explicit plugin repository (#1131)

Build / Infrastructure::

* Replace use of deprecated 'numbered' attribute by 'sectnums' (#1123) (@abelsromero)

== 2.5.4 (2022-06-30)

Improvement::
Expand Down
15 changes: 15 additions & 0 deletions asciidoctorj-api/src/main/java/org/asciidoctor/ast/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ public interface Document extends StructuralNode {
*/
List<Author> getAuthors();

/**
* Make the raw source for the Document available.
* Trailing white characters (spaces, line breaks, etc.) are removed.
*
* @return raw content as String
*/
String getSource();

/**
* Make the raw source lines for the Document available.
*
* @return raw content as List<String>
*/
List<String> getSourceLines();

/**
* @return basebackend attribute value
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ public DocumentImpl(IRubyObject document) {
super(document);
}

@Override
public String getSource() {
return getString("source");
}

@Override
public List<String> getSourceLines() {
return getList("source_lines", String.class);
}

@Override
public boolean isBasebackend(String backend) {
return getBoolean("basebackend?", backend);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public int getInt(String propertyName, Object... args) {
if (result instanceof RubyNil) {
return 0;
} else {
return (int) ((RubyNumeric) result).getIntValue();
return ((RubyNumeric) result).getIntValue();
}
}

Expand All @@ -100,7 +100,7 @@ public <T> List<T> getList(String propertyName, Class<T> elementClass, Object...
if (result instanceof RubyNil) {
return null;
} else {
List<T> ret = new ArrayList<T>();
List<T> ret = new ArrayList<>();
RubyArray array = (RubyArray) result;
for (int i = 0; i < array.size(); i++) {
ret.add(RubyUtils.rubyToJava(runtime, array.at(RubyFixnum.newFixnum(runtime, i)), elementClass));
Expand Down Expand Up @@ -160,7 +160,7 @@ public Object toJava(IRubyObject rubyObject) {
}

public <T> T toJava(IRubyObject rubyObject, Class<T> targetClass) {
return (T) JavaEmbedUtils.rubyToJava(runtime, rubyObject, targetClass);
return JavaEmbedUtils.rubyToJava(runtime, rubyObject, targetClass);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ public void should_return_sectnum_with_custom_delimiter_when_sectionNumbers_are_
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;
return asciidoctor.load(source, options);
}

private Section findSectionNode(Document document, int level) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
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.junit.Test;

import java.util.List;

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

public class WhenDocumentIsLoaded {

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

@Test
public void should_return_empty_when_document_is_null() {
assertEmptySources(loadDocument(null));
}

@Test
public void should_return_empty_when_document_is_empty() {
assertEmptySources(loadDocument(""));
}

private static void assertEmptySources(Document document) {
String source = document.getSource();
assertThat(source).isEmpty();
List<String> sourceLines = document.getSourceLines();
assertThat(sourceLines).isEmpty();
}

@Test
public void should_return_source_and_source_lines() {
final String asciidoc = asciidocSample();

Document document = loadDocument(asciidoc);

String source = document.getSource();
assertThat(source).isEqualTo(asciidoc.trim());
List<String> sourceLines = document.getSourceLines();
assertThat(sourceLines)
.containsExactly("= Document Title",
"",
"== Section A",
"",
"Section A paragraph.",
"",
"=== Section A Subsection",
"",
"Section A 'subsection' paragraph.");
}

@Test
public void should_return_source_and_source_lines_without_trailing() {
final String asciidoc = "= Document Title\n\n" +
"== Section\n\n" +
"Hello\t \n";

Document document = loadDocument(asciidoc);

String source = document.getSource();
assertThat(source).isEqualTo("= Document Title\n\n== Section\n\nHello");
List<String> sourceLines = document.getSourceLines();
assertThat(sourceLines)
.containsExactly("= Document Title",
"",
"== Section",
"",
"Hello");
}

@Test
public void should_return_source_and_source_lines_without_resolving_attributes() {
final String asciidoc = "= Document Title\n" +
":an-attribute: a-value\n\n" +
"This is {an-attribute}";

Document document = loadDocument(asciidoc);

String source = document.getSource();
assertThat(source).isEqualTo("= Document Title\n:an-attribute: a-value\n\nThis is {an-attribute}");
List<String> sourceLines = document.getSourceLines();
assertThat(sourceLines)
.containsExactly("= Document Title",
":an-attribute: a-value",
"",
"This is {an-attribute}");
}

@Test
public void should_return_source_and_source_lines_without_resolving_includes() {
final String asciidoc = "= Document Title\n\n" +
"== Section\n\n" +
"include::partial.adoc[]";

Document document = loadDocument(asciidoc);

String source = document.getSource();
assertThat(source).isEqualTo("= Document Title\n\n== Section\n\ninclude::partial.adoc[]");
List<String> sourceLines = document.getSourceLines();
assertThat(sourceLines)
.containsExactly("= Document Title",
"",
"== Section",
"",
"include::partial.adoc[]");
}

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

static String asciidocSample() {
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 4d64810

Please sign in to comment.