From c07b2fd8a59733f412ca42483c9200dab91db08e Mon Sep 17 00:00:00 2001 From: Robert Panzer Date: Sun, 3 Mar 2024 15:52:43 +0100 Subject: [PATCH] Add methods Reader.getSource and Reader.getSourceLines (#1264) * Add methods Reader.getSource and Reader.getSourceLines * Fix codenarc issue * Clarify comments --- CHANGELOG.adoc | 1 + .../org/asciidoctor/extension/Reader.java | 12 ++++++++++ .../jruby/extension/internal/ReaderImpl.java | 10 ++++++++ .../WhenAPreprocessorIsRegistered.groovy | 23 +++++++++++++++++++ 4 files changed, 46 insertions(+) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index e2c5b856..835069e1 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -27,6 +27,7 @@ Improvement:: * Remove deprecated methods from ast package (#1204) (@abelsromero) * Add Automatic-Module-Name manifest entry to core, api, and cli for reserving stable JPMS module names (#1240) (@leadpony) * Remove Java 'requires open access' module warning in modern Java versions (#1246) +* Add Reader.getSource() and Reader.getSourceLines() (#1262) Bug Fixes:: diff --git a/asciidoctorj-api/src/main/java/org/asciidoctor/extension/Reader.java b/asciidoctorj-api/src/main/java/org/asciidoctor/extension/Reader.java index 6a206aa3..606bc160 100644 --- a/asciidoctorj-api/src/main/java/org/asciidoctor/extension/Reader.java +++ b/asciidoctorj-api/src/main/java/org/asciidoctor/extension/Reader.java @@ -72,6 +72,18 @@ public interface Reader { */ List getLines(); + /** + * Returns the source lines for this Reader joined as a String + * @return The source lines for this Reader joined as a String + */ + String getSource(); + + /** + * Get the document source as a List of Strings. + * @return the document source as a List of Strings. + */ + List getSourceLines(); + /** * Push the String line onto the beginning of the Array of source data. *

diff --git a/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/extension/internal/ReaderImpl.java b/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/extension/internal/ReaderImpl.java index 8ca6c847..307808a0 100644 --- a/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/extension/internal/ReaderImpl.java +++ b/asciidoctorj-core/src/main/java/org/asciidoctor/jruby/extension/internal/ReaderImpl.java @@ -70,6 +70,16 @@ public List getLines() { return getList("lines", String.class); } + @Override + public String getSource() { + return getString("source"); + } + + @Override + public List getSourceLines() { + return getList("source_lines", String.class); + } + @Override public void restoreLine(String line) { getRubyProperty("unshift_line", line); diff --git a/asciidoctorj-core/src/test/groovy/org/asciidoctor/extension/WhenAPreprocessorIsRegistered.groovy b/asciidoctorj-core/src/test/groovy/org/asciidoctor/extension/WhenAPreprocessorIsRegistered.groovy index f97d72d8..b69c1d43 100644 --- a/asciidoctorj-core/src/test/groovy/org/asciidoctor/extension/WhenAPreprocessorIsRegistered.groovy +++ b/asciidoctorj-core/src/test/groovy/org/asciidoctor/extension/WhenAPreprocessorIsRegistered.groovy @@ -175,4 +175,27 @@ $secondLine""" preprocessorCalled.get() } + def 'should be able to get source and source lines'() { + given: + + String source = '' + List sourceLines = [] + + asciidoctor.javaExtensionRegistry().preprocessor(new Preprocessor() { + @Override + Reader process(Document doc, PreprocessorReader reader) { + source = reader.source + sourceLines = reader.sourceLines + reader + } + }) + + when: + asciidoctor.convert(document, emptyOptions()) + + then: + source == document + sourceLines == [firstLine, secondLine] + } + }