Skip to content

Commit

Permalink
Add methods Reader.getSource and Reader.getSourceLines (#1264)
Browse files Browse the repository at this point in the history
* Add methods Reader.getSource and Reader.getSourceLines

* Fix codenarc issue

* Clarify comments
  • Loading branch information
robertpanzer authored Mar 3, 2024
1 parent a03c403 commit c07b2fd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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::

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ public interface Reader {
*/
List<String> 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<String> getSourceLines();

/**
* Push the String line onto the beginning of the Array of source data.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ public List<String> getLines() {
return getList("lines", String.class);
}

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

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

@Override
public void restoreLine(String line) {
getRubyProperty("unshift_line", line);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,27 @@ $secondLine"""
preprocessorCalled.get()
}

def 'should be able to get source and source lines'() {
given:

String source = ''
List<String> 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]
}

}

0 comments on commit c07b2fd

Please sign in to comment.