Login  Register

Re: asciidoctor-maven-plugin and images.

Posted by Jeremie Bresson on Jan 25, 2015; 10:30am
URL: https://discuss.asciidoctor.org/asciidoctor-maven-plugin-and-images-tp2695p2705.html

Thank you for your feedback.

Because you told that you are interested to my setup, I can share how I plan to organize our repository:

    repository
    +---build
    |   |
    |   +---howto_install
    |   |   |   pom.xml
    |   |   |
    |   |   \---src
    |   |           howto_install.adoc
    |   |
    |   \---tutorial_helloworld
    |       |   pom.xml
    |       |
    |       \---src
    |               tutorial_helloworld.adoc
    |
    +---code
    |   +---aNewProject
    |   |       (Some *.java files)
    |   |
    |   \---helloworldJava
    |           HelloWorld.java
    |
    +---images
    |       helloworld_swing.png
    |       helloworld_swt.png
    |
    \---modules
            module1.adoc
            module2.adoc

* modules: short asciidoctor files (modules) that can be reused in several documents. They often correspond to one section of text (without any title).
* images: images for the modules and the documents.
* code: java code that can be compiled. Used in the modules.
* build: contains the documents that are build. Because maven is standard in our project and company (ci-server already provides it) it would be the preferred way for us.

----

From what you told me, for the HTML backend I think that I will go with the option of setting imagesdir in the maven pom to a not existing directory (where the images should be at the end). For example:
<imagesdir>./img</imagesdir>

This way the produced HTML will contain:
<img src="./img/helloworld_swing.png" alt="Hello World">

Then I need to copy the image form the real destination (the common images folder) to the one defined in the html document. I can do this with something similar to this method (I already have from some other experiments):
    public static void copyImages(Document doc, File inFolder, File outFolder) throws IOException {
      Elements elements = doc.getElementsByTag("img");
      for (Element element : elements) {
        String src = element.attr("src");
        if (src != null) {
          File inFile = new File(inFolder, src);
          File outFile = new File(outFolder, src);
          if (inFile.exists() && inFile.isFile()) {
            Files.createParentDirs(outFile);
            Files.copy(inFile, outFile);
          }
          else if (outFile.exists() && outFile.isFile()) {
            //the in file was not found and not copied, but the outFile already exists. Nothing to do, no warning.
          }
          else {
            System.err.println("Image file '" + inFile.getAbsolutePath() + "' is missing");
          }
        }
      }
    }

Where the doc variable is initialized like this: Document doc = Jsoup.parse(html);

I think I will be fine with this pattern. If you have some inputs, do not hesitate to share them with me (I need to do the have possible setup. Changes later on are painful).