aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/main/kotlin/resolvers/external/javadoc/JavadocExternalLocationProvider.kt
blob: a1f1542dbbdac7b8f0cb99e6c1fa23520f573c47 (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
package org.jetbrains.dokka.base.resolvers.external.javadoc

import org.jetbrains.dokka.base.resolvers.external.DefaultExternalLocationProvider
import org.jetbrains.dokka.base.resolvers.shared.ExternalDocumentation
import org.jetbrains.dokka.links.*
import org.jetbrains.dokka.plugability.DokkaContext
import org.jetbrains.dokka.utilities.htmlEscape

open class JavadocExternalLocationProvider(
        externalDocumentation: ExternalDocumentation,
        val brackets: String,
        val separator: String,
        dokkaContext: DokkaContext
) : DefaultExternalLocationProvider(externalDocumentation, ".html", dokkaContext) {

    override fun DRI.constructPath(): String {
        val packageLink = packageName?.replace(".", "/")
        val modulePart = packageName?.let { packageName ->
            externalDocumentation.packageList.moduleFor(packageName)?.let {
                if (it.isNotBlank())
                    "$it/"
                else
                    ""
            }
        }.orEmpty()

        val docWithModule = docURL + modulePart

        if (classNames == null) {
            return "$docWithModule$packageLink/package-summary$extension".htmlEscape()
        }

        if (DRIExtraContainer(extra)[EnumEntryDRIExtra] != null) {
            val lastIndex = classNames?.lastIndexOf(".") ?: 0
            val (classSplit, enumEntityAnchor) =
                classNames?.substring(0, lastIndex) to classNames?.substring(lastIndex + 1)

            val classLink =
                if (packageLink == null) "${classSplit}$extension" else "$packageLink/${classSplit}$extension"
            return "$docWithModule$classLink#$enumEntityAnchor".htmlEscape()
        }

        val classLink = if (packageLink == null) "${classNames}$extension" else "$packageLink/${classNames}$extension"
        val callableChecked = callable ?: return "$docWithModule$classLink".htmlEscape()

        return ("$docWithModule$classLink#" + anchorPart(callableChecked)).htmlEscape()
    }

    protected open fun anchorPart(callable: Callable) = callable.name +
            "${brackets.first()}" +
            callable.params.joinToString(separator) +
            "${brackets.last()}"

}