package org.jetbrains.dokka.pages import org.jetbrains.dokka.model.Documentable import org.jetbrains.dokka.Platform import org.jetbrains.dokka.links.DRI import java.util.* interface PageNode { val name: String val content: ContentNode val dri: DRI val documentable: Documentable? val embeddedResources: List val children: List fun modified( name: String = this.name, content: ContentNode = this.content, embeddedResources: List = this.embeddedResources, children: List = this.children ): PageNode } class ModulePageNode( override val name: String, override val content: ContentNode, override val documentable: Documentable?, override val children: List, override val embeddedResources: List = listOf() ) : PageNode { override val dri: DRI = DRI.topLevel override fun modified( name: String, content: ContentNode, embeddedResources: List, children: List ): ModulePageNode = if (name == this.name && content === this.content && embeddedResources === this.embeddedResources && children shallowEq this.children) this else ModulePageNode(name, content, documentable, children, embeddedResources) private fun PageNode.transformNode(operation: (PageNode) -> PageNode): PageNode = operation(this).let { newNode -> newNode.modified(children = newNode.children.map { it.transformNode(operation) }) } fun transformPageNodeTree(operation: (PageNode) -> PageNode) = this.transformNode(operation) as ModulePageNode val parentMap: IdentityHashMap by lazy { IdentityHashMap().apply { fun addParent(parent: PageNode) { parent.children.forEach { child -> put(child, parent) addParent(child) } } addParent(this@ModulePageNode) } } } class PackagePageNode( override val name: String, override val content: ContentNode, override val dri: DRI, override val documentable: Documentable?, override val children: List, override val embeddedResources: List = listOf() ) : PageNode { override fun modified( name: String, content: ContentNode, 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, documentable, children, embeddedResources) } class ClassPageNode( override val name: String, override val content: ContentNode, override val dri: DRI, override val documentable: Documentable?, override val children: List, override val embeddedResources: List = listOf() ) : PageNode { override fun modified( name: String, content: ContentNode, embeddedResources: List, children: List ): ClassPageNode = if (name == this.name && content === this.content && embeddedResources === this.embeddedResources && children shallowEq this.children) this else ClassPageNode(name, content, dri, documentable, children, embeddedResources) } class MemberPageNode( override val name: String, override val content: ContentNode, override val dri: DRI, override val documentable: Documentable?, override val children: List = emptyList(), override val embeddedResources: List = listOf() ) : PageNode { override fun modified( name: String, content: ContentNode, 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, documentable, children, embeddedResources) } data class PlatformData(val name: String, val platformType: Platform, val targets: List) { override fun toString() = targets.toString() } fun PageNode.dfs(predicate: (PageNode) -> Boolean): PageNode? = if (predicate(this)) { this } else { this.children.asSequence().mapNotNull { it.dfs(predicate) }.firstOrNull() } private infix fun List.shallowEq(other: List) = this === other || (this.size == other.size && (this zip other).all { (a, b) -> a === b }) // Navigation?? // content modifier? //data class ContentLink(val link: String): ContentNode