From fc38ceda06d92978175d37bb7197763539c966a2 Mon Sep 17 00:00:00 2001 From: Kamil Doległo Date: Thu, 23 Jul 2020 12:38:59 +0200 Subject: Make unresolved links render as text --- plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt | 14 ++++++++------ .../base/src/main/kotlin/renderers/PackageListService.kt | 3 ++- .../base/src/main/kotlin/renderers/html/HtmlRenderer.kt | 4 ++-- .../main/kotlin/resolvers/local/BaseLocationProvider.kt | 4 ++-- .../main/kotlin/resolvers/local/DefaultLocationProvider.kt | 4 ++-- .../src/main/kotlin/resolvers/local/LocationProvider.kt | 4 ++-- .../kotlin/resolvers/local/MultimoduleLocationProvider.kt | 4 ++-- plugins/gfm/src/main/kotlin/GfmPlugin.kt | 4 ++-- .../dokka/javadoc/location/JavadocLocationProvider.kt | 14 ++++++++------ .../dokka/javadoc/renderer/KorteJavadocRenderer.kt | 2 +- .../dokka/javadoc/renderer/SearchScriptsCreator.kt | 8 ++++---- 11 files changed, 35 insertions(+), 30 deletions(-) diff --git a/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt b/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt index ef04bb47..fe2913a4 100644 --- a/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt +++ b/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt @@ -66,7 +66,7 @@ abstract class DefaultRenderer( open fun T.wrapGroup(node: ContentGroup, pageContext: ContentPage, childrenCallback: T.() -> Unit) = childrenCallback() - open fun T.buildLinkText( + open fun T.buildText( nodes: List, pageContext: ContentPage, sourceSetRestriction: Set? = null @@ -109,11 +109,13 @@ abstract class DefaultRenderer( is ContentCodeBlock -> buildCodeBlock(node, pageContext) is ContentCodeInline -> buildCodeInline(node, pageContext) is ContentDRILink -> - buildLink(locationProvider.resolve(node.address, node.sourceSets, pageContext)) { - buildLinkText(node.children, pageContext, sourceSetRestriction) - } + locationProvider.resolve(node.address, node.sourceSets, pageContext)?.let { address -> + buildLink(address) { + buildText(node.children, pageContext, sourceSetRestriction) + } + } ?: buildText(node.children, pageContext, sourceSetRestriction) is ContentResolvedLink -> buildLink(node.address) { - buildLinkText(node.children, pageContext, sourceSetRestriction) + buildText(node.children, pageContext, sourceSetRestriction) } is ContentEmbeddedResource -> buildResource(node, pageContext) is ContentList -> buildList(node, pageContext, sourceSetRestriction) @@ -140,7 +142,7 @@ abstract class DefaultRenderer( } open suspend fun renderPage(page: PageNode) { - val path by lazy { locationProvider.resolve(page, skipExtension = true) } + val path by lazy { locationProvider.resolve(page, skipExtension = true)!! } when (page) { is ContentPage -> outputWriter.write(path, buildPage(page) { c, p -> buildPageContent(c, p) }, ".html") is RendererSpecificPage -> when (val strategy = page.strategy) { diff --git a/plugins/base/src/main/kotlin/renderers/PackageListService.kt b/plugins/base/src/main/kotlin/renderers/PackageListService.kt index d4333200..0cf84ddd 100644 --- a/plugins/base/src/main/kotlin/renderers/PackageListService.kt +++ b/plugins/base/src/main/kotlin/renderers/PackageListService.kt @@ -28,7 +28,8 @@ class PackageListService(val context: DokkaContext) { val contentPage = node.safeAs() contentPage?.dri?.forEach { if (parentDris.isNotEmpty() && it.parent !in parentDris) { - nonStandardLocations[it.toString()] = locationProvider.resolve(node) + locationProvider.resolve(node) + ?.let { nodeLocation -> nonStandardLocations[it.toString()] = nodeLocation } } } diff --git a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt index 8c2e9c9e..1a2a98b3 100644 --- a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt +++ b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt @@ -522,7 +522,7 @@ open class HtmlRenderer( } private fun FlowContent.buildLink(to: PageNode, from: PageNode) = - buildLink(locationProvider.resolve(to, from)) { + buildLink(locationProvider.resolve(to, from)!!) { text(to.name) } @@ -550,7 +550,7 @@ open class HtmlRenderer( platforms: List, from: PageNode? = null, block: FlowContent.() -> Unit - ) = buildLink(locationProvider.resolve(to, platforms.toSet(), from), block) + ) = buildLink(locationProvider.resolve(to, platforms.toSet(), from).orEmpty(), block) override fun buildError(node: ContentNode) { context.logger.error("Unknown ContentNode type: $node") diff --git a/plugins/base/src/main/kotlin/resolvers/local/BaseLocationProvider.kt b/plugins/base/src/main/kotlin/resolvers/local/BaseLocationProvider.kt index 7387789b..06730641 100644 --- a/plugins/base/src/main/kotlin/resolvers/local/BaseLocationProvider.kt +++ b/plugins/base/src/main/kotlin/resolvers/local/BaseLocationProvider.kt @@ -25,7 +25,7 @@ abstract class BaseLocationProvider(protected val dokkaContext: DokkaContext) : protected fun getExternalLocation( dri: DRI, sourceSets: Set - ): String { + ): String? { val jdkToExternalDocumentationLinks = dokkaContext.configuration.sourceSets .filter { sourceSet -> sourceSet.sourceSetID in sourceSets.sourceSetIDs } .groupBy({ it.jdkVersion }, { it.externalDocumentationLinks }) @@ -55,7 +55,7 @@ abstract class BaseLocationProvider(protected val dokkaContext: DokkaContext) : } toResolve.remove(jdk) } - return "" + return null } private fun getLink(dri: DRI, locationInfo: DefaultLocationProvider.LocationInfo): String = diff --git a/plugins/base/src/main/kotlin/resolvers/local/DefaultLocationProvider.kt b/plugins/base/src/main/kotlin/resolvers/local/DefaultLocationProvider.kt index b9df45ae..1c27959f 100644 --- a/plugins/base/src/main/kotlin/resolvers/local/DefaultLocationProvider.kt +++ b/plugins/base/src/main/kotlin/resolvers/local/DefaultLocationProvider.kt @@ -48,10 +48,10 @@ open class DefaultLocationProvider( pageGraphRoot.children.forEach { registerPath(it, emptyList()) } } - override fun resolve(node: PageNode, context: PageNode?, skipExtension: Boolean): String = + override fun resolve(node: PageNode, context: PageNode?, skipExtension: Boolean) = pathTo(node, context) + if (!skipExtension) extension else "" - override fun resolve(dri: DRI, sourceSets: Set, context: PageNode?): String = + override fun resolve(dri: DRI, sourceSets: Set, context: PageNode?) = pagesIndex[dri]?.let { resolve(it, context) } ?: anchorsIndex[dri]?.let { resolve(it, context) + "#$dri" } // Not found in PageGraph, that means it's an external link diff --git a/plugins/base/src/main/kotlin/resolvers/local/LocationProvider.kt b/plugins/base/src/main/kotlin/resolvers/local/LocationProvider.kt index 5077195f..391af004 100644 --- a/plugins/base/src/main/kotlin/resolvers/local/LocationProvider.kt +++ b/plugins/base/src/main/kotlin/resolvers/local/LocationProvider.kt @@ -6,8 +6,8 @@ import org.jetbrains.dokka.pages.PageNode import org.jetbrains.dokka.pages.RootPageNode interface LocationProvider { - fun resolve(dri: DRI, sourceSets: Set, context: PageNode? = null): String - fun resolve(node: PageNode, context: PageNode? = null, skipExtension: Boolean = false): String + fun resolve(dri: DRI, sourceSets: Set, context: PageNode? = null): String? + fun resolve(node: PageNode, context: PageNode? = null, skipExtension: Boolean = false): String? fun resolveRoot(node: PageNode): String fun ancestors(node: PageNode): List } diff --git a/plugins/base/src/main/kotlin/resolvers/local/MultimoduleLocationProvider.kt b/plugins/base/src/main/kotlin/resolvers/local/MultimoduleLocationProvider.kt index 75c2ccf6..5d2a96d5 100644 --- a/plugins/base/src/main/kotlin/resolvers/local/MultimoduleLocationProvider.kt +++ b/plugins/base/src/main/kotlin/resolvers/local/MultimoduleLocationProvider.kt @@ -14,12 +14,12 @@ class MultimoduleLocationProvider(private val root: RootPageNode, context: Dokka it.name to it.path }.toMap() - override fun resolve(dri: DRI, sourceSets: Set, context: PageNode?): String = + override fun resolve(dri: DRI, sourceSets: Set, context: PageNode?) = dri.takeIf { it.packageName == MULTIMODULE_PACKAGE_PLACEHOLDER }?.classNames?.let { paths[it] }?.let { "$it/${identifierToFilename(dri.classNames.orEmpty())}/index.html" } ?: defaultLocationProvider.resolve(dri, sourceSets, context) - override fun resolve(node: PageNode, context: PageNode?, skipExtension: Boolean): String = + override fun resolve(node: PageNode, context: PageNode?, skipExtension: Boolean) = defaultLocationProvider.resolve(node, context, skipExtension) override fun resolveRoot(node: PageNode): String = defaultLocationProvider.resolveRoot(node) diff --git a/plugins/gfm/src/main/kotlin/GfmPlugin.kt b/plugins/gfm/src/main/kotlin/GfmPlugin.kt index b565deb5..3cec8320 100644 --- a/plugins/gfm/src/main/kotlin/GfmPlugin.kt +++ b/plugins/gfm/src/main/kotlin/GfmPlugin.kt @@ -318,12 +318,12 @@ open class CommonmarkRenderer( get() = this !is RendererSpecificPage || strategy != RenderingStrategy.DoNothing private fun StringBuilder.buildLink(to: PageNode, from: PageNode) = - buildLink(locationProvider.resolve(to, from)) { + buildLink(locationProvider.resolve(to, from)!!) { append(to.name) } override suspend fun renderPage(page: PageNode) { - val path by lazy { locationProvider.resolve(page, skipExtension = true) } + val path by lazy { locationProvider.resolve(page, skipExtension = true)!! } when (page) { is ContentPage -> outputWriter.write(path, buildPage(page) { c, p -> buildPageContent(c, p) }, ".md") is RendererSpecificPage -> when (val strategy = page.strategy) { diff --git a/plugins/javadoc/src/main/kotlin/org/jetbrains/dokka/javadoc/location/JavadocLocationProvider.kt b/plugins/javadoc/src/main/kotlin/org/jetbrains/dokka/javadoc/location/JavadocLocationProvider.kt index d5499f0b..9b0d8b04 100644 --- a/plugins/javadoc/src/main/kotlin/org/jetbrains/dokka/javadoc/location/JavadocLocationProvider.kt +++ b/plugins/javadoc/src/main/kotlin/org/jetbrains/dokka/javadoc/location/JavadocLocationProvider.kt @@ -76,7 +76,7 @@ class JavadocLocationProvider(pageRoot: RootPageNode, dokkaContext: DokkaContext "${resolve(it, context, skipExtension = true)}.html#$anchor" } ?: getExternalLocation(dri, sourceSets) - } + private fun JavadocFunctionNode.getAnchor(): String = "$name(${parameters.joinToString(",") { @@ -89,18 +89,20 @@ class JavadocLocationProvider(pageRoot: RootPageNode, dokkaContext: DokkaContext is JavaObject -> "Object" else -> bound.toString() } - }})" + })" fun anchorForFunctionNode(node: JavadocFunctionNode) = node.getAnchor() private fun anchorForDri(dri: DRI): String = dri.callable?.let { callable -> - "${callable.name}(${callable.params.joinToString(",") { - ((it as? Nullable)?.wrapped ?: it).toString() - }})" + "${callable.name}(${ + callable.params.joinToString(",") { + ((it as? Nullable)?.wrapped ?: it).toString() + } + })" } ?: dri.classNames.orEmpty() - override fun resolve(node: PageNode, context: PageNode?, skipExtension: Boolean): String = + override fun resolve(node: PageNode, context: PageNode?, skipExtension: Boolean) = pathIndex[node]?.relativeTo(pathIndex[context].orEmpty())?.let { if (skipExtension) it.removeSuffix(".html") else it } ?: run { diff --git a/plugins/javadoc/src/main/kotlin/org/jetbrains/dokka/javadoc/renderer/KorteJavadocRenderer.kt b/plugins/javadoc/src/main/kotlin/org/jetbrains/dokka/javadoc/renderer/KorteJavadocRenderer.kt index 4d5b9a6c..669f4979 100644 --- a/plugins/javadoc/src/main/kotlin/org/jetbrains/dokka/javadoc/renderer/KorteJavadocRenderer.kt +++ b/plugins/javadoc/src/main/kotlin/org/jetbrains/dokka/javadoc/renderer/KorteJavadocRenderer.kt @@ -148,7 +148,7 @@ class KorteJavadocRenderer(private val outputWriter: OutputWriter, val context: " implements " + it.joinToString(", ") { n -> listOfNotNull( n.packageName, - buildLink(n.toLink(), n.classNames.orEmpty()) + n.toLink()?.let{ buildLink(it, n.classNames.orEmpty()) } ?: n ).joinToString(".") } }.orEmpty() diff --git a/plugins/javadoc/src/main/kotlin/org/jetbrains/dokka/javadoc/renderer/SearchScriptsCreator.kt b/plugins/javadoc/src/main/kotlin/org/jetbrains/dokka/javadoc/renderer/SearchScriptsCreator.kt index cc1995eb..736a1f93 100644 --- a/plugins/javadoc/src/main/kotlin/org/jetbrains/dokka/javadoc/renderer/SearchScriptsCreator.kt +++ b/plugins/javadoc/src/main/kotlin/org/jetbrains/dokka/javadoc/renderer/SearchScriptsCreator.kt @@ -103,7 +103,7 @@ class SearchScriptsCreator(private val locationProvider: JavadocLocationProvider packageName = it.first.name, classlikeName = it.second.name, input = function, - url = locationProvider.resolve(function.dri, it.first.sourceSets()) + url = locationProvider.resolve(function.dri, it.first.sourceSets())!! ) } } @@ -114,7 +114,7 @@ class SearchScriptsCreator(private val locationProvider: JavadocLocationProvider packageName = it.first.name, classlikeName = it.second.name, property, - locationProvider.resolve(property.dri, it.first.sourceSets()) + locationProvider.resolve(property.dri, it.first.sourceSets())!! ) } } @@ -125,7 +125,7 @@ class SearchScriptsCreator(private val locationProvider: JavadocLocationProvider packageName = it.first.name, classlikeName = it.second.name, entry, - locationProvider.resolve(entry.dri, it.first.sourceSets()) + locationProvider.resolve(entry.dri, it.first.sourceSets())!! ) } } @@ -185,7 +185,7 @@ class SearchScriptsCreator(private val locationProvider: JavadocLocationProvider sourceSets: Set, label: String ): String = - locationProvider.resolve(dri, sourceSets).formatToEndWithHtml() + "#" + label + locationProvider.resolve(dri, sourceSets)!!.formatToEndWithHtml() + "#" + label } private data class SearchRecord( -- cgit