diff options
Diffstat (limited to 'core')
7 files changed, 66 insertions, 44 deletions
diff --git a/core/src/main/kotlin/CoreExtensions.kt b/core/src/main/kotlin/CoreExtensions.kt index 8c66b41e..18cde210 100644 --- a/core/src/main/kotlin/CoreExtensions.kt +++ b/core/src/main/kotlin/CoreExtensions.kt @@ -9,6 +9,7 @@ import org.jetbrains.dokka.transformers.descriptors.DescriptorToDocumentationTra import org.jetbrains.dokka.transformers.documentation.DocumentationNodeMerger import org.jetbrains.dokka.transformers.documentation.DocumentationNodeTransformer import org.jetbrains.dokka.transformers.documentation.DocumentationToPageTranslator +import org.jetbrains.dokka.transformers.pages.PageMergerStrategy import org.jetbrains.dokka.transformers.pages.PageNodeTransformer import org.jetbrains.dokka.transformers.psi.PsiToDocumentationTranslator import kotlin.reflect.KProperty @@ -29,6 +30,8 @@ object CoreExtensions { val locationProviderFactory by coreExtension<LocationProviderFactory>() val outputWriter by coreExtension<OutputWriter>() val renderer by coreExtension<Renderer>() + val fileExtension by coreExtension<String>() + val pageMergerStrategy by coreExtension<PageMergerStrategy>() private fun <T: Any> coreExtension() = object { operator fun provideDelegate(thisRef: CoreExtensions, property: KProperty<*>): Lazy<ExtensionPoint<T>> = diff --git a/core/src/main/kotlin/plugability/DefaultExtensions.kt b/core/src/main/kotlin/plugability/DefaultExtensions.kt index b7ab1af1..3f630ee3 100644 --- a/core/src/main/kotlin/plugability/DefaultExtensions.kt +++ b/core/src/main/kotlin/plugability/DefaultExtensions.kt @@ -9,6 +9,7 @@ import org.jetbrains.dokka.resolvers.DefaultLocationProviderFactory import org.jetbrains.dokka.transformers.descriptors.DefaultDescriptorToDocumentationTranslator import org.jetbrains.dokka.transformers.documentation.DefaultDocumentationNodeMerger import org.jetbrains.dokka.transformers.documentation.DefaultDocumentationToPageTranslator +import org.jetbrains.dokka.transformers.pages.DefaultPageMergerStrategy import org.jetbrains.dokka.transformers.psi.DefaultPsiToDocumentationTranslator import org.jetbrains.dokka.transformers.pages.DefaultPageNodeMerger @@ -32,10 +33,11 @@ internal object DefaultExtensions { CoreExtensions.documentationMerger -> DefaultDocumentationNodeMerger CoreExtensions.commentsToContentConverter -> converter.get(fullContext) CoreExtensions.documentationToPageTranslator -> DefaultDocumentationToPageTranslator - CoreExtensions.pageTransformer -> DefaultPageNodeMerger + CoreExtensions.pageTransformer -> DefaultPageNodeMerger(fullContext) CoreExtensions.renderer -> renderer.get(fullContext) CoreExtensions.locationProviderFactory -> providerFactory.get(fullContext) CoreExtensions.outputWriter -> outputWriter.get(fullContext) + CoreExtensions.pageMergerStrategy -> DefaultPageMergerStrategy else -> null }.let { listOfNotNull( it ) as List<T> } }
\ No newline at end of file diff --git a/core/src/main/kotlin/renderers/html/htmlPreprocessors.kt b/core/src/main/kotlin/renderers/html/htmlPreprocessors.kt index 4ee67448..7ce6d298 100644 --- a/core/src/main/kotlin/renderers/html/htmlPreprocessors.kt +++ b/core/src/main/kotlin/renderers/html/htmlPreprocessors.kt @@ -8,12 +8,12 @@ import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.renderers.platforms import org.jetbrains.dokka.transformers.pages.PageNodeTransformer -object RootCreator : PageNodeTransformer { +object RootCreator : PageNodeTransformer() { override fun invoke(input: RootPageNode) = RendererSpecificRootPage("", listOf(input), RenderingStrategy.DoNothing) } -object SearchPageInstaller : PageNodeTransformer { +object SearchPageInstaller : PageNodeTransformer() { override fun invoke(input: RootPageNode) = input.modified(children = input.children + searchPage) private val searchPage = RendererSpecificResourcePage( @@ -34,7 +34,7 @@ object SearchPageInstaller : PageNodeTransformer { }) } -object NavigationPageInstaller : PageNodeTransformer { +object NavigationPageInstaller : PageNodeTransformer() { override fun invoke(input: RootPageNode) = input.modified( children = input.children + NavigationPage( input.children.filterIsInstance<ContentPage>().single().let(::visit) @@ -48,7 +48,7 @@ object NavigationPageInstaller : PageNodeTransformer { page.children.filterIsInstance<ContentPage>().map { visit(it) }) } -object ResourceInstaller : PageNodeTransformer { +object ResourceInstaller : PageNodeTransformer() { override fun invoke(input: RootPageNode) = input.modified(children = input.children + resourcePages) private val resourcePages = listOf("styles", "scripts", "images").map { @@ -56,7 +56,7 @@ object ResourceInstaller : PageNodeTransformer { } } -object StyleAndScriptsAppender : PageNodeTransformer { +object StyleAndScriptsAppender : PageNodeTransformer() { override fun invoke(input: RootPageNode) = input.transformContentPagesTree { it.modified( embeddedResources = it.embeddedResources + listOf( diff --git a/core/src/main/kotlin/transformers/pages/DefaultPageMergerStrategy.kt b/core/src/main/kotlin/transformers/pages/DefaultPageMergerStrategy.kt new file mode 100644 index 00000000..81da71ce --- /dev/null +++ b/core/src/main/kotlin/transformers/pages/DefaultPageMergerStrategy.kt @@ -0,0 +1,30 @@ +package org.jetbrains.dokka.transformers.pages + +import org.jetbrains.dokka.pages.* + +object DefaultPageMergerStrategy : PageMergerStrategy() { + override fun tryMerge(pages: List<PageNode>): List<PageNode> = listOf(pages.mergePageNodes()) + + private fun List<PageNode>.mergePageNodes(): ContentPage { + val name = first().name + val resChildren = this.flatMap { it.children }.distinct() + val contentChildren = this.filterIsInstance<ContentPage>() + val dri = contentChildren.flatMap { it.dri }.toSet() + val dci = DCI( + dri = dri, + kind = contentChildren.first().content.dci.kind + ) + return contentChildren.first() + .modified( + dri = dri, + name = name, + children = resChildren, + content = asGroup(dci, contentChildren.map { it.content }) + ) + } + + fun asGroup(dci: DCI, nodes: List<ContentNode>): ContentGroup { + val n = nodes.first() + return ContentGroup(nodes, dci, n.platforms, n.style, n.extras) + } +}
\ No newline at end of file diff --git a/core/src/main/kotlin/transformers/pages/DefaultPageNodeMerger.kt b/core/src/main/kotlin/transformers/pages/DefaultPageNodeMerger.kt index 7cd4aba8..72e491cf 100644 --- a/core/src/main/kotlin/transformers/pages/DefaultPageNodeMerger.kt +++ b/core/src/main/kotlin/transformers/pages/DefaultPageNodeMerger.kt @@ -1,45 +1,23 @@ package org.jetbrains.dokka.transformers.pages -import org.jetbrains.dokka.pages.* +import org.jetbrains.dokka.CoreExtensions +import org.jetbrains.dokka.pages.PageNode +import org.jetbrains.dokka.pages.RootPageNode +import org.jetbrains.dokka.plugability.DokkaContext -object DefaultPageNodeMerger : PageNodeTransformer { +class DefaultPageNodeMerger(val context: DokkaContext) : PageNodeTransformer(context) { override fun invoke(input: RootPageNode): RootPageNode = input.modified(children = input.children.map { it.mergeChildren() }) - fun asGroup(dci: DCI, nodes: List<ContentNode>): ContentGroup { - val n = nodes.first() - return ContentGroup(nodes, dci, n.platforms, n.style, n.extras) - } + fun PageNode.mergeChildren(): PageNode = children.groupBy { it.name } + .map { (_, v) -> v.mergePageNodes() } + .let { pages -> modified(children = pages.map { it.first().mergeChildren() }) } - fun PageNode.mergeChildren(): PageNode = if (children.isNotEmpty()) { - children.groupBy { it.name } - .map { (k, v) -> - if (v.size > 1) { - v.mergePageNodes(k) - } else v.first() - } - .map { it.mergeChildren() } - .let { - modified(children = it) - } - } else this + private fun List<PageNode>.mergePageNodes(): List<PageNode> = + context[CoreExtensions.pageMergerStrategy].fold(this) { pages, strategy -> tryMerge(strategy, pages) } - private fun List<PageNode>.mergePageNodes( - k: String - ): ContentPage { - val resChildren = this.flatMap { it.children }.distinct() - val contentChildren = this.filterIsInstance<ContentPage>() - val dri = contentChildren.flatMap { it.dri }.toSet() - val dci = DCI( - dri = dri, - kind = contentChildren.first().content.dci.kind - ) - return contentChildren.first() - .modified( - dri = dri, - name = k, - children = resChildren, - content = asGroup(dci, contentChildren.map { it.content }) - ) - } + private fun tryMerge(strategy: PageMergerStrategy, pages: List<PageNode>) = if (pages.size > 1) + strategy.tryMerge(pages) + else + pages }
\ No newline at end of file diff --git a/core/src/main/kotlin/transformers/pages/PageMergerStrategy.kt b/core/src/main/kotlin/transformers/pages/PageMergerStrategy.kt new file mode 100644 index 00000000..37dc5a6a --- /dev/null +++ b/core/src/main/kotlin/transformers/pages/PageMergerStrategy.kt @@ -0,0 +1,9 @@ +package org.jetbrains.dokka.transformers.pages + +import org.jetbrains.dokka.pages.PageNode + +abstract class PageMergerStrategy { + + abstract fun tryMerge(pages: List<PageNode>): List<PageNode> + +}
\ No newline at end of file diff --git a/core/src/main/kotlin/transformers/pages/PageNodeTransformer.kt b/core/src/main/kotlin/transformers/pages/PageNodeTransformer.kt index 45357060..ebf53d30 100644 --- a/core/src/main/kotlin/transformers/pages/PageNodeTransformer.kt +++ b/core/src/main/kotlin/transformers/pages/PageNodeTransformer.kt @@ -4,6 +4,6 @@ import org.jetbrains.dokka.pages.ModulePageNode import org.jetbrains.dokka.pages.RootPageNode import org.jetbrains.dokka.plugability.DokkaContext -interface PageNodeTransformer { - operator fun invoke(input: RootPageNode): RootPageNode +abstract class PageNodeTransformer(context: DokkaContext? = null) { + abstract operator fun invoke(input: RootPageNode): RootPageNode }
\ No newline at end of file |