Skip to content

Commit c33af8f

Browse files
committed
Expose Document's source and source_lines properties
Fixes #1143
1 parent b190250 commit c33af8f

File tree

7 files changed

+150
-10
lines changed

7 files changed

+150
-10
lines changed

CHANGELOG.adoc

+2-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Improvement::
2424
* Upgrade to tilt 2.0.11 (#1109)
2525
* Upgrade to asciimath 2.0.4 (#1109)
2626
* Expose `sectnum` property in Section interface (#1121)
27+
* Replace use of deprecated 'numbered' attribute by 'sectnums' (#1123) (@abelsromero)
28+
* Expose `source` and `source_lines` use of deprecated 'numbered' in Document interface (#1145) (@abelsromero)
2729

2830
Bug Fixes::
2931

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

42-
Build / Infrastructure::
43-
44-
* Replace use of deprecated 'numbered' attribute by 'sectnums' (#1123) (@abelsromero)
45-
4644
== 2.5.4 (2022-06-30)
4745

4846
Improvement::

asciidoctorj-api/src/main/java/org/asciidoctor/ast/Document.java

+15
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ public interface Document extends StructuralNode {
3333
*/
3434
List<Author> getAuthors();
3535

36+
/**
37+
* Make the raw source for the Document available.
38+
* Trailing white characters (spaces, line breaks, etc.) are removed.
39+
*
40+
* @return raw content as String
41+
*/
42+
String getSource();
43+
44+
/**
45+
* Make the raw source lines for the Document available.
46+
*
47+
* @return raw content as List<String>
48+
*/
49+
List<String> getSourceLines();
50+
3651
/**
3752
* @return basebackend attribute value
3853
*/

asciidoctorj-core/src/main/java/org/asciidoctor/jruby/ast/impl/DocumentImpl.java

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ public DocumentImpl(IRubyObject document) {
1616
super(document);
1717
}
1818

19+
@Override
20+
public String getSource() {
21+
return getString("source");
22+
}
23+
24+
@Override
25+
public List<String> getSourceLines() {
26+
return getList("source_lines", String.class);
27+
}
28+
1929
@Override
2030
public boolean isBasebackend(String backend) {
2131
return getBoolean("basebackend?", backend);

asciidoctorj-core/src/main/java/org/asciidoctor/jruby/internal/JRubyAsciidoctor.java

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.asciidoctor.extension.RubyExtensionRegistry;
1212
import org.asciidoctor.jruby.AsciidoctorJRuby;
1313
import org.asciidoctor.jruby.DirectoryWalker;
14-
import org.asciidoctor.jruby.ast.impl.AuthorImpl;
1514
import org.asciidoctor.jruby.ast.impl.DocumentHeaderImpl;
1615
import org.asciidoctor.jruby.ast.impl.NodeConverter;
1716
import org.asciidoctor.jruby.converter.internal.ConverterRegistryExecutor;

asciidoctorj-core/src/main/java/org/asciidoctor/jruby/internal/RubyObjectWrapper.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public int getInt(String propertyName, Object... args) {
8686
if (result instanceof RubyNil) {
8787
return 0;
8888
} else {
89-
return (int) ((RubyNumeric) result).getIntValue();
89+
return ((RubyNumeric) result).getIntValue();
9090
}
9191
}
9292

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

162162
public <T> T toJava(IRubyObject rubyObject, Class<T> targetClass) {
163-
return (T) JavaEmbedUtils.rubyToJava(runtime, rubyObject, targetClass);
163+
return JavaEmbedUtils.rubyToJava(runtime, rubyObject, targetClass);
164164
}
165165

166166
}

asciidoctorj-core/src/test/java/org/asciidoctor/jruby/ast/impl/SectionImplTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ public void should_return_sectnum_with_custom_delimiter_when_sectionNumbers_are_
7575
private Document loadDocument(String source, boolean sectionNumbers) {
7676
Attributes attributes = Attributes.builder().sectionNumbers(sectionNumbers).build();
7777
Options options = Options.builder().attributes(attributes).build();
78-
Document document = asciidoctor.load(source, options);
79-
return document;
78+
return asciidoctor.load(source, options);
8079
}
8180

8281
private Section findSectionNode(Document document, int level) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package org.asciidoctor.jruby.ast.impl;
2+
3+
import org.asciidoctor.Asciidoctor;
4+
import org.asciidoctor.Attributes;
5+
import org.asciidoctor.Options;
6+
import org.asciidoctor.ast.Document;
7+
import org.junit.Test;
8+
9+
import java.util.List;
10+
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
13+
public class WhenDocumentIsLoaded {
14+
15+
private Asciidoctor asciidoctor = Asciidoctor.Factory.create();
16+
17+
@Test
18+
public void should_return_empty_when_document_is_empty() {
19+
assertEmptySources(loadDocument(""));
20+
}
21+
22+
private static void assertEmptySources(Document document) {
23+
String source = document.getSource();
24+
assertThat(source).isEmpty();
25+
List<String> sourceLines = document.getSourceLines();
26+
assertThat(sourceLines).isEmpty();
27+
}
28+
29+
@Test
30+
public void should_return_source_and_source_lines() {
31+
final String asciidoc = asciidocSample();
32+
33+
Document document = loadDocument(asciidoc);
34+
35+
String source = document.getSource();
36+
assertThat(source).isEqualTo(asciidoc.trim());
37+
List<String> sourceLines = document.getSourceLines();
38+
assertThat(sourceLines)
39+
.containsExactly("= Document Title",
40+
"",
41+
"== Section A",
42+
"",
43+
"Section A paragraph.",
44+
"",
45+
"=== Section A Subsection",
46+
"",
47+
"Section A 'subsection' paragraph.");
48+
}
49+
50+
@Test
51+
public void should_return_source_and_source_lines_without_trailing() {
52+
final String asciidoc = "= Document Title\n\n" +
53+
"== Section\n\n" +
54+
"Hello\t \n";
55+
56+
Document document = loadDocument(asciidoc);
57+
58+
String source = document.getSource();
59+
assertThat(source).isEqualTo("= Document Title\n\n== Section\n\nHello");
60+
List<String> sourceLines = document.getSourceLines();
61+
assertThat(sourceLines)
62+
.containsExactly("= Document Title",
63+
"",
64+
"== Section",
65+
"",
66+
"Hello");
67+
}
68+
69+
@Test
70+
public void should_return_source_and_source_lines_without_resolving_attributes() {
71+
final String asciidoc = "= Document Title\n" +
72+
":an-attribute: a-value\n\n" +
73+
"This is {an-attribute}";
74+
75+
Document document = loadDocument(asciidoc);
76+
77+
String source = document.getSource();
78+
assertThat(source).isEqualTo("= Document Title\n:an-attribute: a-value\n\nThis is {an-attribute}");
79+
List<String> sourceLines = document.getSourceLines();
80+
assertThat(sourceLines)
81+
.containsExactly("= Document Title",
82+
":an-attribute: a-value",
83+
"",
84+
"This is {an-attribute}");
85+
}
86+
87+
@Test
88+
public void should_return_source_and_source_lines_without_resolving_includes() {
89+
final String asciidoc = "= Document Title\n\n" +
90+
"== Section\n\n" +
91+
"include::partial.adoc[]";
92+
93+
Document document = loadDocument(asciidoc);
94+
95+
String source = document.getSource();
96+
assertThat(source).isEqualTo("= Document Title\n\n== Section\n\ninclude::partial.adoc[]");
97+
List<String> sourceLines = document.getSourceLines();
98+
assertThat(sourceLines)
99+
.containsExactly("= Document Title",
100+
"",
101+
"== Section",
102+
"",
103+
"include::partial.adoc[]");
104+
}
105+
106+
private Document loadDocument(String source) {
107+
Attributes attributes = Attributes.builder().build();
108+
Options options = Options.builder().attributes(attributes).build();
109+
return asciidoctor.load(source, options);
110+
}
111+
112+
static String asciidocSample() {
113+
return "= Document Title\n\n" +
114+
"== Section A\n\n" +
115+
"Section A paragraph.\n\n" +
116+
"=== Section A Subsection\n\n" +
117+
"Section A 'subsection' paragraph.\n\n";
118+
}
119+
}

0 commit comments

Comments
 (0)