/* * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ package org.jetbrains.dokka.pages import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.model.Documentable import org.jetbrains.dokka.model.WithChildren import java.util.* public interface PageNode : WithChildren { public val name: String override val children: List public fun modified( name: String = this.name, children: List = this.children ): PageNode } public interface ContentPage : PageNode { public val content: ContentNode public val dri: Set public val embeddedResources: List @Deprecated("Deprecated. Remove its usages from your code.", ReplaceWith("this.documentables.firstOrNull()") ) public val documentable: Documentable? get() = if (this is WithDocumentables) this.documentables.firstOrNull() else null public fun modified( name: String = this.name, content: ContentNode = this.content, dri: Set = this.dri, embeddedResources: List = this.embeddedResources, children: List = this.children ): ContentPage } public interface WithDocumentables { public val documentables: List } public abstract class RootPageNode( public val forceTopLevelName: Boolean = false ) : PageNode { public val parentMap: Map by lazy { IdentityHashMap().apply { fun process(parent: PageNode) { parent.children.forEach { child -> put(child, parent) process(child) } } process(this@RootPageNode) } } public fun transformPageNodeTree(operation: (PageNode) -> PageNode): RootPageNode = this.transformNode(operation) as RootPageNode public fun transformContentPagesTree(operation: (ContentPage) -> ContentPage): RootPageNode = transformPageNodeTree { if (it is ContentPage) operation(it) else it } private fun PageNode.transformNode(operation: (PageNode) -> PageNode): PageNode = operation(this).let { newNode -> newNode.modified(children = newNode.children.map { it.transformNode(operation) }) } abstract override fun modified( name: String, children: List ): RootPageNode } public class ModulePageNode( override val name: String, override val content: ContentNode, override val documentables: List = listOf(), override val children: List, override val embeddedResources: List = listOf() ) : RootPageNode(), ModulePage { override val dri: Set = setOf(DRI.topLevel) override fun modified(name: String, children: List): ModulePageNode = modified(name = name, content = this.content, dri = dri, children = children) override fun modified( name: String, content: ContentNode, dri: Set, embeddedResources: List, children: List ): ModulePageNode = if (name == this.name && content === this.content && embeddedResources === this.embeddedResources && children shallowEq this.children) this else ModulePageNode(name, content, documentables, children, embeddedResources) } public class PackagePageNode( override val name: String, override val content: ContentNode, override val dri: Set, override val documentables: List = listOf(), override val children: List, override val embeddedResources: List = listOf() ) : PackagePage { init { require(name.isNotBlank()) { "PackagePageNode.name cannot be blank" } } override fun modified(name: String, children: List): PackagePageNode = modified(name = name, content = this.content, children = children) override fun modified( name: String, content: ContentNode, dri: Set, embeddedResources: List, children: List ): PackagePageNode = if (name == this.name && content === this.content && embeddedResources === this.embeddedResources && children shallowEq this.children) this else PackagePageNode(name, content, dri, documentables, children, embeddedResources) } public class ClasslikePageNode( override val name: String, override val content: ContentNode, override val dri: Set, override val documentables: List = listOf(), override val children: List, override val embeddedResources: List = listOf() ) : ClasslikePage { override fun modified(name: String, children: List): ClasslikePageNode = modified(name = name, content = this.content, children = children) override fun modified( name: String, content: ContentNode, dri: Set, embeddedResources: List, children: List ): ClasslikePageNode = if (name == this.name && content === this.content && embeddedResources === this.embeddedResources && children shallowEq this.children) this else ClasslikePageNode(name, content, dri, documentables, children, embeddedResources) } public class MemberPageNode( override val name: String, override val content: ContentNode, override val dri: Set, override val documentables: List = listOf(), override val children: List = emptyList(), override val embeddedResources: List = listOf() ) : MemberPage { override fun modified(name: String, children: List): MemberPageNode = modified(name = name, content = this.content, children = children) override fun modified( name: String, content: ContentNode, dri: Set, embeddedResources: List, children: List ): MemberPageNode = if (name == this.name && content === this.content && embeddedResources === this.embeddedResources && children shallowEq this.children) this else MemberPageNode(name, content, dri, documentables, children, embeddedResources) } public class MultimoduleRootPageNode( override val dri: Set, override val content: ContentNode, override val embeddedResources: List = emptyList() ) : RootPageNode(forceTopLevelName = true), MultimoduleRootPage { override val name: String = "All modules" override val children: List = emptyList() override fun modified(name: String, children: List): RootPageNode = MultimoduleRootPageNode(dri, content, embeddedResources) override fun modified( name: String, content: ContentNode, dri: Set, embeddedResources: List, children: List ): ContentPage = if (name == this.name && content === this.content && embeddedResources === this.embeddedResources && children shallowEq this.children) this else MultimoduleRootPageNode(dri, content, embeddedResources) } public inline fun PageNode.children(): List = children.filterIsInstance() private infix fun List.shallowEq(other: List) = this === other || (this.size == other.size && (this zip other).all { (a, b) -> a === b })