简体   繁体   English

我可以在 AsciiDoctor 文档中包含 Java 常量值吗?

[英]Can I include Java constant value in AsciiDoctor document?

For example, lets have a Java constant in some class例如,让我们在某个类中有一个 Java 常量

public class MyClass{
  public static final String ENDPOINT="http://example.com"
}

and lets try to describe that class in AsciiDoctor (corporate docs reasons)并尝试在 AsciiDoctor 中描述该类(公司文档原因)

==== My class

..... some descripton ..... 
It is exposed trough http://example.com

Now every time I change the endpoint, I have to update docs manually as well (IDE find-and-replace will do the trick obviously).现在每次更改端点时,我也必须手动更新文档(IDE 查找和替换显然可以解决问题)。 Is there a way to include Java constant into the AsciiDoc so I don't have to copy its value into the docs?有没有办法将 Java 常量包含到 AsciiDoc 中,这样我就不必将其值复制到文档中?

I would gladly see something like {import my.package.MyClass#ENDPOINT} or something similiar.我很乐意看到类似{import my.package.MyClass#ENDPOINT}或类似的东西。

I'd write a shell script (PERL, whatever) that greps your Java code for a regex something like 'class (\\w+).*ENDPOINT="([^"]+)' , sending the results to an include file ( .\\endpoints.adoc ) using ':\\1_endpoint: \\2' , thus:我会编写一个 shell 脚本(PERL,无论如何),它可以将您的 Java 代码用于正则表达式,例如'class (\\w+).*ENDPOINT="([^"]+)' ,将结果发送到包含文件( .\\endpoints.adoc ) 使用':\\1_endpoint: \\2' ,因此:

:MyClass_endpoint: http://example.com
:MyOtherClass_endpoint: http://other.example.com

Then, at the top of your AsciiDoc file insert: include::[endpoints.adoc]然后,在 AsciiDoc 文件的顶部插入: include::[endpoints.adoc]

and then refer to {MyClass_endpoint} and {MyOtherClass_endpoint} in the body.然后在{MyClass_endpoint}引用{MyClass_endpoint}{MyOtherClass_endpoint}

Then, simply run the script as part of your toolchain prior to calling AsciiDoctor.然后,在调用 AsciiDoctor 之前,简单地将脚本作为工具链的一部分运行。

One other solution is to use the include-pattern of asciidoctor which is described here.另一种解决方案是使用这里描述的 asciidoctor 的包含模式。 https://docs.asciidoctor.org/asciidoc/latest/directives/include-tagged-regions/ https://docs.asciidoctor.org/asciidoc/latest/directives/include-tagged-regions/

In short.简而言之。

Define a tagged region:定义一个标记区域:

public class MyClass{
  //tag::endpointdefinition[]
  public static final String ENDPOINT="http://example.com"
  //end::endpointdefinition[]
}

include it your document将其包含在您的文档中

[source,java]
----
include::pathYourFile.java[tag=endpointdefinition] 
----

To optimize it a little bit you could break your lines different int the java file and place the tag definition on that.为了稍微优化它,您可以在 java 文件中打破不同的行并将标签定义放在上面。 You could also remove the source around including the java file.您还可以删除包含 java 文件的源代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM