diff options
author | Ignat Beresnev <ignat.beresnev@jetbrains.com> | 2023-11-10 11:46:54 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-10 11:46:54 +0100 |
commit | 8e5c63d035ef44a269b8c43430f43f5c8eebfb63 (patch) | |
tree | 1b915207b2b9f61951ddbf0ff2e687efd053d555 /plugins/base/src/main/kotlin/transformers/pages | |
parent | a44efd4ba0c2e4ab921ff75e0f53fc9335aa79db (diff) | |
download | dokka-8e5c63d035ef44a269b8c43430f43f5c8eebfb63.tar.gz dokka-8e5c63d035ef44a269b8c43430f43f5c8eebfb63.tar.bz2 dokka-8e5c63d035ef44a269b8c43430f43f5c8eebfb63.zip |
Restructure the project to utilize included builds (#3174)
* Refactor and simplify artifact publishing
* Update Gradle to 8.4
* Refactor and simplify convention plugins and build scripts
Fixes #3132
---------
Co-authored-by: Adam <897017+aSemy@users.noreply.github.com>
Co-authored-by: Oleg Yukhnevich <whyoleg@gmail.com>
Diffstat (limited to 'plugins/base/src/main/kotlin/transformers/pages')
12 files changed, 0 insertions, 1022 deletions
diff --git a/plugins/base/src/main/kotlin/transformers/pages/DefaultSamplesTransformer.kt b/plugins/base/src/main/kotlin/transformers/pages/DefaultSamplesTransformer.kt deleted file mode 100644 index 1ba049c8..00000000 --- a/plugins/base/src/main/kotlin/transformers/pages/DefaultSamplesTransformer.kt +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package org.jetbrains.dokka.base.transformers.pages - -import org.jetbrains.dokka.links.DRI -import org.jetbrains.dokka.model.DisplaySourceSet -import org.jetbrains.dokka.model.doc.Sample -import org.jetbrains.dokka.model.properties.PropertyContainer -import org.jetbrains.dokka.pages.* -import org.jetbrains.dokka.plugability.DokkaContext -import org.jetbrains.dokka.plugability.plugin -import org.jetbrains.dokka.plugability.querySingle -import org.jetbrains.dokka.transformers.pages.PageTransformer -import org.jetbrains.dokka.analysis.kotlin.internal.InternalKotlinAnalysisPlugin -import org.jetbrains.dokka.analysis.kotlin.internal.SampleProvider -import org.jetbrains.dokka.analysis.kotlin.internal.SampleProviderFactory - -internal const val KOTLIN_PLAYGROUND_SCRIPT = "https://unpkg.com/kotlin-playground@1/dist/playground.min.js" - -internal class DefaultSamplesTransformer(val context: DokkaContext) : PageTransformer { - - private val sampleProviderFactory: SampleProviderFactory = context.plugin<InternalKotlinAnalysisPlugin>().querySingle { sampleProviderFactory } - - override fun invoke(input: RootPageNode): RootPageNode { - return sampleProviderFactory.build().use { sampleProvider -> - input.transformContentPagesTree { page -> - val samples = (page as? WithDocumentables)?.documentables?.flatMap { - it.documentation.entries.flatMap { entry -> - entry.value.children.filterIsInstance<Sample>().map { entry.key to it } - } - } ?: return@transformContentPagesTree page - - val newContent = samples.fold(page.content) { acc, (sampleSourceSet, sample) -> - sampleProvider.getSample(sampleSourceSet, sample.name) - ?.let { - acc.addSample(page, sample.name, it) - } ?: acc - } - - page.modified( - content = newContent, - embeddedResources = page.embeddedResources + KOTLIN_PLAYGROUND_SCRIPT - ) - } - } - } - - - private fun ContentNode.addSample( - contentPage: ContentPage, - fqLink: String, - sample: SampleProvider.SampleSnippet, - ): ContentNode { - val node = contentCode(contentPage.content.sourceSets, contentPage.dri, createSampleBody(sample.imports, sample.body), "kotlin") - return dfs(fqLink, node) - } - - fun createSampleBody(imports: String, body: String) = - """ |$imports - |fun main() { - | //sampleStart - | $body - | //sampleEnd - |}""".trimMargin() - - private fun ContentNode.dfs(fqName: String, node: ContentCodeBlock): ContentNode { - return when (this) { - is ContentHeader -> copy(children.map { it.dfs(fqName, node) }) - is ContentDivergentGroup -> @Suppress("UNCHECKED_CAST") copy(children.map { - it.dfs(fqName, node) - } as List<ContentDivergentInstance>) - is ContentDivergentInstance -> copy( - before.let { it?.dfs(fqName, node) }, - divergent.dfs(fqName, node), - after.let { it?.dfs(fqName, node) }) - is ContentCodeBlock -> copy(children.map { it.dfs(fqName, node) }) - is ContentCodeInline -> copy(children.map { it.dfs(fqName, node) }) - is ContentDRILink -> copy(children.map { it.dfs(fqName, node) }) - is ContentResolvedLink -> copy(children.map { it.dfs(fqName, node) }) - is ContentEmbeddedResource -> copy(children.map { it.dfs(fqName, node) }) - is ContentTable -> copy(children = children.map { it.dfs(fqName, node) as ContentGroup }) - is ContentList -> copy(children.map { it.dfs(fqName, node) }) - is ContentGroup -> copy(children.map { it.dfs(fqName, node) }) - is PlatformHintedContent -> copy(inner.dfs(fqName, node)) - is ContentText -> if (text == fqName) node else this - is ContentBreakLine -> this - else -> this.also { context.logger.error("Could not recognize $this ContentNode in SamplesTransformer") } - } - } - - private fun contentCode( - sourceSets: Set<DisplaySourceSet>, - dri: Set<DRI>, - content: String, - language: String, - styles: Set<Style> = emptySet(), - extra: PropertyContainer<ContentNode> = PropertyContainer.empty() - ) = - ContentCodeBlock( - children = listOf( - ContentText( - text = content, - dci = DCI(dri, ContentKind.Sample), - sourceSets = sourceSets, - style = emptySet(), - extra = PropertyContainer.empty() - ) - ), - language = language, - dci = DCI(dri, ContentKind.Sample), - sourceSets = sourceSets, - style = styles + ContentStyle.RunnableSample + TextStyle.Monospace, - extra = extra - ) -} diff --git a/plugins/base/src/main/kotlin/transformers/pages/annotations/SinceKotlinTransformer.kt b/plugins/base/src/main/kotlin/transformers/pages/annotations/SinceKotlinTransformer.kt deleted file mode 100644 index 9ff5960d..00000000 --- a/plugins/base/src/main/kotlin/transformers/pages/annotations/SinceKotlinTransformer.kt +++ /dev/null @@ -1,186 +0,0 @@ -/* - * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package org.jetbrains.dokka.base.transformers.pages.annotations - - -import org.jetbrains.dokka.DokkaConfiguration -import org.jetbrains.dokka.Platform -import org.jetbrains.dokka.analysis.markdown.jb.MARKDOWN_ELEMENT_FILE_NAME -import org.jetbrains.dokka.base.signatures.KotlinSignatureUtils.annotations -import org.jetbrains.dokka.model.* -import org.jetbrains.dokka.model.doc.CustomDocTag -import org.jetbrains.dokka.model.doc.CustomTagWrapper -import org.jetbrains.dokka.model.doc.DocumentationNode -import org.jetbrains.dokka.model.doc.Text -import org.jetbrains.dokka.plugability.DokkaContext -import org.jetbrains.dokka.transformers.documentation.DocumentableTransformer -import org.jetbrains.dokka.utilities.associateWithNotNull - -public class SinceKotlinVersion(str: String) : Comparable<SinceKotlinVersion> { - private val parts: List<Int> = str.split(".").map { it.toInt() } - - /** - * Corner case: 1.0 == 1.0.0 - */ - override fun compareTo(other: SinceKotlinVersion): Int { - val i1 = parts.listIterator() - val i2 = other.parts.listIterator() - - while (i1.hasNext() || i2.hasNext()) { - val diff = (if (i1.hasNext()) i1.next() else 0) - (if (i2.hasNext()) i2.next() else 0) - if (diff != 0) return diff - } - - return 0 - } - - override fun toString(): String = parts.joinToString(".") -} - -public class SinceKotlinTransformer( - public val context: DokkaContext -) : DocumentableTransformer { - - private val minSinceKotlinVersionOfPlatform = mapOf( - Platform.common to SinceKotlinVersion("1.0"), - Platform.jvm to SinceKotlinVersion("1.0"), - Platform.js to SinceKotlinVersion("1.1"), - Platform.native to SinceKotlinVersion("1.3"), - Platform.wasm to SinceKotlinVersion("1.8"), - ) - - override fun invoke(original: DModule, context: DokkaContext): DModule = original.transform() as DModule - - private fun <T : Documentable> T.transform(parent: SourceSetDependent<SinceKotlinVersion>? = null): Documentable { - val versions = calculateVersions(parent) - return when (this) { - is DModule -> copy( - packages = packages.map { it.transform() as DPackage } - ) - - is DPackage -> copy( - classlikes = classlikes.map { it.transform() as DClasslike }, - functions = functions.map { it.transform() as DFunction }, - properties = properties.map { it.transform() as DProperty }, - typealiases = typealiases.map { it.transform() as DTypeAlias } - ) - - is DClass -> copy( - documentation = appendSinceKotlin(versions), - classlikes = classlikes.map { it.transform(versions) as DClasslike }, - functions = functions.map { it.transform(versions) as DFunction }, - properties = properties.map { it.transform(versions) as DProperty } - ) - - is DEnum -> copy( - documentation = appendSinceKotlin(versions), - classlikes = classlikes.map { it.transform(versions) as DClasslike }, - functions = functions.map { it.transform(versions) as DFunction }, - properties = properties.map { it.transform(versions) as DProperty } - ) - - is DInterface -> copy( - documentation = appendSinceKotlin(versions), - classlikes = classlikes.map { it.transform(versions) as DClasslike }, - functions = functions.map { it.transform(versions) as DFunction }, - properties = properties.map { it.transform(versions) as DProperty } - ) - - is DObject -> copy( - documentation = appendSinceKotlin(versions), - classlikes = classlikes.map { it.transform(versions) as DClasslike }, - functions = functions.map { it.transform(versions) as DFunction }, - properties = properties.map { it.transform(versions) as DProperty } - ) - - is DTypeAlias -> copy( - documentation = appendSinceKotlin(versions) - ) - - is DAnnotation -> copy( - documentation = appendSinceKotlin(versions), - classlikes = classlikes.map { it.transform(versions) as DClasslike }, - functions = functions.map { it.transform(versions) as DFunction }, - properties = properties.map { it.transform(versions) as DProperty } - ) - - is DFunction -> copy( - documentation = appendSinceKotlin(versions) - ) - - is DProperty -> copy( - documentation = appendSinceKotlin(versions) - ) - - is DParameter -> copy( - documentation = appendSinceKotlin(versions) - ) - - else -> this.also { context.logger.warn("Unrecognized documentable $this while SinceKotlin transformation") } - } - } - - private fun List<Annotations.Annotation>.findSinceKotlinAnnotation(): Annotations.Annotation? = - this.find { it.dri.packageName == "kotlin" && it.dri.classNames == "SinceKotlin" } - - private fun Documentable.getVersion(sourceSet: DokkaConfiguration.DokkaSourceSet): SinceKotlinVersion { - val annotatedVersion = - annotations()[sourceSet] - ?.findSinceKotlinAnnotation() - ?.params?.let { it["version"] as? StringValue }?.value - ?.let { SinceKotlinVersion(it) } - - val minSinceKotlin = minSinceKotlinVersionOfPlatform[sourceSet.analysisPlatform] - ?: throw IllegalStateException("No value for platform: ${sourceSet.analysisPlatform}") - - return annotatedVersion?.takeIf { version -> version >= minSinceKotlin } ?: minSinceKotlin - } - - - private fun Documentable.calculateVersions(parent: SourceSetDependent<SinceKotlinVersion>?): SourceSetDependent<SinceKotlinVersion> { - return sourceSets.associateWithNotNull { sourceSet -> - val version = getVersion(sourceSet) - val parentVersion = parent?.get(sourceSet) - if (parentVersion != null) - maxOf(version, parentVersion) - else - version - } - } - - private fun Documentable.appendSinceKotlin(versions: SourceSetDependent<SinceKotlinVersion>) = - sourceSets.fold(documentation) { acc, sourceSet -> - - val version = versions[sourceSet] - - val sinceKotlinCustomTag = CustomTagWrapper( - CustomDocTag( - listOf( - Text( - version.toString() - ) - ), - name = MARKDOWN_ELEMENT_FILE_NAME - ), - "Since Kotlin" - ) - if (acc[sourceSet] == null) - acc + (sourceSet to DocumentationNode(listOf(sinceKotlinCustomTag))) - else - acc.mapValues { - if (it.key == sourceSet) it.value.copy( - it.value.children + listOf( - sinceKotlinCustomTag - ) - ) else it.value - } - } - - internal companion object { - internal const val SHOULD_DISPLAY_SINCE_KOTLIN_SYS_PROP = "dokka.shouldDisplaySinceKotlin" - internal fun shouldDisplaySinceKotlin() = - System.getProperty(SHOULD_DISPLAY_SINCE_KOTLIN_SYS_PROP) in listOf("true", "1") - } -} diff --git a/plugins/base/src/main/kotlin/transformers/pages/comments/CommentsToContentConverter.kt b/plugins/base/src/main/kotlin/transformers/pages/comments/CommentsToContentConverter.kt deleted file mode 100644 index 6ca3f8d0..00000000 --- a/plugins/base/src/main/kotlin/transformers/pages/comments/CommentsToContentConverter.kt +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package org.jetbrains.dokka.base.transformers.pages.comments - -import org.jetbrains.dokka.DokkaConfiguration.DokkaSourceSet -import org.jetbrains.dokka.model.doc.DocTag -import org.jetbrains.dokka.model.properties.PropertyContainer -import org.jetbrains.dokka.pages.ContentNode -import org.jetbrains.dokka.pages.DCI -import org.jetbrains.dokka.pages.Style - -public interface CommentsToContentConverter { - public fun buildContent( - docTag: DocTag, - dci: DCI, - sourceSets: Set<DokkaSourceSet>, - styles: Set<Style> = emptySet(), - extras: PropertyContainer<ContentNode> = PropertyContainer.empty() - ): List<ContentNode> -} diff --git a/plugins/base/src/main/kotlin/transformers/pages/comments/DocTagToContentConverter.kt b/plugins/base/src/main/kotlin/transformers/pages/comments/DocTagToContentConverter.kt deleted file mode 100644 index e4e0f53f..00000000 --- a/plugins/base/src/main/kotlin/transformers/pages/comments/DocTagToContentConverter.kt +++ /dev/null @@ -1,270 +0,0 @@ -/* - * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package org.jetbrains.dokka.base.transformers.pages.comments - - -import org.jetbrains.dokka.DokkaConfiguration.DokkaSourceSet -import org.jetbrains.dokka.analysis.markdown.jb.MARKDOWN_ELEMENT_FILE_NAME -import org.jetbrains.dokka.model.doc.* -import org.jetbrains.dokka.model.properties.PropertyContainer -import org.jetbrains.dokka.model.properties.plus -import org.jetbrains.dokka.model.toDisplaySourceSets -import org.jetbrains.dokka.pages.* -import org.jetbrains.dokka.utilities.firstIsInstanceOrNull - -public open class DocTagToContentConverter : CommentsToContentConverter { - override fun buildContent( - docTag: DocTag, - dci: DCI, - sourceSets: Set<DokkaSourceSet>, - styles: Set<Style>, - extras: PropertyContainer<ContentNode> - ): List<ContentNode> { - - fun buildChildren(docTag: DocTag, newStyles: Set<Style> = emptySet(), newExtras: SimpleAttr? = null) = - docTag.children.flatMap { - buildContent(it, dci, sourceSets, styles + newStyles, newExtras?.let { extras + it } ?: extras) - } - - fun buildTableRows(rows: List<DocTag>, newStyle: Style): List<ContentGroup> = - rows.flatMap { - @Suppress("UNCHECKED_CAST") - buildContent(it, dci, sourceSets, styles + newStyle, extras) as List<ContentGroup> - } - - fun buildHeader(level: Int) = - listOf( - ContentHeader( - buildChildren(docTag), - level, - dci, - sourceSets.toDisplaySourceSets(), - styles - ) - ) - - fun buildList(ordered: Boolean, newStyles: Set<Style> = emptySet(), start: Int = 1) = - listOf( - ContentList( - buildChildren(docTag), - ordered, - dci, - sourceSets.toDisplaySourceSets(), - styles + newStyles, - ((PropertyContainer.empty<ContentNode>()) + SimpleAttr("start", start.toString())) - ) - ) - - fun buildNewLine() = listOf( - ContentBreakLine( - sourceSets.toDisplaySourceSets() - ) - ) - - fun P.collapseParagraphs(): P = - if (children.size == 1 && children.first() is P) (children.first() as P).collapseParagraphs() else this - - return when (docTag) { - is H1 -> buildHeader(1) - is H2 -> buildHeader(2) - is H3 -> buildHeader(3) - is H4 -> buildHeader(4) - is H5 -> buildHeader(5) - is H6 -> buildHeader(6) - is Ul -> buildList(false) - is Ol -> buildList(true, start = docTag.params["start"]?.toInt() ?: 1) - is Li -> listOf( - ContentGroup(buildChildren(docTag), dci, sourceSets.toDisplaySourceSets(), styles, extras) - ) - is Dl -> buildList(false, newStyles = setOf(ListStyle.DescriptionList)) - is Dt -> listOf( - ContentGroup( - buildChildren(docTag), - dci, - sourceSets.toDisplaySourceSets(), - styles + ListStyle.DescriptionTerm - ) - ) - is Dd -> listOf( - ContentGroup( - buildChildren(docTag), - dci, - sourceSets.toDisplaySourceSets(), - styles + ListStyle.DescriptionDetails - ) - ) - is Br -> buildNewLine() - is B -> buildChildren(docTag, setOf(TextStyle.Strong)) - is I -> buildChildren(docTag, setOf(TextStyle.Italic)) - is P -> listOf( - ContentGroup( - buildChildren(docTag.collapseParagraphs()), - dci, - sourceSets.toDisplaySourceSets(), - styles + setOf(TextStyle.Paragraph), - extras - ) - ) - is A -> listOf( - ContentResolvedLink( - buildChildren(docTag), - docTag.params.getValue("href"), - dci, - sourceSets.toDisplaySourceSets(), - styles - ) - ) - is DocumentationLink -> listOf( - ContentDRILink( - buildChildren(docTag), - docTag.dri, - DCI( - setOf(docTag.dri), - ContentKind.Main - ), - sourceSets.toDisplaySourceSets(), - styles - ) - ) - is BlockQuote -> listOf( - ContentGroup( - buildChildren(docTag), - dci, - sourceSets.toDisplaySourceSets(), - styles + TextStyle.Quotation, - ) - ) - is Pre, is CodeBlock -> listOf( - ContentCodeBlock( - buildChildren(docTag), - docTag.params.getOrDefault("lang", ""), - dci, - sourceSets.toDisplaySourceSets(), - styles - ) - ) - is CodeInline -> listOf( - ContentCodeInline( - buildChildren(docTag), - "", - dci, - sourceSets.toDisplaySourceSets(), - styles - ) - ) - is Img -> listOf( - ContentEmbeddedResource( - address = docTag.params["href"]!!, - altText = docTag.params["alt"], - dci = dci, - sourceSets = sourceSets.toDisplaySourceSets(), - style = styles, - extra = extras - ) - ) - is HorizontalRule -> listOf( - ContentText( - "", - dci, - sourceSets.toDisplaySourceSets(), - setOf() - ) - ) - is Text -> listOf( - ContentText( - docTag.body, - dci, - sourceSets.toDisplaySourceSets(), - styles, - extras + HtmlContent.takeIf { docTag.params["content-type"] == "html" } - ) - ) - is Strikethrough -> buildChildren(docTag, setOf(TextStyle.Strikethrough)) - is Table -> { - //https://html.spec.whatwg.org/multipage/tables.html#the-caption-element - if (docTag.children.any { it is TBody }) { - val head = docTag.children.filterIsInstance<THead>().flatMap { it.children } - val body = docTag.children.filterIsInstance<TBody>().flatMap { it.children } - listOf( - ContentTable( - header = buildTableRows(head.filterIsInstance<Th>(), CommentTable), - caption = docTag.children.firstIsInstanceOrNull<Caption>()?.let { - ContentGroup( - buildContent(it, dci, sourceSets), - dci, - sourceSets.toDisplaySourceSets(), - styles, - extras - ) - }, - buildTableRows(body.filterIsInstance<Tr>(), CommentTable), - dci, - sourceSets.toDisplaySourceSets(), - styles + CommentTable - ) - ) - } else { - listOf( - ContentTable( - header = buildTableRows(docTag.children.filterIsInstance<Th>(), CommentTable), - caption = null, - buildTableRows(docTag.children.filterIsInstance<Tr>(), CommentTable), - dci, - sourceSets.toDisplaySourceSets(), - styles + CommentTable - ) - ) - } - } - is Th, - is Tr -> listOf( - ContentGroup( - docTag.children.map { - ContentGroup(buildChildren(it), dci, sourceSets.toDisplaySourceSets(), styles, extras) - }, - dci, - sourceSets.toDisplaySourceSets(), - styles - ) - ) - is Index -> listOf( - ContentGroup( - buildChildren(docTag, newStyles = styles + ContentStyle.InDocumentationAnchor), - dci, - sourceSets.toDisplaySourceSets(), - styles - ) - ) - is CustomDocTag -> if (docTag.isNonemptyFile()) { - listOf( - ContentGroup( - buildChildren(docTag), - dci, - sourceSets.toDisplaySourceSets(), - styles, - extra = extras - ) - ) - } else { - buildChildren(docTag) - } - is Caption -> listOf( - ContentGroup( - buildChildren(docTag), - dci, - sourceSets.toDisplaySourceSets(), - styles + ContentStyle.Caption, - extra = extras - ) - ) - is Var -> buildChildren(docTag, setOf(TextStyle.Var)) - is U -> buildChildren(docTag, setOf(TextStyle.Underlined)) - - else -> buildChildren(docTag) - } - } - - private fun CustomDocTag.isNonemptyFile() = name == MARKDOWN_ELEMENT_FILE_NAME && children.size > 1 -} diff --git a/plugins/base/src/main/kotlin/transformers/pages/merger/FallbackPageMergerStrategy.kt b/plugins/base/src/main/kotlin/transformers/pages/merger/FallbackPageMergerStrategy.kt deleted file mode 100644 index 80886cc5..00000000 --- a/plugins/base/src/main/kotlin/transformers/pages/merger/FallbackPageMergerStrategy.kt +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package org.jetbrains.dokka.base.transformers.pages.merger - -import org.jetbrains.dokka.pages.ContentPage -import org.jetbrains.dokka.pages.PageNode -import org.jetbrains.dokka.utilities.DokkaLogger - -public class FallbackPageMergerStrategy( - private val logger: DokkaLogger -) : PageMergerStrategy { - override fun tryMerge(pages: List<PageNode>, path: List<String>): List<PageNode> { - pages.map { - (it as? ContentPage) - } - val renderedPath = path.joinToString(separator = "/") - if (pages.size != 1) logger.warn("For $renderedPath: expected 1 page, but got ${pages.size}") - return listOf(pages.first()) - } -} diff --git a/plugins/base/src/main/kotlin/transformers/pages/merger/PageMerger.kt b/plugins/base/src/main/kotlin/transformers/pages/merger/PageMerger.kt deleted file mode 100644 index e52c233c..00000000 --- a/plugins/base/src/main/kotlin/transformers/pages/merger/PageMerger.kt +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package org.jetbrains.dokka.base.transformers.pages.merger - -import org.jetbrains.dokka.base.DokkaBase -import org.jetbrains.dokka.pages.PageNode -import org.jetbrains.dokka.pages.RootPageNode -import org.jetbrains.dokka.plugability.DokkaContext -import org.jetbrains.dokka.plugability.plugin -import org.jetbrains.dokka.plugability.query -import org.jetbrains.dokka.transformers.pages.PageTransformer - -public class PageMerger(context: DokkaContext) : PageTransformer { - - private val strategies: Iterable<PageMergerStrategy> = context.plugin<DokkaBase>().query { pageMergerStrategy } - - override fun invoke(input: RootPageNode): RootPageNode = - input.modified(children = input.children.map { it.mergeChildren(emptyList()) }) - - private fun PageNode.mergeChildren(path: List<String>): PageNode = children.groupBy { it::class }.map { - it.value.groupBy { it.name }.map { (n, v) -> mergePageNodes(v, path + n) }.map { it.assertSingle(path) } - }.let { pages -> - modified(children = pages.flatten().map { it.mergeChildren(path + it.name) }) - } - - private fun mergePageNodes(pages: List<PageNode>, path: List<String>): List<PageNode> = - strategies.fold(pages) { acc, strategy -> tryMerge(strategy, acc, path) } - - private fun tryMerge(strategy: PageMergerStrategy, pages: List<PageNode>, path: List<String>) = - if (pages.size > 1) strategy.tryMerge(pages, path) else pages -} - -private fun <T> Iterable<T>.assertSingle(path: List<String>): T = try { - single() - } catch (e: Exception) { - val renderedPath = path.joinToString(separator = "/") - throw IllegalStateException("Page merger is misconfigured. Error for $renderedPath: ${e.message}") - } diff --git a/plugins/base/src/main/kotlin/transformers/pages/merger/PageMergerStrategy.kt b/plugins/base/src/main/kotlin/transformers/pages/merger/PageMergerStrategy.kt deleted file mode 100644 index ea1b1f03..00000000 --- a/plugins/base/src/main/kotlin/transformers/pages/merger/PageMergerStrategy.kt +++ /dev/null @@ -1,13 +0,0 @@ -/* - * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package org.jetbrains.dokka.base.transformers.pages.merger - -import org.jetbrains.dokka.pages.PageNode - -public fun interface PageMergerStrategy { - - public fun tryMerge(pages: List<PageNode>, path: List<String>): List<PageNode> - -} diff --git a/plugins/base/src/main/kotlin/transformers/pages/merger/SameMethodNamePageMergerStrategy.kt b/plugins/base/src/main/kotlin/transformers/pages/merger/SameMethodNamePageMergerStrategy.kt deleted file mode 100644 index 864545e6..00000000 --- a/plugins/base/src/main/kotlin/transformers/pages/merger/SameMethodNamePageMergerStrategy.kt +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package org.jetbrains.dokka.base.transformers.pages.merger - -import org.jetbrains.dokka.base.renderers.sourceSets -import org.jetbrains.dokka.base.transformers.documentables.isDeprecated -import org.jetbrains.dokka.model.DisplaySourceSet -import org.jetbrains.dokka.model.Documentable -import org.jetbrains.dokka.model.dfs -import org.jetbrains.dokka.model.properties.WithExtraProperties -import org.jetbrains.dokka.pages.* -import org.jetbrains.dokka.utilities.DokkaLogger - -/** - * Merges [MemberPage] elements that have the same name. - * That includes **both** properties and functions. - */ -public class SameMethodNamePageMergerStrategy( - public val logger: DokkaLogger -) : PageMergerStrategy { - override fun tryMerge(pages: List<PageNode>, path: List<String>): List<PageNode> { - val members = pages - .filterIsInstance<MemberPageNode>() - .takeIf { it.isNotEmpty() } - ?.sortedBy { it.containsDeprecatedDocumentables() } // non-deprecated first - ?: return pages - - val name = pages.first().name.also { - if (pages.any { page -> page.name != it }) { // Is this even possible? - logger.error("Page names for $it do not match!") - } - } - val dri = members.flatMap { it.dri }.toSet() - - - val merged = MemberPageNode( - dri = dri, - name = name, - children = members.flatMap { it.children }.distinct(), - content = squashDivergentInstances(members).withSourceSets(members.allSourceSets()), - embeddedResources = members.flatMap { it.embeddedResources }.distinct(), - documentables = members.flatMap { it.documentables } - ) - - return (pages - members) + listOf(merged) - } - - @Suppress("UNCHECKED_CAST") - private fun MemberPageNode.containsDeprecatedDocumentables() = - this.documentables.any { (it as? WithExtraProperties<Documentable>)?.isDeprecated() == true } - - private fun List<MemberPageNode>.allSourceSets(): Set<DisplaySourceSet> = - fold(emptySet()) { acc, e -> acc + e.sourceSets() } - - private fun squashDivergentInstances(nodes: List<MemberPageNode>): ContentNode = - nodes.map { it.content } - .reduce { acc, node -> - acc.mapTransform<ContentDivergentGroup, ContentNode> { g -> - g.copy(children = (g.children + - ((node.dfs { it is ContentDivergentGroup && it.groupID == g.groupID } as? ContentDivergentGroup) - ?.children ?: emptyList()) - ) - ) - } - } -} diff --git a/plugins/base/src/main/kotlin/transformers/pages/merger/SourceSetMergingPageTransformer.kt b/plugins/base/src/main/kotlin/transformers/pages/merger/SourceSetMergingPageTransformer.kt deleted file mode 100644 index 8d52a39d..00000000 --- a/plugins/base/src/main/kotlin/transformers/pages/merger/SourceSetMergingPageTransformer.kt +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package org.jetbrains.dokka.base.transformers.pages.merger - -import org.jetbrains.dokka.Platform -import org.jetbrains.dokka.model.DisplaySourceSet -import org.jetbrains.dokka.model.toDisplaySourceSets -import org.jetbrains.dokka.pages.ContentComposite -import org.jetbrains.dokka.pages.ContentNode -import org.jetbrains.dokka.pages.RootPageNode -import org.jetbrains.dokka.plugability.DokkaContext -import org.jetbrains.dokka.transformers.pages.PageTransformer - -public class SourceSetMergingPageTransformer(context: DokkaContext) : PageTransformer { - - private val mergedSourceSets = context.configuration.sourceSets.toDisplaySourceSets() - .associateBy { sourceSet -> sourceSet.key } - - override fun invoke(input: RootPageNode): RootPageNode { - return input.transformContentPagesTree { contentPage -> - val content: ContentNode = contentPage.content - contentPage.modified(content = transformWithMergedSourceSets(content)) - } - } - - private fun transformWithMergedSourceSets( - contentNode: ContentNode - ): ContentNode { - val mergedSourceSets = contentNode.sourceSets.map { mergedSourceSets.getValue(it.key) }.toSet() - return when (contentNode) { - is ContentComposite -> contentNode - .transformChildren(::transformWithMergedSourceSets) - .withSourceSets(mergedSourceSets) - else -> contentNode.withSourceSets(mergedSourceSets.toSet()) - } - } -} - -private val DisplaySourceSet.key get() = SourceSetMergingKey(name, platform) - -private data class SourceSetMergingKey(private val displayName: String, private val platform: Platform) diff --git a/plugins/base/src/main/kotlin/transformers/pages/sourcelinks/SourceLinksTransformer.kt b/plugins/base/src/main/kotlin/transformers/pages/sourcelinks/SourceLinksTransformer.kt deleted file mode 100644 index 80eeca7e..00000000 --- a/plugins/base/src/main/kotlin/transformers/pages/sourcelinks/SourceLinksTransformer.kt +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package org.jetbrains.dokka.base.transformers.pages.sourcelinks - -import org.jetbrains.dokka.DokkaConfiguration -import org.jetbrains.dokka.DokkaConfiguration.DokkaSourceSet -import org.jetbrains.dokka.base.DokkaBase -import org.jetbrains.dokka.base.translators.documentables.PageContentBuilder -import org.jetbrains.dokka.links.DRI -import org.jetbrains.dokka.model.* -import org.jetbrains.dokka.pages.* -import org.jetbrains.dokka.plugability.DokkaContext -import org.jetbrains.dokka.plugability.plugin -import org.jetbrains.dokka.plugability.querySingle -import org.jetbrains.dokka.transformers.pages.PageTransformer -import java.io.File - -public class SourceLinksTransformer( - public val context: DokkaContext -) : PageTransformer { - - private val builder : PageContentBuilder = PageContentBuilder( - context.plugin<DokkaBase>().querySingle { commentsToContentConverter }, - context.plugin<DokkaBase>().querySingle { signatureProvider }, - context.logger - ) - - override fun invoke(input: RootPageNode): RootPageNode { - val sourceLinks = getSourceLinksFromConfiguration() - if (sourceLinks.isEmpty()) { - return input - } - return input.transformContentPagesTree { node -> - when (node) { - is WithDocumentables -> { - val sources = node.documentables - .filterIsInstance<WithSources>() - .fold(mutableMapOf<DRI, List<Pair<DokkaSourceSet, String>>>()) { acc, documentable -> - val dri = (documentable as Documentable).dri - acc.compute(dri) { _, v -> - val sources = resolveSources(sourceLinks, documentable) - v?.plus(sources) ?: sources - } - acc - } - if (sources.isNotEmpty()) - node.modified(content = transformContent(node.content, sources)) - else - node - } - else -> node - } - } - } - - private fun getSourceLinksFromConfiguration(): List<SourceLink> { - return context.configuration.sourceSets - .flatMap { it.sourceLinks.map { sl -> SourceLink(sl, it) } } - } - - private fun resolveSources( - sourceLinks: List<SourceLink>, documentable: WithSources - ): List<Pair<DokkaSourceSet, String>> { - return documentable.sources.mapNotNull { (sourceSet, documentableSource) -> - val sourceLink = sourceLinks.find { sourceLink -> - File(documentableSource.path).startsWith(sourceLink.path) && sourceLink.sourceSetData == sourceSet - } ?: return@mapNotNull null - - sourceSet to documentableSource.toLink(sourceLink) - } - } - - private fun DocumentableSource.toLink(sourceLink: SourceLink): String { - val sourcePath = File(this.path).invariantSeparatorsPath - val sourceLinkPath = File(sourceLink.path).invariantSeparatorsPath - - val lineNumber = this.computeLineNumber() - return sourceLink.url + - sourcePath.split(sourceLinkPath)[1] + - sourceLink.lineSuffix + - "${lineNumber ?: 1}" - } - - private fun ContentNode.signatureGroupOrNull() = - (this as? ContentGroup)?.takeIf { it.dci.kind == ContentKind.Symbol } - - private fun transformContent( - contentNode: ContentNode, sources: Map<DRI, List<Pair<DokkaSourceSet, String>>> - ): ContentNode = - contentNode.signatureGroupOrNull()?.let { sg -> - val sgIds = sg.sourceSets.computeSourceSetIds() - sources[sg.dci.dri.singleOrNull()]?.let { sourceLinks -> - sourceLinks - .filter { it.first.sourceSetID in sgIds } - .takeIf { it.isNotEmpty() } - ?.let { filteredSourcesLinks -> - sg.copy(children = sg.children + filteredSourcesLinks.map { - buildContentLink( - sg.dci.dri.first(), - it.first, - it.second - ) - }) - } - } - } ?: when (contentNode) { - is ContentComposite -> contentNode.transformChildren { transformContent(it, sources) } - else -> contentNode - } - - private fun buildContentLink(dri: DRI, sourceSet: DokkaSourceSet, link: String) = builder.contentFor( - dri, - setOf(sourceSet), - ContentKind.Source, - setOf(TextStyle.FloatingRight) - ) { - text("(") - link("source", link) - text(")") - } -} - -public data class SourceLink( - val path: String, - val url: String, - val lineSuffix: String?, - val sourceSetData: DokkaSourceSet -) { - public constructor( - sourceLinkDefinition: DokkaConfiguration.SourceLinkDefinition, - sourceSetData: DokkaSourceSet - ) : this( - sourceLinkDefinition.localDirectory, - sourceLinkDefinition.remoteUrl.toExternalForm(), - sourceLinkDefinition.remoteLineSuffix, - sourceSetData - ) -} diff --git a/plugins/base/src/main/kotlin/transformers/pages/tags/CustomTagContentProvider.kt b/plugins/base/src/main/kotlin/transformers/pages/tags/CustomTagContentProvider.kt deleted file mode 100644 index fcec234f..00000000 --- a/plugins/base/src/main/kotlin/transformers/pages/tags/CustomTagContentProvider.kt +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package org.jetbrains.dokka.base.transformers.pages.tags - -import org.jetbrains.dokka.DokkaConfiguration.DokkaSourceSet -import org.jetbrains.dokka.base.translators.documentables.PageContentBuilder.DocumentableContentBuilder -import org.jetbrains.dokka.model.doc.CustomTagWrapper -import org.jetbrains.dokka.model.doc.DocTag - -/** - * Provides an ability to render custom doc tags - * - * Custom tags can be generated during build, for instance via transformers from converting an annotation - * (such as in [org.jetbrains.dokka.base.transformers.pages.annotations.SinceKotlinTransformer]) - * - * Also, custom tags can come from the kdoc itself, where "custom" is defined as unknown to the compiler/spec. - * `@property` and `@throws` are not custom tags - they are defined by the spec and have special meaning - * and separate blocks on the documentation page, it's clear how to render it. Whereas `@usesMathJax` is - * a custom tag - it's application/plugin specific and is not handled by dokka by default. - * - * Using this provider, we can map custom tags (such as `@usesMathJax`) and generate content for it that - * will be displayed on the pages. - */ -public interface CustomTagContentProvider { - - /** - * Whether this content provider supports given [CustomTagWrapper]. - * - * Tags can be filtered out either by name or by nested [DocTag] type - */ - public fun isApplicable(customTag: CustomTagWrapper): Boolean - - /** - * Full blown content description, most likely to be on a separate page - * dedicated to just one element (i.e one class/function), so any - * amount of detail should be fine. - */ - public fun DocumentableContentBuilder.contentForDescription( - sourceSet: DokkaSourceSet, - customTag: CustomTagWrapper - ) {} - - /** - * Brief comment section, usually displayed as a summary/preview. - * - * For instance, when listing all functions of a class on one page, - * it'll be too much to display complete documentation for each function. - * Instead, a small brief is shown for each one (i.e the first paragraph - * or some other important information) - the user can go to the dedicated - * page for more details if they find the brief interesting. - * - * Tag-wise, it would make sense to include `Since Kotlin`, since it's - * important information for the users of stdlib. It would make little - * sense to include `@usesMathjax` here, as this information seems - * to be more specific and detailed than is needed for a brief. - */ - public fun DocumentableContentBuilder.contentForBrief( - sourceSet: DokkaSourceSet, - customTag: CustomTagWrapper - ) {} -} diff --git a/plugins/base/src/main/kotlin/transformers/pages/tags/SinceKotlinTagContentProvider.kt b/plugins/base/src/main/kotlin/transformers/pages/tags/SinceKotlinTagContentProvider.kt deleted file mode 100644 index 7c35f719..00000000 --- a/plugins/base/src/main/kotlin/transformers/pages/tags/SinceKotlinTagContentProvider.kt +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package org.jetbrains.dokka.base.transformers.pages.tags - -import org.jetbrains.dokka.DokkaConfiguration -import org.jetbrains.dokka.base.translators.documentables.KDOC_TAG_HEADER_LEVEL -import org.jetbrains.dokka.base.translators.documentables.PageContentBuilder.DocumentableContentBuilder -import org.jetbrains.dokka.model.doc.CustomTagWrapper -import org.jetbrains.dokka.pages.TextStyle - -public object SinceKotlinTagContentProvider : CustomTagContentProvider { - - private const val SINCE_KOTLIN_TAG_NAME = "Since Kotlin" - - override fun isApplicable(customTag: CustomTagWrapper): Boolean = customTag.name == SINCE_KOTLIN_TAG_NAME - - override fun DocumentableContentBuilder.contentForDescription( - sourceSet: DokkaConfiguration.DokkaSourceSet, - customTag: CustomTagWrapper - ) { - group(sourceSets = setOf(sourceSet), styles = emptySet()) { - header(KDOC_TAG_HEADER_LEVEL, customTag.name) - comment(customTag.root) - } - } - - override fun DocumentableContentBuilder.contentForBrief( - sourceSet: DokkaConfiguration.DokkaSourceSet, - customTag: CustomTagWrapper - ) { - group(sourceSets = setOf(sourceSet), styles = setOf(TextStyle.InlineComment)) { - text(customTag.name + " ", styles = setOf(TextStyle.Bold)) - comment(customTag.root, styles = emptySet()) - } - } -} |