gpt4 book ai didi

java - 如何在 groovy 中编写可写接口(interface)代码

转载 作者:行者123 更新时间:2023-12-01 13:36:54 25 4
gpt4 key购买 nike

希望使用groovy为Asciidoctor标记编写一个groovy.text.Template。到目前为止:

//-----------------------------------

public class AsciidocTemplate implements groovy.text.Template
{
boolean includeHeaderFooter = true;

Asciidoctor asciidoctor = create();
org.asciidoctor.Options asciidoctorJOptions=["backend": "html","header_footer":includeHeaderFooter]

def payload="";
Map binding = [:]

public AsciidocTemplate()
{
} // end of constructor

public AsciidocTemplate(def payload)
{
this.payload = payload;
} // end of constructor

// use doctor to transform the template
public Writable process()
{
def output = asciidoctor.render(this.payload, asciidoctorJOptions);
Writer ou = output;
return ou;
} // end of make

Writable make()
{
return process(); //output as Writable;
} // end of make

// -----------------------------------

render() 返回一个字符串,我们如何将该字符串转换为实现 Writable 接口(interface)的对象,我只是不明白。也看不到如何使用/读取/存储返回的“可写”对象:-P

最佳答案

您可以使用the asWritable method on a Closure获得一个可写的返回,即:

Writable process()
{
def output = asciidoctor.render(this.payload, asciidoctorJOptions);
{ w -> w.println output }.asWritable()
}

关于java - 如何在 groovy 中编写可写接口(interface)代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21205446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com