Search code examples
asciidoc

How can I find always the same include file from within different sub folders


I want to include a adoc file from within different other adoc files. But the path to the include file depends from the deep of the directory structure. My Idea would be defining an environment variable to define the root of the structure. eg. something like this

include::${MY_ROOT_DIR}/.inc/include.adoc[]

Writing a fix absolute path is no option, I use the directory from linux windows and other different pcs.

But there are no env variables as far as I could google. And $MY_ROOT_DIR etc. does not work


Solution

  • Asciidoc markup doesn't do path manipulation for you. When you set an attribute, it is a string value. When you include an attribute in an include, it is used as-is.

    The include preprocessor directive's file resolution is described properly in the documentation.

    The TL;DR: when you use include in an Asciidoc file, a relative path is resolved relative to the including file, whether it is the top-level file or an included file.

    Your only solution is to use an absolute path.

    If you want to use the attribute MY_ROOT_DIR, you can set it on the command line:

    asciidoctor -a MY_ROOT_DIR=`pwd` file.adoc
    

    The backticks mean that the pwd command is executed and its output is returned as a string value. So, MY_ROOT_DIR is set to the current directory.

    For Windows, you would change the invocation to this:

    asciidoctor -a MY_ROOT_DIR=%cd% file.adoc