forked from asciidoctor/asciidoctorj
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new Link interface and accessors methods to Catalog interface. Original links in asciidoctor are just a string but here we wrap them in an object to protect from future evolutions. Fixes asciidoctor#1183
- Loading branch information
1 parent
8603b64
commit 03968ff
Showing
9 changed files
with
210 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
asciidoctorj-api/src/main/java/org/asciidoctor/ast/ImageReference.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,24 @@ | ||
package org.asciidoctor.ast; | ||
|
||
/** | ||
* Image reference view as available in the assets catalog. | ||
* | ||
* @since 3.0.0 | ||
*/ | ||
public interface ImageReference { | ||
|
||
/** | ||
* The image target describing the image location. | ||
* The target may be a path or a URL. | ||
* | ||
* @return image target | ||
*/ | ||
String getTarget(); | ||
|
||
/** | ||
* Value of the 'imagesdir' attribute applied to resolve the image location. | ||
* | ||
* @return imagesdir value | ||
*/ | ||
String getImagesdir(); | ||
} |
17 changes: 17 additions & 0 deletions
17
asciidoctorj-api/src/main/java/org/asciidoctor/ast/Link.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.asciidoctor.ast; | ||
|
||
/** | ||
* Link view as available in the assets catalog. | ||
* | ||
* @since 3.0.0 | ||
*/ | ||
public interface Link { | ||
|
||
/** | ||
* The resolved path of the link. | ||
* The text may be a file path or a URL. | ||
* | ||
* @return The link path including substitutions being applied. | ||
*/ | ||
String getText(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
asciidoctorj-core/src/main/java/org/asciidoctor/jruby/ast/impl/LinkImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.asciidoctor.jruby.ast.impl; | ||
|
||
import org.asciidoctor.ast.Link; | ||
|
||
public class LinkImpl implements Link { | ||
|
||
private final String text; | ||
|
||
private LinkImpl(String text) { | ||
this.text = text; | ||
} | ||
|
||
static Link getInstance(String value) { | ||
return new LinkImpl(value); | ||
} | ||
|
||
@Override | ||
public String getText() { | ||
return text; | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
...j-core/src/test/java/org/asciidoctor/jruby/internal/WhenReadingLinksFromCatalogAsset.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package org.asciidoctor.jruby.internal; | ||
|
||
import org.asciidoctor.Asciidoctor; | ||
import org.asciidoctor.Options; | ||
import org.asciidoctor.SafeMode; | ||
import org.asciidoctor.arquillian.api.Unshared; | ||
import org.asciidoctor.ast.Document; | ||
import org.asciidoctor.ast.Link; | ||
import org.asciidoctor.util.ClasspathResources; | ||
import org.jboss.arquillian.junit.Arquillian; | ||
import org.jboss.arquillian.test.api.ArquillianResource; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@RunWith(Arquillian.class) | ||
public class WhenReadingLinksFromCatalogAsset { | ||
|
||
@ArquillianResource | ||
private ClasspathResources classpath; | ||
|
||
@ArquillianResource(Unshared.class) | ||
private Asciidoctor asciidoctor; | ||
|
||
@ArquillianResource | ||
private TemporaryFolder testFolder; | ||
|
||
static final String[] ALL_LINKS = new String[]{ | ||
"https://docs.asciidoctor.org", | ||
"https://github.com", | ||
"https://some.rangomsite.org", | ||
"downloads/report.pdf" | ||
}; | ||
|
||
@Test | ||
public void shouldReturnEmptyWhenThereAreNoLinks() { | ||
final Options options = catalogAssetsEnabled(); | ||
|
||
Document document = asciidoctor.load("= Hello", options); | ||
List<Link> links = document.getCatalog().getLinks(); | ||
|
||
assertThat(links) | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
public void shouldNotReturnLinksWhenNotConverting() { | ||
final Options options = catalogAssetsEnabled(); | ||
final String content = getAsciiDocWithLinksContent(); | ||
|
||
Document document = asciidoctor.load(content, options); | ||
List<Link> links = document.getCatalog().getLinks(); | ||
|
||
assertThat(links) | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
public void shouldNotReturnLinksWhenCatalogAssetsIsFalse() { | ||
final Options options = Options.builder() | ||
.catalogAssets(false) | ||
.build(); | ||
final File file = getAsciiDocWithLinksFile(); | ||
|
||
Document document = asciidoctor.convertFile(file, options, Document.class); | ||
|
||
List<Link> links = document.getCatalog().getLinks(); | ||
assertThat(links) | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
public void shouldReturnLinksWhenConvertingFile() { | ||
final Options options = catalogAssetsEnabled(); | ||
final File file = getAsciiDocWithLinksFile(); | ||
|
||
Document document = asciidoctor.convertFile(file, options, Document.class); | ||
|
||
List<Link> links = document.getCatalog().getLinks(); | ||
assertThat(links) | ||
.map(link -> link.getText()) | ||
.containsExactlyInAnyOrder(ALL_LINKS); | ||
} | ||
|
||
@Test | ||
public void shouldReturnLinksWhenConvertingString() throws IOException { | ||
final Options options = Options.builder() | ||
.catalogAssets(true) | ||
.safe(SafeMode.UNSAFE) | ||
.toFile(testFolder.newFile()) | ||
.build(); | ||
final String content = getAsciiDocWithLinksContent(); | ||
|
||
Document document = asciidoctor.convert(content, options, Document.class); | ||
|
||
List<Link> links = document.getCatalog().getLinks(); | ||
assertThat(links) | ||
.map(link -> link.getText()) | ||
.containsExactlyInAnyOrder(ALL_LINKS); | ||
} | ||
|
||
private String getAsciiDocWithLinksContent() { | ||
try { | ||
return Files.readString(getAsciiDocWithLinksFile().toPath()); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private File getAsciiDocWithLinksFile() { | ||
return classpath.getResource("sample-with-links.adoc"); | ||
} | ||
|
||
private static Options catalogAssetsEnabled() { | ||
return Options.builder() | ||
.catalogAssets(true) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
= This is a title | ||
:site-host: some.rangomsite.org | ||
|
||
You can learn about Asciidoctor at https://docs.asciidoctor.org. | ||
The Asciidoctor source repo is hosted on https://github.com[GitHub]. | ||
|
||
== A few links | ||
|
||
You can also use attributes for links like in https://{site-host}, or use full syntax for files link:downloads/report.pdf[Get Report]. |