From 46bfb60f356a861097149f64bf414e732807c522 Mon Sep 17 00:00:00 2001 From: Filip ZybaƂa Date: Wed, 8 Jul 2020 10:11:53 +0200 Subject: Implemented extension point for tab sorting strategy --- plugins/base/src/main/kotlin/DokkaBase.kt | 6 +++++ .../kotlin/renderers/DefaultTabSortingStrategy.kt | 29 ++++++++++++++++++++++ .../main/kotlin/renderers/TabSortingStrategy.kt | 7 ++++++ .../src/main/kotlin/renderers/html/HtmlRenderer.kt | 14 +++++++++-- .../pages/sourcelinks/SourceLinksTransformer.kt | 2 +- .../documentables/DefaultPageCreator.kt | 8 +++--- .../documentables/PageContentBuilder.kt | 4 +-- 7 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 plugins/base/src/main/kotlin/renderers/DefaultTabSortingStrategy.kt create mode 100644 plugins/base/src/main/kotlin/renderers/TabSortingStrategy.kt (limited to 'plugins/base/src/main/kotlin') diff --git a/plugins/base/src/main/kotlin/DokkaBase.kt b/plugins/base/src/main/kotlin/DokkaBase.kt index bcd2c93e..6586f2dc 100644 --- a/plugins/base/src/main/kotlin/DokkaBase.kt +++ b/plugins/base/src/main/kotlin/DokkaBase.kt @@ -41,6 +41,7 @@ class DokkaBase : DokkaPlugin() { val outputWriter by extensionPoint() val htmlPreprocessors by extensionPoint() val kotlinAnalysis by extensionPoint() + val tabSortingStrategy by extensionPoint() val descriptorToDocumentableTranslator by extending { @@ -134,10 +135,15 @@ class DokkaBase : DokkaPlugin() { } } + val defaultTabSortingStrategy by extending { + tabSortingStrategy with DefaultTabSortingStrategy() + } + val htmlRenderer by extending { CoreExtensions.renderer providing ::HtmlRenderer } + val defaultKotlinAnalysis by extending { kotlinAnalysis providing { ctx -> KotlinAnalysis(ctx) } } diff --git a/plugins/base/src/main/kotlin/renderers/DefaultTabSortingStrategy.kt b/plugins/base/src/main/kotlin/renderers/DefaultTabSortingStrategy.kt new file mode 100644 index 00000000..b40f9afe --- /dev/null +++ b/plugins/base/src/main/kotlin/renderers/DefaultTabSortingStrategy.kt @@ -0,0 +1,29 @@ +package org.jetbrains.dokka.base.renderers + +import org.jetbrains.dokka.pages.ContentKind +import org.jetbrains.dokka.pages.ContentNode +import org.jetbrains.dokka.pages.Kind +import org.jetbrains.dokka.utilities.DokkaLogger + +class DefaultTabSortingStrategy : TabSortingStrategy { + override fun sort(tabs: Collection): List { + val tabMap: Map> = mapOf( + ContentKind.Classlikes to mutableListOf(), + ContentKind.Constructors to mutableListOf(), + ContentKind.Functions to mutableListOf(), + ContentKind.Properties to mutableListOf(), + ContentKind.Extensions to mutableListOf(), + ContentKind.Parameters to mutableListOf(), + ContentKind.Inheritors to mutableListOf(), + ContentKind.Source to mutableListOf(), + ContentKind.Sample to mutableListOf(), + ContentKind.Comment to mutableListOf() + ) + val unrecognized: MutableList = mutableListOf() + tabs.forEach { + tabMap[it.dci.kind]?.add(it) ?: unrecognized.add(it) + } + return tabMap.values.flatten() + unrecognized + } + +} \ No newline at end of file diff --git a/plugins/base/src/main/kotlin/renderers/TabSortingStrategy.kt b/plugins/base/src/main/kotlin/renderers/TabSortingStrategy.kt new file mode 100644 index 00000000..dcf49ca9 --- /dev/null +++ b/plugins/base/src/main/kotlin/renderers/TabSortingStrategy.kt @@ -0,0 +1,7 @@ +package org.jetbrains.dokka.base.renderers + +import org.jetbrains.dokka.pages.ContentNode + +interface TabSortingStrategy { + fun sort(tabs: Collection) : List +} \ No newline at end of file diff --git a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt index 595dc8d2..50af53f0 100644 --- a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt +++ b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt @@ -9,12 +9,14 @@ import org.jetbrains.dokka.DokkaConfiguration.DokkaSourceSet import org.jetbrains.dokka.Platform import org.jetbrains.dokka.base.DokkaBase import org.jetbrains.dokka.base.renderers.DefaultRenderer +import org.jetbrains.dokka.base.renderers.TabSortingStrategy import org.jetbrains.dokka.links.DRI 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.query +import org.jetbrains.dokka.plugability.querySingle import java.io.File import java.net.URI @@ -26,11 +28,19 @@ open class HtmlRenderer( sourceSet to context.configuration.sourceSets.filter { sourceSet.dependentSourceSets.contains(it.sourceSetID) } }.toMap() - private val pageList = mutableMapOf>() override val preprocessors = context.plugin().query { htmlPreprocessors } + private val tabSortingStrategy = context.plugin().querySingle { tabSortingStrategy } + + private fun sortTabs(strategy: TabSortingStrategy, tabs: Collection) : List { + val sorted = strategy.sort(tabs) + if(sorted.size != tabs.size) + context.logger.warn("Tab sorting strategy has changed number of tabs from ${tabs.size} to ${sorted.size}") + return sorted; + } + override fun FlowContent.wrapGroup( node: ContentGroup, pageContext: ContentPage, @@ -44,7 +54,7 @@ open class HtmlRenderer( val firstLevel = node.children.filterIsInstance().flatMap { it.children } .filterIsInstance() - val renderable = firstLevel.union(secondLevel) + val renderable = firstLevel.union(secondLevel).let { sortTabs(tabSortingStrategy, it) } div(classes = "tabs-section") { attributes["tabs-section"] = "tabs-section" diff --git a/plugins/base/src/main/kotlin/transformers/pages/sourcelinks/SourceLinksTransformer.kt b/plugins/base/src/main/kotlin/transformers/pages/sourcelinks/SourceLinksTransformer.kt index 9a11c126..f0e62f11 100644 --- a/plugins/base/src/main/kotlin/transformers/pages/sourcelinks/SourceLinksTransformer.kt +++ b/plugins/base/src/main/kotlin/transformers/pages/sourcelinks/SourceLinksTransformer.kt @@ -57,7 +57,7 @@ class SourceLinksTransformer(val context: DokkaContext, val builder: PageContent node.dri.first(), node.documentable!!.sourceSets.toSet() ) { - header(2, "Sources") + header(2, "Sources", kind = ContentKind.Source) +ContentTable( emptyList(), sources.map { diff --git a/plugins/base/src/main/kotlin/translators/documentables/DefaultPageCreator.kt b/plugins/base/src/main/kotlin/translators/documentables/DefaultPageCreator.kt index 40283d82..f8dbde60 100644 --- a/plugins/base/src/main/kotlin/translators/documentables/DefaultPageCreator.kt +++ b/plugins/base/src/main/kotlin/translators/documentables/DefaultPageCreator.kt @@ -315,7 +315,7 @@ open class DefaultPageCreator( fun DocumentableContentBuilder.contentForParams() { if (tags.isNotEmptyForTag()) { - header(2, "Parameters") + header(2, "Parameters", kind = ContentKind.Parameters) group( extra = mainExtra + SimpleAttr.header("Parameters"), styles = setOf(ContentStyle.WithExtraAttributes) @@ -356,7 +356,7 @@ open class DefaultPageCreator( fun DocumentableContentBuilder.contentForSeeAlso() { if (tags.isNotEmptyForTag()) { - header(2, "See also") + header(2, "See also", kind = ContentKind.Comment) group( extra = mainExtra + SimpleAttr.header("See also"), styles = setOf(ContentStyle.WithExtraAttributes) @@ -393,7 +393,7 @@ open class DefaultPageCreator( fun DocumentableContentBuilder.contentForSamples() { val samples = tags.withTypeNamed() if (samples.isNotEmpty()) { - header(2, "Samples") + header(2, "Samples", kind = ContentKind.Sample) group( extra = mainExtra + SimpleAttr.header("Samples"), styles = emptySet() @@ -476,7 +476,7 @@ open class DefaultPageCreator( extra: PropertyContainer = mainExtra ) { if (collection.any()) { - header(2, name) + header(2, name, kind = kind) table(kind, extra = extra, styles = emptySet()) { collection .groupBy { it.name } diff --git a/plugins/base/src/main/kotlin/translators/documentables/PageContentBuilder.kt b/plugins/base/src/main/kotlin/translators/documentables/PageContentBuilder.kt index 94aa2c5a..b7927076 100644 --- a/plugins/base/src/main/kotlin/translators/documentables/PageContentBuilder.kt +++ b/plugins/base/src/main/kotlin/translators/documentables/PageContentBuilder.kt @@ -114,7 +114,7 @@ open class PageContentBuilder( styles, extra + SimpleAttr("anchor", text.replace("\\s".toRegex(), "").toLowerCase()) ) { - text(text) + text(text, kind = kind) block() } ) @@ -172,7 +172,7 @@ open class PageContentBuilder( operation: DocumentableContentBuilder.(T) -> Unit ) { if (renderWhenEmpty || elements.any()) { - header(level, name) { } + header(level, name, kind = kind) { } contents += ContentTable( headers ?: defaultHeaders, elements -- cgit