Kotlin 如何在Dokka生成的文档中嵌入可运行的示例代码
我正在尝试将示例代码添加到API引用中 我遵循了这里强调的模式 能够创建我的示例项目和所有代码运行,复制kotlin stdlib源代码使用的sampleUtils 我将模块配置为在dokka配置中包含示例Kotlin 如何在Dokka生成的文档中嵌入可运行的示例代码,kotlin,kotlin-dokka,Kotlin,Kotlin Dokka,我正在尝试将示例代码添加到API引用中 我遵循了这里强调的模式 能够创建我的示例项目和所有代码运行,复制kotlin stdlib源代码使用的sampleUtils 我将模块配置为在dokka配置中包含示例 tasks.dokkaHtml{ dokkaSourceSets.configureEach{ samples.from(“$projectDir/。/module samples/test/samples/SampleFile.kt”) } } 使用“运行”按钮将样本正确加载到函数的文档
tasks.dokkaHtml{
dokkaSourceSets.configureEach{
samples.from(“$projectDir/。/module samples/test/samples/SampleFile.kt”)
}
}
使用“运行”按钮将样本正确加载到函数的文档中并显示在文档中
当我点击run Through时,它返回的是库中所有符号的未解析引用。它也无法从_samplesUtils.kt文件中找到assertPrint方法
在Dokka配置中,我没有看到任何其他设置可以让示例包含所记录库的jar文件。研究后,我发现示例使用的是Kotlin Playerd。要添加额外的依赖项,您需要创建自己的编译器服务器
然后使用额外的依赖项修改build.gradle.kts文件
下一部分是更新Dokka生成的html以指向您的新服务器。我找不到一种内置的方法来实现这一点,所以我实现了这个
dokkaHtml {
doLast {
def docTree = fileTree(
dir: "${dokkaHtml.outputDirectory.get()}",
includes: ["**/*.html"])
docTree.each { file ->
def text = file.text
file.write(text.replace(
'<script src="https://unpkg.com/kotlin-playground@1" data-selector="code.runnablesample"></script>',
'<script src="https://unpkg.com/kotlin-playground@1" data-selector="code.runnablesample" data-server="http://localhost:8080"></script>'
))
}
}
}
dokkaHtml{
多拉斯特{
def docTree=fileTree(
目录:“${dokkaHtml.outputDirectory.get()}”,
包括:[“***.html”])
docTree.each{file->
def text=file.text
file.write(text.replace(
'',
''
))
}
}
}
您还必须创建一个定制的小jar,用对样本更有效的代码类型和函数替换测试代码类型和函数