From fee86d79a3aa7357c68c2f8f519d187f43447e8c Mon Sep 17 00:00:00 2001 From: Andrzej Ratajczak Date: Fri, 3 Apr 2020 13:57:33 +0200 Subject: Refactor package list to be a preprocessor --- plugins/base/src/main/kotlin/DokkaBase.kt | 14 ++++-- .../src/main/kotlin/renderers/DefaultRenderer.kt | 19 -------- .../main/kotlin/renderers/PackageListService.kt | 51 ++++++++++++++++++++++ .../kotlin/renderers/html/htmlPreprocessors.kt | 29 ++++++------ .../src/main/kotlin/renderers/preprocessors.kt | 27 ++++++++++++ .../test/kotlin/renderers/RenderingOnlyTestBase.kt | 2 +- .../annotatedFunction/out/html/root/package-list | 5 +++ .../out/html/root/package-list | 8 ++++ .../expect/function/out/html/root/package-list | 5 +++ .../out/html/root/package-list | 8 ++++ .../out/html/root/package-list | 5 +++ .../out/html/root/package-list | 5 +++ .../out/html/root/package-list | 5 +++ .../functionWithParams/out/html/root/package-list | 5 +++ .../out/html/root/package-list | 6 +++ .../genericFunction/out/html/root/package-list | 5 +++ .../out/html/root/package-list | 5 +++ .../inlineFunction/out/html/root/package-list | 5 +++ .../out/html/root/package-list | 5 +++ .../signatureTest/out/html/root/package-list | 5 +++ .../expect/sinceKotlin/out/html/root/package-list | 5 +++ .../suspendFunction/out/html/root/package-list | 5 +++ .../out/html/root/package-list | 5 +++ 23 files changed, 196 insertions(+), 38 deletions(-) create mode 100644 plugins/base/src/main/kotlin/renderers/PackageListService.kt create mode 100644 plugins/base/src/main/kotlin/renderers/preprocessors.kt create mode 100644 plugins/base/src/test/resources/expect/annotatedFunction/out/html/root/package-list create mode 100644 plugins/base/src/test/resources/expect/annotatedFunctionWithAnnotationParameters/out/html/root/package-list create mode 100644 plugins/base/src/test/resources/expect/function/out/html/root/package-list create mode 100644 plugins/base/src/test/resources/expect/functionWithAnnotatedParam/out/html/root/package-list create mode 100644 plugins/base/src/test/resources/expect/functionWithDefaultParameter/out/html/root/package-list create mode 100644 plugins/base/src/test/resources/expect/functionWithNoinlineParam/out/html/root/package-list create mode 100644 plugins/base/src/test/resources/expect/functionWithNotDocumentedAnnotation/out/html/root/package-list create mode 100644 plugins/base/src/test/resources/expect/functionWithParams/out/html/root/package-list create mode 100644 plugins/base/src/test/resources/expect/functionWithReceiver/out/html/root/package-list create mode 100644 plugins/base/src/test/resources/expect/genericFunction/out/html/root/package-list create mode 100644 plugins/base/src/test/resources/expect/genericFunctionWithConstraints/out/html/root/package-list create mode 100644 plugins/base/src/test/resources/expect/inlineFunction/out/html/root/package-list create mode 100644 plugins/base/src/test/resources/expect/inlineSuspendFunction/out/html/root/package-list create mode 100644 plugins/base/src/test/resources/expect/signatureTest/out/html/root/package-list create mode 100644 plugins/base/src/test/resources/expect/sinceKotlin/out/html/root/package-list create mode 100644 plugins/base/src/test/resources/expect/suspendFunction/out/html/root/package-list create mode 100644 plugins/base/src/test/resources/expect/suspendInlineFunction/out/html/root/package-list (limited to 'plugins/base') diff --git a/plugins/base/src/main/kotlin/DokkaBase.kt b/plugins/base/src/main/kotlin/DokkaBase.kt index e8614507..5a86780b 100644 --- a/plugins/base/src/main/kotlin/DokkaBase.kt +++ b/plugins/base/src/main/kotlin/DokkaBase.kt @@ -1,10 +1,8 @@ package org.jetbrains.dokka.base import org.jetbrains.dokka.CoreExtensions -import org.jetbrains.dokka.base.renderers.FileWriter -import org.jetbrains.dokka.base.renderers.OutputWriter +import org.jetbrains.dokka.base.renderers.* import org.jetbrains.dokka.base.renderers.html.* -import org.jetbrains.dokka.base.renderers.html.HtmlRenderer import org.jetbrains.dokka.base.signatures.KotlinSignatureProvider import org.jetbrains.dokka.base.signatures.SignatureProvider import org.jetbrains.dokka.base.resolvers.external.* @@ -154,4 +152,14 @@ class DokkaBase : DokkaPlugin() { val styleAndScriptsAppender by extending { htmlPreprocessors with StyleAndScriptsAppender order { after(rootCreator) } } + + val packageListCreator by extending { + htmlPreprocessors providing { + PackageListCreator( + it, + "html", + "html" + ) + } order { after(rootCreator) } + } } \ No newline at end of file diff --git a/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt b/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt index 0ff0511a..26c75dbf 100644 --- a/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt +++ b/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt @@ -114,31 +114,12 @@ abstract class DefaultRenderer( root.children.forEach { renderPages(it) } } - // reimplement this as preprocessor - open fun renderPackageList(root: ContentPage) = - getPackageNamesAndPlatforms(root) - .keys - .joinToString("\n") - .also { outputWriter.write("${root.name}/package-list", it, "") } - - open fun getPackageNamesAndPlatforms(root: PageNode): Map> = - root.children - .map(::getPackageNamesAndPlatforms) - .fold(emptyMap>()) { e, acc -> acc + e } + - if (root is PackagePageNode) { - mapOf(root.name to root.platforms()) - } else { - emptyMap() - } - override fun render(root: RootPageNode) { val newRoot = preprocessors.fold(root) { acc, t -> t(acc) } locationProvider = context.plugin().querySingle { locationProviderFactory }.getLocationProvider(newRoot) - root.children().forEach { renderPackageList(it) } - renderPages(newRoot) } } diff --git a/plugins/base/src/main/kotlin/renderers/PackageListService.kt b/plugins/base/src/main/kotlin/renderers/PackageListService.kt new file mode 100644 index 00000000..d4333200 --- /dev/null +++ b/plugins/base/src/main/kotlin/renderers/PackageListService.kt @@ -0,0 +1,51 @@ +package org.jetbrains.dokka.base.renderers + +import org.jetbrains.dokka.base.DokkaBase +import org.jetbrains.dokka.links.DRI +import org.jetbrains.dokka.links.parent +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.kotlin.utils.addToStdlib.safeAs + +class PackageListService(val context: DokkaContext) { + + fun formatPackageList(module: RootPageNode, format: String, linkExtension: String): String { + + val packages = mutableSetOf() + val nonStandardLocations = mutableMapOf() + + val locationProvider = + context.plugin().querySingle { locationProviderFactory }.getLocationProvider(module) + + fun visit(node: PageNode, parentDris: Set) { + + if (node is PackagePageNode) { + packages.add(node.name) + } + + val contentPage = node.safeAs() + contentPage?.dri?.forEach { + if (parentDris.isNotEmpty() && it.parent !in parentDris) { + nonStandardLocations[it.toString()] = locationProvider.resolve(node) + } + } + + node.children.forEach { visit(it, contentPage?.dri ?: setOf()) } + } + + visit(module, setOf()) + + return buildString { + appendln("\$dokka.format:${format}") + appendln("\$dokka.linkExtension:${linkExtension}") + nonStandardLocations.map { (signature, location) -> "\$dokka.location:$signature\u001f$location" } + .sorted().joinTo(this, separator = "\n", postfix = "\n") + + packages.sorted().joinTo(this, separator = "\n", postfix = "\n") + } + + } + +} \ No newline at end of file diff --git a/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt b/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt index a72c77ea..9fed74e2 100644 --- a/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt +++ b/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt @@ -8,10 +8,6 @@ import org.jetbrains.dokka.base.renderers.platforms import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.transformers.pages.PageTransformer -object RootCreator : PageTransformer { - override fun invoke(input: RootPageNode) = - RendererSpecificRootPage("", listOf(input), RenderingStrategy.DoNothing) -} object SearchPageInstaller : PageTransformer { override fun invoke(input: RootPageNode) = input.modified(children = input.children + searchPage) @@ -37,19 +33,22 @@ object SearchPageInstaller : PageTransformer { object NavigationPageInstaller : PageTransformer { override fun invoke(input: RootPageNode) = input.modified( children = input.children + NavigationPage( - input.children.filterIsInstance().single().let(::visit) + input.children.filterIsInstance().single() + .let(NavigationPageInstaller::visit) ) ) - private fun visit(page: ContentPage): NavigationNode = NavigationNode( - page.name, - page.dri.first(), - page.platforms(), - if (page !is ClasslikePageNode) - page.children.filterIsInstance().map { visit(it) } - else - emptyList() - ) + private fun visit(page: ContentPage): NavigationNode = + NavigationNode( + page.name, + page.dri.first(), + page.platforms(), + if (page !is ClasslikePageNode) + page.children.filterIsInstance() + .map { visit(it) } + else + emptyList() + ) } object ResourceInstaller : PageTransformer { @@ -69,4 +68,4 @@ object StyleAndScriptsAppender : PageTransformer { ) ) } -} +} \ No newline at end of file diff --git a/plugins/base/src/main/kotlin/renderers/preprocessors.kt b/plugins/base/src/main/kotlin/renderers/preprocessors.kt new file mode 100644 index 00000000..bf2a9eb4 --- /dev/null +++ b/plugins/base/src/main/kotlin/renderers/preprocessors.kt @@ -0,0 +1,27 @@ +package org.jetbrains.dokka.base.renderers + +import org.jetbrains.dokka.pages.* +import org.jetbrains.dokka.plugability.DokkaContext +import org.jetbrains.dokka.transformers.pages.PageTransformer + +object RootCreator : PageTransformer { + override fun invoke(input: RootPageNode) = + RendererSpecificRootPage("", listOf(input), RenderingStrategy.DoNothing) +} + + +class PackageListCreator(val context: DokkaContext, val format: String, val linkExtension: String) : PageTransformer { + override fun invoke(input: RootPageNode) = + input.modified(children = input.children.map { + it.takeUnless { it is ModulePageNode } + ?: it.modified(children = it.children + packageList(input)) // TODO packageList should take module as an input + }) + + + private fun packageList(pageNode: RootPageNode) = + RendererSpecificResourcePage( + "${pageNode.name}/package-list", + emptyList(), + RenderingStrategy.Write(PackageListService(context).formatPackageList(pageNode, format, linkExtension)) + ) +} diff --git a/plugins/base/src/test/kotlin/renderers/RenderingOnlyTestBase.kt b/plugins/base/src/test/kotlin/renderers/RenderingOnlyTestBase.kt index 5f8d1dc4..b9809ac1 100644 --- a/plugins/base/src/test/kotlin/renderers/RenderingOnlyTestBase.kt +++ b/plugins/base/src/test/kotlin/renderers/RenderingOnlyTestBase.kt @@ -2,7 +2,7 @@ package renderers import org.jetbrains.dokka.DokkaConfigurationImpl import org.jetbrains.dokka.base.DokkaBase -import org.jetbrains.dokka.base.renderers.html.RootCreator +import org.jetbrains.dokka.base.renderers.RootCreator import org.jetbrains.dokka.base.resolvers.external.DokkaExternalLocationProviderFactory import org.jetbrains.dokka.base.resolvers.external.JavadocExternalLocationProviderFactory import org.jetbrains.dokka.base.resolvers.local.DefaultLocationProviderFactory diff --git a/plugins/base/src/test/resources/expect/annotatedFunction/out/html/root/package-list b/plugins/base/src/test/resources/expect/annotatedFunction/out/html/root/package-list new file mode 100644 index 00000000..caf6273a --- /dev/null +++ b/plugins/base/src/test/resources/expect/annotatedFunction/out/html/root/package-list @@ -0,0 +1,5 @@ +$dokka.format:html +$dokka.linkExtension:html +$dokka.location://f/#//root//f.html + + diff --git a/plugins/base/src/test/resources/expect/annotatedFunctionWithAnnotationParameters/out/html/root/package-list b/plugins/base/src/test/resources/expect/annotatedFunctionWithAnnotationParameters/out/html/root/package-list new file mode 100644 index 00000000..d2e42c86 --- /dev/null +++ b/plugins/base/src/test/resources/expect/annotatedFunctionWithAnnotationParameters/out/html/root/package-list @@ -0,0 +1,8 @@ +$dokka.format:html +$dokka.linkExtension:html +$dokka.location://f/#//root//f.html +$dokka.location:/Fancy/equals/#kotlin.Any?//root//-fancy/equals.html +$dokka.location:/Fancy/hashCode/#//root//-fancy/hash-code.html +$dokka.location:/Fancy/toString/#//root//-fancy/to-string.html + + diff --git a/plugins/base/src/test/resources/expect/function/out/html/root/package-list b/plugins/base/src/test/resources/expect/function/out/html/root/package-list new file mode 100644 index 00000000..3a7c6807 --- /dev/null +++ b/plugins/base/src/test/resources/expect/function/out/html/root/package-list @@ -0,0 +1,5 @@ +$dokka.format:html +$dokka.linkExtension:html +$dokka.location://fn/#//root//fn.html + + diff --git a/plugins/base/src/test/resources/expect/functionWithAnnotatedParam/out/html/root/package-list b/plugins/base/src/test/resources/expect/functionWithAnnotatedParam/out/html/root/package-list new file mode 100644 index 00000000..0b48e7e0 --- /dev/null +++ b/plugins/base/src/test/resources/expect/functionWithAnnotatedParam/out/html/root/package-list @@ -0,0 +1,8 @@ +$dokka.format:html +$dokka.linkExtension:html +$dokka.location://function/#kotlin.Function0[kotlin.Unit]//root//function.html +$dokka.location:/Fancy/equals/#kotlin.Any?//root//-fancy/equals.html +$dokka.location:/Fancy/hashCode/#//root//-fancy/hash-code.html +$dokka.location:/Fancy/toString/#//root//-fancy/to-string.html + + diff --git a/plugins/base/src/test/resources/expect/functionWithDefaultParameter/out/html/root/package-list b/plugins/base/src/test/resources/expect/functionWithDefaultParameter/out/html/root/package-list new file mode 100644 index 00000000..c8e4e8e1 --- /dev/null +++ b/plugins/base/src/test/resources/expect/functionWithDefaultParameter/out/html/root/package-list @@ -0,0 +1,5 @@ +$dokka.format:html +$dokka.linkExtension:html +$dokka.location://f/#kotlin.String//root//f.html + + diff --git a/plugins/base/src/test/resources/expect/functionWithNoinlineParam/out/html/root/package-list b/plugins/base/src/test/resources/expect/functionWithNoinlineParam/out/html/root/package-list new file mode 100644 index 00000000..7de7dd3b --- /dev/null +++ b/plugins/base/src/test/resources/expect/functionWithNoinlineParam/out/html/root/package-list @@ -0,0 +1,5 @@ +$dokka.format:html +$dokka.linkExtension:html +$dokka.location://function/#kotlin.Function0[kotlin.Unit]//root//function.html + + diff --git a/plugins/base/src/test/resources/expect/functionWithNotDocumentedAnnotation/out/html/root/package-list b/plugins/base/src/test/resources/expect/functionWithNotDocumentedAnnotation/out/html/root/package-list new file mode 100644 index 00000000..caf6273a --- /dev/null +++ b/plugins/base/src/test/resources/expect/functionWithNotDocumentedAnnotation/out/html/root/package-list @@ -0,0 +1,5 @@ +$dokka.format:html +$dokka.linkExtension:html +$dokka.location://f/#//root//f.html + + diff --git a/plugins/base/src/test/resources/expect/functionWithParams/out/html/root/package-list b/plugins/base/src/test/resources/expect/functionWithParams/out/html/root/package-list new file mode 100644 index 00000000..8b5d43ee --- /dev/null +++ b/plugins/base/src/test/resources/expect/functionWithParams/out/html/root/package-list @@ -0,0 +1,5 @@ +$dokka.format:html +$dokka.linkExtension:html +$dokka.location://function/#kotlin.Int//root//function.html + + diff --git a/plugins/base/src/test/resources/expect/functionWithReceiver/out/html/root/package-list b/plugins/base/src/test/resources/expect/functionWithReceiver/out/html/root/package-list new file mode 100644 index 00000000..13dc2923 --- /dev/null +++ b/plugins/base/src/test/resources/expect/functionWithReceiver/out/html/root/package-list @@ -0,0 +1,6 @@ +$dokka.format:html +$dokka.linkExtension:html +$dokka.location://fn/kotlin.String#//root//fn.html +$dokka.location://fn/kotlin.String#kotlin.Int//root//fn.html + + diff --git a/plugins/base/src/test/resources/expect/genericFunction/out/html/root/package-list b/plugins/base/src/test/resources/expect/genericFunction/out/html/root/package-list new file mode 100644 index 00000000..30905d0f --- /dev/null +++ b/plugins/base/src/test/resources/expect/genericFunction/out/html/root/package-list @@ -0,0 +1,5 @@ +$dokka.format:html +$dokka.linkExtension:html +$dokka.location://generic/#//root//generic.html + + diff --git a/plugins/base/src/test/resources/expect/genericFunctionWithConstraints/out/html/root/package-list b/plugins/base/src/test/resources/expect/genericFunctionWithConstraints/out/html/root/package-list new file mode 100644 index 00000000..30905d0f --- /dev/null +++ b/plugins/base/src/test/resources/expect/genericFunctionWithConstraints/out/html/root/package-list @@ -0,0 +1,5 @@ +$dokka.format:html +$dokka.linkExtension:html +$dokka.location://generic/#//root//generic.html + + diff --git a/plugins/base/src/test/resources/expect/inlineFunction/out/html/root/package-list b/plugins/base/src/test/resources/expect/inlineFunction/out/html/root/package-list new file mode 100644 index 00000000..60993388 --- /dev/null +++ b/plugins/base/src/test/resources/expect/inlineFunction/out/html/root/package-list @@ -0,0 +1,5 @@ +$dokka.format:html +$dokka.linkExtension:html +$dokka.location://f/#kotlin.Function0[kotlin.String]//root//f.html + + diff --git a/plugins/base/src/test/resources/expect/inlineSuspendFunction/out/html/root/package-list b/plugins/base/src/test/resources/expect/inlineSuspendFunction/out/html/root/package-list new file mode 100644 index 00000000..60993388 --- /dev/null +++ b/plugins/base/src/test/resources/expect/inlineSuspendFunction/out/html/root/package-list @@ -0,0 +1,5 @@ +$dokka.format:html +$dokka.linkExtension:html +$dokka.location://f/#kotlin.Function0[kotlin.String]//root//f.html + + diff --git a/plugins/base/src/test/resources/expect/signatureTest/out/html/root/package-list b/plugins/base/src/test/resources/expect/signatureTest/out/html/root/package-list new file mode 100644 index 00000000..3c53d355 --- /dev/null +++ b/plugins/base/src/test/resources/expect/signatureTest/out/html/root/package-list @@ -0,0 +1,5 @@ +$dokka.format:html +$dokka.linkExtension:html + +signatureTest + diff --git a/plugins/base/src/test/resources/expect/sinceKotlin/out/html/root/package-list b/plugins/base/src/test/resources/expect/sinceKotlin/out/html/root/package-list new file mode 100644 index 00000000..8810ec13 --- /dev/null +++ b/plugins/base/src/test/resources/expect/sinceKotlin/out/html/root/package-list @@ -0,0 +1,5 @@ +$dokka.format:html +$dokka.linkExtension:html +$dokka.location://availableSince1.1/#//root//available-since1.1.html + + diff --git a/plugins/base/src/test/resources/expect/suspendFunction/out/html/root/package-list b/plugins/base/src/test/resources/expect/suspendFunction/out/html/root/package-list new file mode 100644 index 00000000..caf6273a --- /dev/null +++ b/plugins/base/src/test/resources/expect/suspendFunction/out/html/root/package-list @@ -0,0 +1,5 @@ +$dokka.format:html +$dokka.linkExtension:html +$dokka.location://f/#//root//f.html + + diff --git a/plugins/base/src/test/resources/expect/suspendInlineFunction/out/html/root/package-list b/plugins/base/src/test/resources/expect/suspendInlineFunction/out/html/root/package-list new file mode 100644 index 00000000..60993388 --- /dev/null +++ b/plugins/base/src/test/resources/expect/suspendInlineFunction/out/html/root/package-list @@ -0,0 +1,5 @@ +$dokka.format:html +$dokka.linkExtension:html +$dokka.location://f/#kotlin.Function0[kotlin.String]//root//f.html + + -- cgit