-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Current implementation works correctly when using load+getContent. But not when using convert which fails. Same applies when trying to integrate with a custom converter, note I see the same behauviour for Asciidoctor Ruby: that is, load+content does not populate the catalog, only calling convert
1 parent
8853c8a
commit 78efe30
Showing
10 changed files
with
413 additions
and
34 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
8 changes: 8 additions & 0 deletions
8
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.asciidoctor.ast; | ||
|
||
public interface ImageReference { | ||
|
||
String getTarget(); | ||
|
||
String getImagesdir(); | ||
} |
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
39 changes: 39 additions & 0 deletions
39
asciidoctorj-core/src/main/java/org/asciidoctor/jruby/ast/impl/ImageReferenceImpl.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,39 @@ | ||
package org.asciidoctor.jruby.ast.impl; | ||
|
||
import org.asciidoctor.ast.ImageReference; | ||
import org.jruby.RubyStruct; | ||
import org.jruby.javasupport.JavaEmbedUtils; | ||
|
||
public class ImageReferenceImpl implements ImageReference { | ||
|
||
private static final String IMAGESDIR_KEY_NAME = "imagesdir"; | ||
private static final String TARGET_KEY_NAME = "target"; | ||
|
||
private final String target; | ||
private final String imagesdir; | ||
|
||
private ImageReferenceImpl(String target, String imagesdir) { | ||
this.target = target; | ||
this.imagesdir = imagesdir; | ||
} | ||
|
||
static ImageReference getInstance(RubyStruct rubyFootnote) { | ||
final String target = (String) aref(rubyFootnote, TARGET_KEY_NAME); | ||
final String imagesdir = (String) aref(rubyFootnote, IMAGESDIR_KEY_NAME); | ||
return new ImageReferenceImpl(target, imagesdir); | ||
} | ||
|
||
private static Object aref(RubyStruct s, String key) { | ||
return JavaEmbedUtils.rubyToJava(s.aref(s.getRuntime().newString(key))); | ||
} | ||
|
||
@Override | ||
public String getTarget() { | ||
return target; | ||
} | ||
|
||
@Override | ||
public String getImagesdir() { | ||
return imagesdir; | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
asciidoctorj-core/src/test/java/org/asciidoctor/jruby/ast/impl/TestImageReference.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,29 @@ | ||
package org.asciidoctor.jruby.ast.impl; | ||
|
||
import org.asciidoctor.ast.ImageReference; | ||
|
||
public class TestImageReference implements ImageReference { | ||
|
||
private final String target; | ||
private final String imagesdir; | ||
|
||
public TestImageReference(String target) { | ||
this.target = target; | ||
this.imagesdir = null; | ||
} | ||
|
||
public TestImageReference(String target, String imagesdir) { | ||
this.target = target; | ||
this.imagesdir = imagesdir; | ||
} | ||
|
||
@Override | ||
public String getTarget() { | ||
return target; | ||
} | ||
|
||
@Override | ||
public String getImagesdir() { | ||
return imagesdir; | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
...-core/src/test/java/org/asciidoctor/jruby/internal/WhenReadingImagesFromCatalogAsset.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,139 @@ | ||
package org.asciidoctor.jruby.internal; | ||
|
||
import org.asciidoctor.Asciidoctor; | ||
import org.asciidoctor.Attributes; | ||
import org.asciidoctor.Options; | ||
import org.asciidoctor.arquillian.api.Unshared; | ||
import org.asciidoctor.ast.Document; | ||
import org.asciidoctor.ast.ImageReference; | ||
import org.asciidoctor.jruby.ast.impl.TestImageReference; | ||
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.runner.RunWith; | ||
|
||
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 WhenReadingImagesFromCatalogAsset { | ||
|
||
@ArquillianResource | ||
private ClasspathResources classpath; | ||
|
||
@ArquillianResource(Unshared.class) | ||
private Asciidoctor asciidoctor; | ||
|
||
static final TestImageReference[] BLOCK_IMAGES = new TestImageReference[]{ | ||
new TestImageReference("images/block-image.jpg") | ||
}; | ||
|
||
static final TestImageReference[] ALL_IMAGES = new TestImageReference[]{ | ||
new TestImageReference("images/block-image.jpg"), | ||
new TestImageReference("images/inline-image.png") | ||
}; | ||
|
||
|
||
@Test | ||
public void shouldReturnEmptyWhenThereAreNoImages() { | ||
final Options options = Options.builder() | ||
.catalogAssets(true) | ||
.build(); | ||
|
||
Document document = asciidoctor.load("= Hello", options); | ||
List<ImageReference> images = document.getCatalog().getImages(); | ||
|
||
assertThat(images) | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
public void shouldReturnNullImagesDirWhenNotSet() { | ||
final Options options = Options.builder() | ||
.catalogAssets(true) | ||
.build(); | ||
final String content = getAsciiDodWithImagesDocument(); | ||
|
||
Document document = asciidoctor.load(content, options); | ||
List<ImageReference> images = document.getCatalog().getImages(); | ||
|
||
assertThat(images) | ||
.usingRecursiveFieldByFieldElementComparator() | ||
.containsExactly(BLOCK_IMAGES); | ||
} | ||
|
||
@Test | ||
public void shouldReturnImagesDirWhenSet() { | ||
final Options options = Options.builder() | ||
.catalogAssets(true) | ||
.attributes(Attributes.builder() | ||
.imagesDir("some-path") | ||
.build()) | ||
.build(); | ||
final String content = getAsciiDodWithImagesDocument(); | ||
|
||
Document document = asciidoctor.load(content, options); | ||
List<ImageReference> images = document.getCatalog().getImages(); | ||
|
||
assertThat(images) | ||
.usingRecursiveFieldByFieldElementComparator() | ||
.containsExactly(new TestImageReference("images/block-image.jpg", "some-path")); | ||
} | ||
|
||
@Test | ||
public void shouldNotCatalogInlineImagesWhenNotConverting() { | ||
final Options options = Options.builder() | ||
.catalogAssets(true) | ||
.build(); | ||
final String content = getAsciiDodWithImagesDocument(); | ||
|
||
Document document = asciidoctor.load(content, options); | ||
|
||
List<ImageReference> images = document.getCatalog().getImages(); | ||
assertThat(images) | ||
.usingRecursiveFieldByFieldElementComparator() | ||
.containsExactly(BLOCK_IMAGES); | ||
} | ||
|
||
@Test | ||
public void shouldCatalogInlineImagesWhenProcessingContent() { | ||
final Options options = Options.builder() | ||
.catalogAssets(true) | ||
.build(); | ||
final String content = getAsciiDodWithImagesDocument(); | ||
|
||
Document document = asciidoctor.load(content, options); | ||
document.getContent(); | ||
|
||
List<ImageReference> images = document.getCatalog().getImages(); | ||
assertThat(images) | ||
.usingRecursiveFieldByFieldElementComparator() | ||
.containsExactlyInAnyOrder(ALL_IMAGES); | ||
} | ||
|
||
@Test | ||
public void shouldNotCatalogInlineImagesWhenCatalogAssetsIsFalse() { | ||
final Options options = Options.builder() | ||
.catalogAssets(false) | ||
.build(); | ||
final String content = getAsciiDodWithImagesDocument(); | ||
|
||
Document document = asciidoctor.load(content, options); | ||
document.getContent(); | ||
|
||
List<ImageReference> images = document.getCatalog().getImages(); | ||
assertThat(images).isEmpty(); | ||
} | ||
|
||
private String getAsciiDodWithImagesDocument() { | ||
try { | ||
return Files.readString(classpath.getResource("sample-with-images.adoc").toPath()); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
...t/java/org/asciidoctor/jruby/internal/WhenReadingImagesFromCatalogAssetFromConverter.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,132 @@ | ||
package org.asciidoctor.jruby.internal; | ||
|
||
import org.asciidoctor.Asciidoctor; | ||
import org.asciidoctor.Options; | ||
import org.asciidoctor.arquillian.api.Unshared; | ||
import org.asciidoctor.ast.*; | ||
import org.asciidoctor.converter.StringConverter; | ||
import org.asciidoctor.jruby.ast.impl.TestImageReference; | ||
import org.asciidoctor.util.ClasspathResources; | ||
import org.jboss.arquillian.junit.Arquillian; | ||
import org.jboss.arquillian.test.api.ArquillianResource; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.as; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@RunWith(Arquillian.class) | ||
public class WhenReadingImagesFromCatalogAssetFromConverter { | ||
|
||
@ArquillianResource | ||
private ClasspathResources classpath; | ||
|
||
@ArquillianResource(Unshared.class) | ||
private Asciidoctor asciidoctor; | ||
|
||
static final String CONVERTER_BACKEND = "custom-backend"; | ||
|
||
static final TestImageReference[] ALL_IMAGES = new TestImageReference[]{ | ||
new TestImageReference("images/block-image.jpg"), | ||
new TestImageReference("images/inline-image.png") | ||
}; | ||
|
||
private static List<ImageReference> imagesBeforeConvert; | ||
private static List<ImageReference> imagesAfterConvert; | ||
|
||
@Before | ||
public void beforeEach() { | ||
final var javaConverterRegistry = asciidoctor.javaConverterRegistry(); | ||
javaConverterRegistry.converters().clear(); | ||
javaConverterRegistry.register(TestConverter.class, CONVERTER_BACKEND); | ||
|
||
imagesBeforeConvert = List.of(); | ||
imagesAfterConvert = List.of(); | ||
} | ||
|
||
@Test | ||
public void shouldReturnEmptyWhenThereAreNoImages() { | ||
final String content = ""; | ||
|
||
convert(content); | ||
|
||
assertThat(imagesBeforeConvert).isEmpty(); | ||
assertThat(imagesAfterConvert).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void shouldReturnEmptyWhenThereUsingLoad() { | ||
final String content = getAsciiDodWithImagesDocument(); | ||
|
||
load(content); | ||
|
||
assertThat(imagesBeforeConvert).isEmpty(); | ||
assertThat(imagesAfterConvert).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void shouldReturnAllImages() { | ||
final String content = getAsciiDodWithImagesDocument(); | ||
|
||
var options = Options.builder().backend(CONVERTER_BACKEND).build(); | ||
Document load = asciidoctor.load(content, options); | ||
String content1 = (String) load.getContent(); | ||
|
||
Catalog catalog = load.getCatalog(); | ||
assertThat(imagesBeforeConvert).isEmpty(); | ||
assertThat(imagesAfterConvert) | ||
.usingRecursiveFieldByFieldElementComparator() | ||
.containsExactly(ALL_IMAGES); | ||
} | ||
|
||
private String convert(String document) { | ||
var options = Options.builder().backend(CONVERTER_BACKEND).build(); | ||
return asciidoctor.convert(document, options); | ||
} | ||
|
||
private void load(String document) { | ||
var options = Options.builder().backend(CONVERTER_BACKEND).build(); | ||
asciidoctor.load(document, options); | ||
} | ||
|
||
private String getAsciiDodWithImagesDocument() { | ||
try { | ||
return Files.readString(classpath.getResource("sample-with-images.adoc").toPath()); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public static class TestConverter extends StringConverter { | ||
|
||
public TestConverter(String backend, Map<String, Object> opts) { | ||
super(backend, opts); | ||
} | ||
|
||
/* | ||
* For this conversion test we do not care about the conversion result, | ||
* we simply want to force the conversion to verify that footnotes | ||
* are populated. | ||
*/ | ||
|
||
@Override | ||
public String convert(ContentNode node, String transform, Map<Object, Object> opts) { | ||
String content = ""; | ||
if (node instanceof Document) { | ||
var doc = (Document) node; | ||
imagesBeforeConvert = doc.getCatalog().getImages(); | ||
content = (String) doc.getContent(); | ||
imagesAfterConvert = doc.getCatalog().getImages(); | ||
} else if (node instanceof StructuralNode) { | ||
content = (String) ((StructuralNode) node).getContent(); | ||
} | ||
return content; | ||
} | ||
} | ||
} |
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 | ||
|
||
== A few images | ||
|
||
A block image | ||
|
||
image::images/block-image.jpg[] | ||
|
||
An inlined image image:images/inline-image.png[] in the text. |