尝试获取包括使用 AsciiDoc Java Interface



我不得不承认我是AsciiDoc和ASciiDoctor的新手...

我试图完成的是用groovy渲染一个给定的AsciiDoc模板(https://github.com/masch70/arc42-template-asciidoc)。我的解决方案是使用AsciiDoctor Java接口,它似乎是使用jRuby运行的AsciiDoc的重写。到目前为止,代码运行良好:

@Grab('org.asciidoctor:asciidoctor-java-integration:0.1.4')
import org.asciidoctor.*
def asciidoctor = Asciidoctor.Factory.create()
def output = asciidoctor.renderFile(new File('index.ad'),[
'in_place':true,
'section-numbers':true,
'header_footer':true,
])

但它似乎忽略了对我来说看起来还不错的包含部分:

include::sections/02_architecture_constraints.ad[]

呈现指向文件的链接,而不是包含文件。

AsciiDoctor手册说包括支持:http://asciidoctor.org/docs/user-manual/#include-directive,所以问题是,我做错了什么?

同时添加一个safe选项。默认情况下,API 的safe选项为 SECURE 或 20(整数值),这将禁用要呈现include指令。您可以使用以下任何键值对:

'safe':'SAFE'
'safe': 1
'safe':'SERVER'
'safe': 10
'safe':'UNSAFE' //If you want
'safe': 0

下面应该可以工作。

def output = asciidoctor.renderFile(new File('index.ad'),[
'in_place':true,
'header_footer':true,
'safe':'SAFE'
])

有关更多详细信息,请参阅安全地运行 AsciiDoctor。

更新
tocsection numbers是 CLI 中的属性,因此我们只需要向选项添加所需的属性。

最简单的方法:

def output = asciidoctor.renderFile(new File('index.ad'),[
'in_place':true,
'header_footer':true,
'safe':'SAFE',
'attributes': [toc: true, numbered: true] //I suppose
])

但是上述内容与我们首先想要抽象的底层 ascii 文档实现紧密结合。Asciidoctor提供了有用的构建器模式来克服这种混乱。

import static org.asciidoctor.AttributesBuilder.attributes
import static org.asciidoctor.OptionsBuilder.options
import org.asciidoctor.Placement
Attributes attributes = attributes().tableOfContents(true)
                                    .tableOfContents2(Placement.LEFT)
                                    .sectionNumbers(true)
                                    .get()
Options options = options().inPlace(true)
                           .headerFooter(true)
                           .attributes(attributes)
                           .get()
def output = asciidoctor.renderFile(new File('index.ad'), options)

访问asciidoctor java集成的这个软件包,它将非常清楚如何轻松定制。

我建议你试试asciidoctor gradle插件。

最新更新