diff options
author | Kamil Doległo <kamilok1965@interia.pl> | 2020-08-11 21:47:26 +0200 |
---|---|---|
committer | Sebastian Sellmair <34319766+sellmair@users.noreply.github.com> | 2020-08-19 13:34:10 +0200 |
commit | 852a6ce6c0f43c9b2044320dcceb4c6cc0a3b302 (patch) | |
tree | fb95be79e816769ca5305cf3a601348549000c6d /plugins/base/src/main/kotlin/resolvers/external | |
parent | 822653f017fa58352148e1c586690debb6773965 (diff) | |
download | dokka-852a6ce6c0f43c9b2044320dcceb4c6cc0a3b302.tar.gz dokka-852a6ce6c0f43c9b2044320dcceb4c6cc0a3b302.tar.bz2 dokka-852a6ce6c0f43c9b2044320dcceb4c6cc0a3b302.zip |
Refactor location providers
Diffstat (limited to 'plugins/base/src/main/kotlin/resolvers/external')
9 files changed, 125 insertions, 94 deletions
diff --git a/plugins/base/src/main/kotlin/resolvers/external/DefaultExternalLocationProvider.kt b/plugins/base/src/main/kotlin/resolvers/external/DefaultExternalLocationProvider.kt new file mode 100644 index 00000000..5e22206b --- /dev/null +++ b/plugins/base/src/main/kotlin/resolvers/external/DefaultExternalLocationProvider.kt @@ -0,0 +1,22 @@ +package org.jetbrains.dokka.base.resolvers.external + +import org.jetbrains.dokka.base.resolvers.local.DokkaLocationProvider.Companion.identifierToFilename +import org.jetbrains.dokka.base.resolvers.shared.ExternalDocumentationInfo +import org.jetbrains.dokka.links.DRI +import org.jetbrains.dokka.plugability.DokkaContext + +open class DefaultExternalLocationProvider( + val externalDocumentationInfo: ExternalDocumentationInfo, + val extension: String, + val dokkaContext: DokkaContext +) : ExternalLocationProvider { + override fun resolve(dri: DRI): String? { // TODO: classes without packages? + val docURL = externalDocumentationInfo.documentationURL.toString().removeSuffix("/") + "/" + val classNamesChecked = dri.classNames ?: return "$docURL${dri.packageName ?: ""}/index$extension" + val classLink = (listOfNotNull(dri.packageName) + classNamesChecked.split('.')) + .joinToString("/", transform = ::identifierToFilename) + + val callableChecked = dri.callable ?: return "$docURL$classLink/index$extension" + return "$docURL$classLink/" + identifierToFilename(callableChecked.name) + extension + } +} diff --git a/plugins/base/src/main/kotlin/resolvers/external/DefaultExternalLocationProviderFactory.kt b/plugins/base/src/main/kotlin/resolvers/external/DefaultExternalLocationProviderFactory.kt new file mode 100644 index 00000000..a6e09bac --- /dev/null +++ b/plugins/base/src/main/kotlin/resolvers/external/DefaultExternalLocationProviderFactory.kt @@ -0,0 +1,20 @@ +package org.jetbrains.dokka.base.resolvers.external + +import org.jetbrains.dokka.base.resolvers.shared.ExternalDocumentationInfo +import org.jetbrains.dokka.base.resolvers.shared.RecognizedLinkFormat +import org.jetbrains.dokka.plugability.DokkaContext + +class DefaultExternalLocationProviderFactory(val context: DokkaContext) : + ExternalLocationProviderFactory by ExternalLocationProviderFactoryWithCache( + object : ExternalLocationProviderFactory { + override fun getExternalLocationProvider(docInfo: ExternalDocumentationInfo): ExternalLocationProvider? = + when (docInfo.packageList.linkFormat) { + RecognizedLinkFormat.KotlinWebsiteHtml, + RecognizedLinkFormat.DokkaOldHtml, + RecognizedLinkFormat.DokkaHtml -> DefaultExternalLocationProvider(docInfo, ".html", context) + RecognizedLinkFormat.DokkaGFM, + RecognizedLinkFormat.DokkaJekyll -> DefaultExternalLocationProvider(docInfo, ".md", context) + else -> null + } + } + )
\ No newline at end of file diff --git a/plugins/base/src/main/kotlin/resolvers/external/DokkaExternalLocationProviderFactory.kt b/plugins/base/src/main/kotlin/resolvers/external/DokkaExternalLocationProviderFactory.kt deleted file mode 100644 index ff9186f7..00000000 --- a/plugins/base/src/main/kotlin/resolvers/external/DokkaExternalLocationProviderFactory.kt +++ /dev/null @@ -1,35 +0,0 @@ -package org.jetbrains.dokka.base.resolvers.external - -import org.jetbrains.dokka.base.resolvers.local.identifierToFilename -import org.jetbrains.dokka.links.DRI - - -class DokkaExternalLocationProviderFactory : ExternalLocationProviderFactory by ExternalLocationProviderFactoryWithCache( - object : ExternalLocationProviderFactory { - override fun getExternalLocationProvider(param: String): ExternalLocationProvider? = - when (param) { - "kotlin-website-html", "html" -> DokkaExternalLocationProvider(param, ".html") - "markdown" -> DokkaExternalLocationProvider(param, ".md") - else -> null - } - } -) - -class DokkaExternalLocationProvider(override val param: String, val extension: String) : ExternalLocationProvider { - - override fun DRI.toLocation(): String { // TODO: classes without packages? - - val classNamesChecked = classNames ?: return "${packageName ?: ""}/index$extension" - - val classLink = (listOfNotNull(packageName) + classNamesChecked.split('.')).joinToString( - "/", - transform = ::identifierToFilename - ) - - val callableChecked = callable ?: return "$classLink/index$extension" - - return "$classLink/${identifierToFilename( - callableChecked.name - )}$extension" - } -} diff --git a/plugins/base/src/main/kotlin/resolvers/external/ExternalLocationProvider.kt b/plugins/base/src/main/kotlin/resolvers/external/ExternalLocationProvider.kt new file mode 100644 index 00000000..51df1504 --- /dev/null +++ b/plugins/base/src/main/kotlin/resolvers/external/ExternalLocationProvider.kt @@ -0,0 +1,7 @@ +package org.jetbrains.dokka.base.resolvers.external + +import org.jetbrains.dokka.links.DRI + +interface ExternalLocationProvider { + fun resolve(dri: DRI): String? +} diff --git a/plugins/base/src/main/kotlin/resolvers/external/ExternalLocationProviderFactory.kt b/plugins/base/src/main/kotlin/resolvers/external/ExternalLocationProviderFactory.kt index 83de9911..10d9fffa 100644 --- a/plugins/base/src/main/kotlin/resolvers/external/ExternalLocationProviderFactory.kt +++ b/plugins/base/src/main/kotlin/resolvers/external/ExternalLocationProviderFactory.kt @@ -1,26 +1,7 @@ package org.jetbrains.dokka.base.resolvers.external -import org.jetbrains.dokka.links.DRI -import java.util.concurrent.ConcurrentHashMap - - -interface ExternalLocationProvider { - - val param: String - fun DRI.toLocation(): String -} +import org.jetbrains.dokka.base.resolvers.shared.ExternalDocumentationInfo interface ExternalLocationProviderFactory { - - fun getExternalLocationProvider(param: String): ExternalLocationProvider? -} - -class ExternalLocationProviderFactoryWithCache(val ext: ExternalLocationProviderFactory) : ExternalLocationProviderFactory { - - private val locationProviders = ConcurrentHashMap<String, CacheWrapper>() - - override fun getExternalLocationProvider(param: String): ExternalLocationProvider? = - locationProviders.getOrPut(param) { CacheWrapper(ext.getExternalLocationProvider(param)) }.provider -} - -private class CacheWrapper(val provider: ExternalLocationProvider?)
\ No newline at end of file + fun getExternalLocationProvider(docInfo: ExternalDocumentationInfo): ExternalLocationProvider? +}
\ No newline at end of file diff --git a/plugins/base/src/main/kotlin/resolvers/external/ExternalLocationProviderFactoryWithCache.kt b/plugins/base/src/main/kotlin/resolvers/external/ExternalLocationProviderFactoryWithCache.kt new file mode 100644 index 00000000..4fa1f391 --- /dev/null +++ b/plugins/base/src/main/kotlin/resolvers/external/ExternalLocationProviderFactoryWithCache.kt @@ -0,0 +1,17 @@ +package org.jetbrains.dokka.base.resolvers.external + +import org.jetbrains.dokka.base.resolvers.shared.ExternalDocumentationInfo +import org.jetbrains.dokka.base.resolvers.shared.PackageList +import java.util.concurrent.ConcurrentHashMap + +class ExternalLocationProviderFactoryWithCache(val ext: ExternalLocationProviderFactory) : + ExternalLocationProviderFactory { + + private val locationProviders = ConcurrentHashMap<ExternalDocumentationInfo, CacheWrapper>() + + override fun getExternalLocationProvider(docInfo: ExternalDocumentationInfo): ExternalLocationProvider? = + locationProviders.getOrPut(docInfo) { CacheWrapper(ext.getExternalLocationProvider(docInfo)) }.provider + + private class CacheWrapper(val provider: ExternalLocationProvider?) +} + diff --git a/plugins/base/src/main/kotlin/resolvers/external/JavadocExternalLocationProviderFactory.kt b/plugins/base/src/main/kotlin/resolvers/external/JavadocExternalLocationProviderFactory.kt deleted file mode 100644 index c52c9bbb..00000000 --- a/plugins/base/src/main/kotlin/resolvers/external/JavadocExternalLocationProviderFactory.kt +++ /dev/null @@ -1,37 +0,0 @@ -package org.jetbrains.dokka.base.resolvers.external - -import org.jetbrains.dokka.links.DRI -import org.jetbrains.dokka.utilities.htmlEscape - -class JavadocExternalLocationProviderFactory : ExternalLocationProviderFactory by ExternalLocationProviderFactoryWithCache( - object : ExternalLocationProviderFactory { - override fun getExternalLocationProvider(param: String): ExternalLocationProvider? = - when(param) { - "javadoc1" -> JavadocExternalLocationProvider(param, "()", ", ") // Covers JDK 1 - 7 - "javadoc8" -> JavadocExternalLocationProvider(param, "--", "-") // Covers JDK 8 - 9 - "javadoc10" -> JavadocExternalLocationProvider(param, "()", ",") // Covers JDK 10 - else -> null - } - } -) - -class JavadocExternalLocationProvider(override val param: String, val brackets: String, val separator: String) : ExternalLocationProvider { - - override fun DRI.toLocation(): String { - - val packageLink = packageName?.replace(".", "/") - if (classNames == null) { - return "$packageLink/package-summary.html".htmlEscape() - } - val classLink = if (packageLink == null) "$classNames.html" else "$packageLink/$classNames.html" - val callableChecked = callable ?: return classLink.htmlEscape() - - val callableLink = "$classLink#" + - callableChecked.name + - "${brackets.first()}" + - callableChecked.params.joinToString(separator) + - "${brackets.last()}" - - return callableLink.htmlEscape() - } -}
\ No newline at end of file diff --git a/plugins/base/src/main/kotlin/resolvers/external/javadoc/JavadocExternalLocationProvider.kt b/plugins/base/src/main/kotlin/resolvers/external/javadoc/JavadocExternalLocationProvider.kt new file mode 100644 index 00000000..d42b5b5c --- /dev/null +++ b/plugins/base/src/main/kotlin/resolvers/external/javadoc/JavadocExternalLocationProvider.kt @@ -0,0 +1,31 @@ +package org.jetbrains.dokka.base.resolvers.external.javadoc + +import org.jetbrains.dokka.base.resolvers.external.DefaultExternalLocationProvider +import org.jetbrains.dokka.base.resolvers.shared.ExternalDocumentationInfo +import org.jetbrains.dokka.links.DRI +import org.jetbrains.dokka.plugability.DokkaContext +import org.jetbrains.dokka.utilities.htmlEscape + +class JavadocExternalLocationProvider( + externalDocumentationInfo: ExternalDocumentationInfo, + val brackets: String, + val separator: String, + dokkaContext: DokkaContext +) : DefaultExternalLocationProvider(externalDocumentationInfo, ".html", dokkaContext) { + + override fun resolve(dri: DRI): String? { + val docURL = externalDocumentationInfo.documentationURL.toString().removeSuffix("/") + "/" + val packageLink = dri.packageName?.replace(".", "/") + if (dri.classNames == null) { + return "$docURL$packageLink/package-summary$extension".htmlEscape() + } + val classLink = if (packageLink == null) "${dri.classNames}$extension" else "$packageLink/${dri.classNames}$extension" + val callableChecked = dri.callable ?: return "$docURL$classLink".htmlEscape() + + return ("$docURL$classLink#" + + callableChecked.name + + "${brackets.first()}" + + callableChecked.params.joinToString(separator) + + "${brackets.last()}").htmlEscape() + } +}
\ No newline at end of file diff --git a/plugins/base/src/main/kotlin/resolvers/external/javadoc/JavadocExternalLocationProviderFactory.kt b/plugins/base/src/main/kotlin/resolvers/external/javadoc/JavadocExternalLocationProviderFactory.kt new file mode 100644 index 00000000..1cfd73a8 --- /dev/null +++ b/plugins/base/src/main/kotlin/resolvers/external/javadoc/JavadocExternalLocationProviderFactory.kt @@ -0,0 +1,25 @@ +package org.jetbrains.dokka.base.resolvers.external.javadoc + +import org.jetbrains.dokka.base.resolvers.external.ExternalLocationProvider +import org.jetbrains.dokka.base.resolvers.external.ExternalLocationProviderFactory +import org.jetbrains.dokka.base.resolvers.external.ExternalLocationProviderFactoryWithCache +import org.jetbrains.dokka.base.resolvers.shared.ExternalDocumentationInfo +import org.jetbrains.dokka.base.resolvers.shared.RecognizedLinkFormat +import org.jetbrains.dokka.plugability.DokkaContext + +class JavadocExternalLocationProviderFactory(val context: DokkaContext) : + ExternalLocationProviderFactory by ExternalLocationProviderFactoryWithCache( + object : ExternalLocationProviderFactory { + override fun getExternalLocationProvider(docInfo: ExternalDocumentationInfo): ExternalLocationProvider? = + when (docInfo.packageList.linkFormat) { + RecognizedLinkFormat.Javadoc1 -> + JavadocExternalLocationProvider(docInfo, "()", ", ", context) // Covers JDK 1 - 7 + RecognizedLinkFormat.Javadoc8, + RecognizedLinkFormat.DokkaJavadoc -> + JavadocExternalLocationProvider(docInfo, "--", "-", context) // Covers JDK 8 - 9 + RecognizedLinkFormat.Javadoc10 -> + JavadocExternalLocationProvider(docInfo, "()", ",", context) // Covers JDK 10 + else -> null + } + } + )
\ No newline at end of file |