Re: AsciidoctorJ's "convertFile(s)" methods don't return html

Posted by abelsromero on
URL: https://discuss.asciidoctor.org/AsciidoctorJ-s-convertFile-s-methods-don-t-return-html-tp2771p2772.html

Hi Sean,

First of all, I have to admit that the documentation (and API interface) in the README can be confusing and should be clearer, please feel free to open and issue or propose any recomendation on the repo :).
The thing is that the String is only returned when the output is not rendered to a File, and by default the output is written to a file in the same folder. If you want to render the documents "on-the-fly" you have two options:

1. Enable the option not to render to a file with the "setFile(false)" option:
        Asciidoctor instance = Asciidoctor.Factory.create();

        File input = new File("src/asciidoc/sample.adoc");
        OptionsBuilder options = OptionsBuilder.options().toFile(false); *
        String output = instance.convertFile(input, options.asMap());

        Assert.assertNotNull(output);
2. Use the "convert" method with a reader and a writter:
        Asciidoctor instance = Asciidoctor.Factory.create();

        FileReader fr = new FileReader(new File("src/asciidoc/sample-2.adoc"));
        StringWriter sw = new StringWriter();      
        // This convert method returns void
        instance.convert(fr, sw, OptionsBuilder.options().asMap()); *

        Assert.assertNotNull(output);

(*) Note the use of OptionBuilder to create the options map, I recomend it since it makes everything easier and reduces the chances of typos in the parameters.

For the sake of completeness, there's a third option; transforming the input file to a String and converting it, but this is not a good option since you may encounter encoding and memory issues.