diff options
-rw-r--r-- | core/src/main/kotlin/pages/PageNodes.kt | 6 | ||||
-rw-r--r-- | core/src/main/kotlin/resolvers/DefaultLocationProvider.kt | 58 |
2 files changed, 37 insertions, 27 deletions
diff --git a/core/src/main/kotlin/pages/PageNodes.kt b/core/src/main/kotlin/pages/PageNodes.kt index 46109d87..3d2b6fb7 100644 --- a/core/src/main/kotlin/pages/PageNodes.kt +++ b/core/src/main/kotlin/pages/PageNodes.kt @@ -18,6 +18,12 @@ abstract class PageNode( fun appendChildren(children: List<PageNode>) = _children.addAll(children) fun appendChild(child: PageNode) = _children.add(child) + + override fun equals(other: Any?): Boolean = + if (other is PageNode) { + dri?.equals(other.dri) ?: (other.dri == null && name == other.name) + } + else false } class ModulePageNode( diff --git a/core/src/main/kotlin/resolvers/DefaultLocationProvider.kt b/core/src/main/kotlin/resolvers/DefaultLocationProvider.kt index a2756d42..65b27779 100644 --- a/core/src/main/kotlin/resolvers/DefaultLocationProvider.kt +++ b/core/src/main/kotlin/resolvers/DefaultLocationProvider.kt @@ -8,38 +8,42 @@ import org.jetbrains.dokka.pages.* open class DefaultLocationProvider(private val pageGraphRoot: PageNode, val configuration: DokkaConfiguration, val extension: String): LocationProvider { // TODO: cache override fun resolve(node: PageNode, context: PageNode?): String = pathTo(node, context) + extension - override fun resolve(dri: DRI, platforms: List<PlatformData>, context: PageNode?): String { - findInPageGraph(dri, platforms)?.let { return resolve(it, context) } + override fun resolve(dri: DRI, platforms: List<PlatformData>, context: PageNode?): String = + findInPageGraph(dri, platforms)?.let { resolve(it, context) } ?: // Not found in PageGraph, that means it's an external link - - val externalDocs = configuration.passesConfigurations + ExternalLocationProvider.getLocation(dri, + configuration.passesConfigurations .filter { passConfig -> passConfig.targets.toSet() == platforms.toSet() } // TODO: change targets to something better? .flatMap { it.externalDocumentationLinks }.map { it.packageListUrl }.distinct() - - return ExternalLocationProvider.getLocation(dri, externalDocs) - } + ) protected open fun findInPageGraph(dri: DRI, platforms: List<PlatformData>): PageNode? = pageGraphRoot.dfs { it.dri == dri } - protected open fun pathTo(node: PageNode, context: PageNode?): String { // TODO: can be refactored probably, also we should think about root - fun parentPath(node: PageNode?): String { - if (node == null) return "" - val parts = node.parent?.let(::parentPath) ?: "" - return if(node is PackagePageNode) { - "$parts${node.name}/" - } else if (parts.isNotBlank()) { - "$parts${identifierToFilename(node.name)}/" - } else { - identifierToFilename(node.name) + "/" - } - } - val resolved = parentPath(node.parent) + identifierToFilename(node.name) + - if (node.children.isEmpty()) { - "" - } else { - "/index" - } - return context?.let { resolved.replace(pathTo(it, null).dropLastWhile { it != '/' }, "") } ?: resolved + protected open fun pathTo(node: PageNode, context: PageNode?): String { + + fun getPath(pathNode: PageNode?, path: MutableList<String> = mutableListOf()): List<String> = + if (pathNode == null || pathNode == pageGraphRoot) + path + else + getPath(pathNode.parent, path.apply { + add(if (node is PackagePageNode) { + node.name + } else { + identifierToFilename(node.name) + }) + }) + + val nodePath = getPath(node).reversed() + val contextPath = getPath(context).reversed() + + val commonPathElements = nodePath.zip(contextPath).takeWhile { (a, b) -> a == b }.size + + return ( List(contextPath.size - commonPathElements) { ".." } + nodePath.drop(commonPathElements) ).joinToString("/") + + if(node.children.isNotEmpty()) PAGE_WITH_CHILDREN_SUFFIX else "" + } + + private companion object { + const val PAGE_WITH_CHILDREN_SUFFIX = "/index" } } @@ -76,7 +80,7 @@ fun DRI.toDokkaLocation(extension: String): String { // TODO: classes without pa return "$classLink/${identifierToFilename(callable.name)}$extension" } -private val reservedFilenames = setOf("index", "con", "aux", "lst", "prn", "nul", "eof", "inp", "out") +private val reservedFilenames = setOf("index", "con", "aux", "lst", "prn", "nul", "eof", "inp", "out") private fun identifierToFilename(name: String): String { if (name.isEmpty()) return "--root--" |