From c33af8fed09bf40b15de5869882718c0ae20c601 Mon Sep 17 00:00:00 2001 From: Abel Salgado Romero Date: Sun, 19 Mar 2023 11:25:55 +0100 Subject: [PATCH] Expose Document's source and source_lines properties Fixes #1143 --- CHANGELOG.adoc | 6 +- .../java/org/asciidoctor/ast/Document.java | 15 +++ .../jruby/ast/impl/DocumentImpl.java | 10 ++ .../jruby/internal/JRubyAsciidoctor.java | 1 - .../jruby/internal/RubyObjectWrapper.java | 6 +- .../jruby/ast/impl/SectionImplTest.java | 3 +- .../jruby/ast/impl/WhenDocumentIsLoaded.java | 119 ++++++++++++++++++ 7 files changed, 150 insertions(+), 10 deletions(-) create mode 100644 asciidoctorj-core/src/test/java/org/asciidoctor/jruby/ast/impl/WhenDocumentIsLoaded.java diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 0b52fca4b..f13ab2466 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -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:: @@ -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:: diff --git a/asciidoctorj-api/src/main/java/org/asciidoctor/ast/Document.java b/asciidoctorj-api/src/main/java/org/asciidoctor/ast/Document.java index d4e86ac8d..86cbbff9c 100644 --- a/asciidoctorj-api/src/main/java/org/asciidoctor/ast/Document.java +++ b/asciidoctorj-api/src/main/java/org/asciidoctor/ast/Document.java @@ -33,6 +33,21 @@ public interface Document extends StructuralNode { */ List 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 + */ + List getSourceLines(); + /** * @return basebackend attribute value */ diff --git a/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/ast/impl/DocumentImpl.java b/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/ast/impl/DocumentImpl.java index 9fea488f2..d4f7497c6 100644 --- a/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/ast/impl/DocumentImpl.java +++ b/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/ast/impl/DocumentImpl.java @@ -16,6 +16,16 @@ public DocumentImpl(IRubyObject document) { super(document); } + @Override + public String getSource() { + return getString("source"); + } + + @Override + public List getSourceLines() { + return getList("source_lines", String.class); + } + @Override public boolean isBasebackend(String backend) { return getBoolean("basebackend?", backend); diff --git a/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/internal/JRubyAsciidoctor.java b/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/internal/JRubyAsciidoctor.java index ff85e1e2b..d7bf75ad2 100644 --- a/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/internal/JRubyAsciidoctor.java +++ b/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/internal/JRubyAsciidoctor.java @@ -11,7 +11,6 @@ import org.asciidoctor.extension.RubyExtensionRegistry; import org.asciidoctor.jruby.AsciidoctorJRuby; import org.asciidoctor.jruby.DirectoryWalker; -import org.asciidoctor.jruby.ast.impl.AuthorImpl; import org.asciidoctor.jruby.ast.impl.DocumentHeaderImpl; import org.asciidoctor.jruby.ast.impl.NodeConverter; import org.asciidoctor.jruby.converter.internal.ConverterRegistryExecutor; diff --git a/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/internal/RubyObjectWrapper.java b/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/internal/RubyObjectWrapper.java index a6f351795..1e9d60e91 100644 --- a/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/internal/RubyObjectWrapper.java +++ b/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/internal/RubyObjectWrapper.java @@ -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(); } } @@ -100,7 +100,7 @@ public List getList(String propertyName, Class elementClass, Object... if (result instanceof RubyNil) { return null; } else { - List ret = new ArrayList(); + List 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)); @@ -160,7 +160,7 @@ public Object toJava(IRubyObject rubyObject) { } public T toJava(IRubyObject rubyObject, Class targetClass) { - return (T) JavaEmbedUtils.rubyToJava(runtime, rubyObject, targetClass); + return JavaEmbedUtils.rubyToJava(runtime, rubyObject, targetClass); } } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/ast/impl/SectionImplTest.java b/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/ast/impl/SectionImplTest.java index a5b0daf51..d0dfe47bd 100644 --- a/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/ast/impl/SectionImplTest.java +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/ast/impl/SectionImplTest.java @@ -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) { diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/ast/impl/WhenDocumentIsLoaded.java b/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/ast/impl/WhenDocumentIsLoaded.java new file mode 100644 index 000000000..8c1643d8e --- /dev/null +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/jruby/ast/impl/WhenDocumentIsLoaded.java @@ -0,0 +1,119 @@ +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_empty() { + assertEmptySources(loadDocument("")); + } + + private static void assertEmptySources(Document document) { + String source = document.getSource(); + assertThat(source).isEmpty(); + List 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 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 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 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 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"; + } +}