asciidoctor / asciidoctorj-diagram

AsciidoctorJ Diagram bundles the Asciidoctor Diagram RubyGem (asciidoctor-diagram) so it can be loaded into the JVM using JRuby.
Apache License 2.0
10 stars 6 forks source link

Load() and convert() has different behaviour as convert() #25

Open gillesB opened 2 years ago

gillesB commented 2 years ago

Hello,

I am not sure if the following is a bug or a feature and if this is the right project to post this issue.

Anyhow I created a simple .adoc with a PlantUml diagram and I want to convert it to HTML.

# Foo

:imagesdir: images

.Title (optional)
[plantuml]
--
abstract class AbstractList
abstract AbstractCollection
interface List
interface Collection

List <|-- AbstractList
Collection <|-- AbstractCollection

Collection <|- List
AbstractCollection <|- AbstractList
AbstractList <|-- ArrayList

class ArrayList {
  Object[] elementData
  size()
}

enum TimeUnit {
  DAYS
  HOURS
  MINUTES
}

annotation SuppressWarnings
--

When converting the .adoc I expect that a .png will be created in the images-folder. This folder should be a sibling of the actual document.

The following code does this as expected but only if doctor.convert(text, options); is called. If I call Document doc = doctor.load(text, options); String converted = doc.convert(); the .png is created but in the wrong images-folder. This images-folder is a child of my current working folder.

public class Test {

    public static void main(String[] args) throws IOException {
        String absPath = "C:/Users/gilles.baatz/git/gilles/PlantumlTest/src/main/resources/foobar/test.adoc";
        Path absPathAdoc = Paths.get(absPath);
        String text = Files.readString(absPathAdoc);

        Path parent = absPathAdoc.getParent();
        Path images = parent.resolve("images");
        Files.createDirectories(images);
        Files.walk(images)
             .sorted(Comparator.reverseOrder())
             .map(Path::toFile)
             .forEach(File::delete);
        Files.createDirectories(images);

        Asciidoctor doctor = Asciidoctor.Factory.create();
        doctor.requireLibrary("asciidoctor-diagram");
        System.out.println("HTML creation");

        Options options = Options.builder()
                                 .backend("html5")
                                 .baseDir(parent.toFile())
                                 .safe(SafeMode.UNSAFE)
                                 .sourcemap(true)
                                 .headerFooter(false)
                                 .build();

                // Does not work as expected: images will be created at "C:/Users/gilles.baatz/git/gilles/PlantumlTest/images"
        System.out.println("load then convert");
        Document doc = doctor.load(text, options);
        String converted = doc.convert();
        Files.list(images).forEach(p -> System.out.println(p));
                // no files printed

        System.out.println("only convert");
        doctor.convert(text, options);

        Files.list(images).forEach(p -> System.out.println(p));
                // one file printed
    }
}
robertpanzer commented 2 years ago

You might want to open this ticket against asciidoctor/asciidoctor-diagram. This repository is just a repackaging of that gem.