如何在不同的子文件夹中始终找到相同的包含文件

huangapple go评论50阅读模式
英文:

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[]

huangapple
  • 本文由 发表于 2023年1月9日 00:34:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定