1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
package org.jetbrains.dokka.templates
import kotlinx.coroutines.*
import org.jetbrains.dokka.DokkaConfiguration
import org.jetbrains.dokka.base.DokkaBase
import org.jetbrains.dokka.base.templating.Command
import org.jetbrains.dokka.model.withDescendants
import org.jetbrains.dokka.pages.RootPageNode
import org.jetbrains.dokka.plugability.DokkaContext
import org.jetbrains.dokka.plugability.plugin
import org.jetbrains.dokka.plugability.query
import org.jetbrains.dokka.plugability.querySingle
import org.jsoup.nodes.Element
import java.io.File
interface TemplateProcessor
interface SubmoduleTemplateProcessor : TemplateProcessor {
fun process(modules: List<DokkaConfiguration.DokkaModuleDescription>): TemplatingResult
}
interface MultiModuleTemplateProcessor : TemplateProcessor {
fun process(generatedPagesTree: RootPageNode)
}
interface TemplateProcessingStrategy {
fun process(input: File, output: File): Boolean
fun finish(output: File) {}
}
class DefaultSubmoduleTemplateProcessor(
private val context: DokkaContext,
) : SubmoduleTemplateProcessor {
private val strategies: List<TemplateProcessingStrategy> =
context.plugin<TemplatingPlugin>().query { templateProcessingStrategy }
private val configuredModulesPaths =
context.configuration.modules.map { it.sourceOutputDirectory.absolutePath to it.name }.toMap()
override fun process(modules: List<DokkaConfiguration.DokkaModuleDescription>) =
runBlocking(Dispatchers.Default) {
coroutineScope {
modules.fold(TemplatingResult()) { acc, module ->
acc + module.sourceOutputDirectory.visit(context.configuration.outputDir.resolve(module.relativePathToOutputDirectory))
}
}
}
private suspend fun File.visit(target: File, acc: TemplatingResult = TemplatingResult()): TemplatingResult =
coroutineScope {
val source = this@visit
if (source.isDirectory) {
target.mkdir()
val files = source.list().orEmpty()
val accWithSelf = configuredModulesPaths[source.absolutePath]
?.takeIf { files.firstOrNull { !it.startsWith(".") } != null }
?.let { acc.copy(modules = acc.modules + it) }
?: acc
files.fold(accWithSelf) { acc, path ->
source.resolve(path).visit(target.resolve(path), acc)
}
} else {
strategies.first { it.process(source, target) }
acc
}
}
}
class DefaultMultiModuleTemplateProcessor(
context: DokkaContext,
) : MultiModuleTemplateProcessor {
private val strategies: List<TemplateProcessingStrategy> =
context.plugin<TemplatingPlugin>().query { templateProcessingStrategy }
private val locationProviderFactory = context.plugin<DokkaBase>().querySingle { locationProviderFactory }
override fun process(generatedPagesTree: RootPageNode) {
val locationProvider = locationProviderFactory.getLocationProvider(generatedPagesTree)
generatedPagesTree.withDescendants().mapNotNull { locationProvider.resolve(it)?.let { File(it) } }
.forEach { location -> strategies.first { it.process(location, location) } }
}
}
data class TemplatingContext<out T : Command>(
val input: File,
val output: File,
val element: Element,
val command: T,
)
data class TemplatingResult(val modules: List<String> = emptyList()) {
operator fun plus(rhs: TemplatingResult): TemplatingResult = TemplatingResult((modules + rhs.modules).distinct())
}
|