簡體   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