英文:
How can I find always the same include file from within different sub folders
问题
我想要在不同的adoc文件中包含一个adoc文件。但是包含文件的路径取决于目录结构的深度。我的想法是定义一个环境变量来定义结构的根目录。例如,类似这样的内容:
include::${MY_ROOT_DIR}/.inc/include.adoc[]
写一个固定的绝对路径不是一个选择,因为我在Linux、Windows和其他不同的电脑上使用这个目录。
但是据我所查,没有环境变量可用。而且$MY_ROOT_DIR等等也不起作用。
英文:
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
答案1
得分: 2
Asciidoc标记不会为您执行路径操作。当您设置一个属性时,它是一个字符串值。当您在include
中包含一个属性时,它会按原样使用。
include
预处理指令的文件解析在文档中有详细描述。
简而言之:当您在一个Asciidoc文件中使用include
时,相对路径会相对于包含它的文件进行解析,无论它是顶级文件还是一个已包含的文件。
您唯一的解决方案是使用绝对路径。
如果您想使用属性MY_ROOT_DIR
,您可以在命令行上设置它:
asciidoctor -a MY_ROOT_DIR=`pwd` file.adoc
反引号表示执行pwd
命令,并将其输出作为字符串值返回。因此,MY_ROOT_DIR
被设置为当前目录。
对于Windows,您将更改调用方式为:
asciidoctor -a MY_ROOT_DIR=%cd% file.adoc
英文:
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
答案2
得分: 0
如果您使用https://github.com/verhas/jamal预处理器与Asciidoc一起使用,您可以使用它来执行包含操作。该预处理器中的include
宏具有[top]
选项,如果从主文件中包含了文件,则会包含来自已包含文件内部的文件。
此外,还有一个宏可以获取环境变量的值。然而,使用@eskwayrd所示的Asciidoctor解决方案,使用-a
属性设置,可以实现相同的功能,更加简单。
免责声明:我创建了Jamal。
英文:
I am not sure this is what you are looking for, but here it is.
If you use the https://github.com/verhas/jamal preprocessor with Asciidoc, you can use it to perform the includes. The include
macro in this preprocessor has a [top]
option, which will include a file from within an already included file if it was included from the main file.
Also, there is a macro to get the value of an environment variable. This, however, is functionally equivalent and simpler with Asciidoctor solution depicted by @eskwayrd, using the -a
attribute setting.
disclaimer: I created Jamal.
答案3
得分: 0
我找到了一个更好的方法:如果你想要在项目中的任何地方包含例如一个模板文件,你需要在你的根目录下创建一个名为“.asciidoctorconfig”的文件,在其中加入以下内容:
:root: {asciidoctorconfigdir}
然后你可以在你的asciidoc文件中使用它:
include::{root}/myPathFromRoot/myFile.adoc[]
英文:
I find a better way: If you want to include eg. a template file from anywhere in your project you need a file in your root called ".asciidoctorconfig"
where you put the following line:
:root: {asciidoctorconfigdir}
Then you can use it in your asciidoc files:
include::{root}/myPathFromRoot/myFile.adoc[]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论