繁体   English   中英

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

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

例如,让我们在某个类中有一个 Java 常量

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

并尝试在 AsciiDoctor 中描述该类(公司文档原因)

==== My class

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

现在每次更改端点时,我也必须手动更新文档(IDE 查找和替换显然可以解决问题)。 有没有办法将 Java 常量包含到 AsciiDoc 中,这样我就不必将其值复制到文档中?

我很乐意看到类似{import my.package.MyClass#ENDPOINT}或类似的东西。

我会编写一个 shell 脚本(PERL,无论如何),它可以将您的 Java 代码用于正则表达式,例如'class (\\w+).*ENDPOINT="([^"]+)' ,将结果发送到包含文件( .\\endpoints.adoc ) 使用':\\1_endpoint: \\2' ,因此:

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

然后,在 AsciiDoc 文件的顶部插入: include::[endpoints.adoc]

然后在{MyClass_endpoint}引用{MyClass_endpoint}{MyOtherClass_endpoint}

然后,在调用 AsciiDoctor 之前,简单地将脚本作为工具链的一部分运行。

另一种解决方案是使用这里描述的 asciidoctor 的包含模式。 https://docs.asciidoctor.org/asciidoc/latest/directives/include-tagged-regions/

简而言之。

定义一个标记区域:

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

将其包含在您的文档中

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

为了稍微优化它,您可以在 java 文件中打破不同的行并将标签定义放在上面。 您还可以删除包含 java 文件的源代码。

暂无
暂无

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

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