blob: 74513957d55fabc45b9892ed865bd988953782e6 (
plain)
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
|
package org.jetbrains.dokka.allModulesPage
import org.jetbrains.dokka.DokkaConfiguration
import org.jetbrains.dokka.base.DokkaBase
import org.jetbrains.dokka.base.resolvers.local.DokkaLocationProvider.Companion.identifierToFilename
import org.jetbrains.dokka.base.resolvers.shared.ExternalDocumentation
import org.jetbrains.dokka.base.resolvers.shared.PackageList
import org.jetbrains.dokka.base.resolvers.shared.RecognizedLinkFormat
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.plugability.DokkaContext
import org.jetbrains.dokka.plugability.plugin
import org.jetbrains.dokka.plugability.query
import java.io.File
import java.net.URL
interface ExternalModuleLinkResolver {
fun resolve(dri: DRI, fileContext: File): String?
fun resolveLinkToModuleIndex(moduleName: String): String?
}
class DefaultExternalModuleLinkResolver(val context: DokkaContext) : ExternalModuleLinkResolver {
private val elpFactory = context.plugin<DokkaBase>().query { externalLocationProviderFactory }
private val externalDocumentations by lazy(::setupExternalDocumentations)
private val elps by lazy {
elpFactory.flatMap { externalDocumentations.map { ed -> it.getExternalLocationProvider(ed) } }.filterNotNull()
}
private fun setupExternalDocumentations(): List<ExternalDocumentation> {
val packageLists =
context.configuration.modules.map(::loadPackageListForModule).toMap()
return packageLists.mapNotNull { (module, packageList) ->
packageList?.let {
context.configuration.modules.find { it.name == module.name }?.let { m ->
ExternalDocumentation(
URL("file:/${m.relativePathToOutputDirectory.toRelativeOutputDir()}"),
packageList
)
}
}
}
}
private fun File.toRelativeOutputDir(): File = if(isAbsolute) {
relativeToOrSelf(context.configuration.outputDir)
} else {
this
}
private fun loadPackageListForModule(module: DokkaConfiguration.DokkaModuleDescription) =
module.sourceOutputDirectory.resolve(File(identifierToFilename(module.name))).let {
it to PackageList.load(
URL("file:" + it.resolve("package-list").path),
8,
true
)
}
override fun resolve(dri: DRI, fileContext: File): String? {
val absoluteLink = elps.mapNotNull { it.resolve(dri) }.firstOrNull() ?: return null
val modulePath = context.configuration.outputDir.absolutePath.split(File.separator)
val contextPath = fileContext.absolutePath.split(File.separator)
val commonPathElements = modulePath.zip(contextPath)
.takeWhile { (a, b) -> a == b }.count()
return (List(contextPath.size - commonPathElements - 1) { ".." } + modulePath.drop(commonPathElements)).joinToString(
"/"
) + absoluteLink.removePrefix("file:")
}
override fun resolveLinkToModuleIndex(moduleName: String): String? =
context.configuration.modules.firstOrNull { it.name == moduleName }
?.let { module ->
val (_, packageList) = loadPackageListForModule(module)
val extension = when (packageList?.linkFormat) {
RecognizedLinkFormat.KotlinWebsiteHtml,
RecognizedLinkFormat.DokkaOldHtml,
RecognizedLinkFormat.DokkaHtml -> ".html"
RecognizedLinkFormat.DokkaGFM,
RecognizedLinkFormat.DokkaJekyll -> ".md"
else -> ""
}
"${module.relativePathToOutputDirectory}/index$extension"
}
}
|