diff options
author | Andrzej Ratajczak <andrzej.ratajczak98@gmail.com> | 2020-04-03 13:57:33 +0200 |
---|---|---|
committer | Kamil Doległo <kamilok1965@users.noreply.github.com> | 2020-04-15 14:33:24 +0200 |
commit | fee86d79a3aa7357c68c2f8f519d187f43447e8c (patch) | |
tree | 7ba7ef24bf7733e7778f0a5344756ed39c364a3a /plugins | |
parent | 64e600b126ae8edcfbb3c2716febb01d221520b5 (diff) | |
download | dokka-fee86d79a3aa7357c68c2f8f519d187f43447e8c.tar.gz dokka-fee86d79a3aa7357c68c2f8f519d187f43447e8c.tar.bz2 dokka-fee86d79a3aa7357c68c2f8f519d187f43447e8c.zip |
Refactor package list to be a preprocessor
Diffstat (limited to 'plugins')
25 files changed, 243 insertions, 39 deletions
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<T>( 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<String, List<PlatformData>> = - root.children - .map(::getPackageNamesAndPlatforms) - .fold(emptyMap<String, List<PlatformData>>()) { 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<DokkaBase>().querySingle { locationProviderFactory }.getLocationProvider(newRoot) - root.children<ModulePageNode>().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<String>() + val nonStandardLocations = mutableMapOf<String, String>() + + val locationProvider = + context.plugin<DokkaBase>().querySingle { locationProviderFactory }.getLocationProvider(module) + + fun visit(node: PageNode, parentDris: Set<DRI>) { + + if (node is PackagePageNode) { + packages.add(node.name) + } + + val contentPage = node.safeAs<ContentPage>() + 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<ContentPage>().single().let(::visit) + input.children.filterIsInstance<ContentPage>().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<ContentPage>().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<ContentPage>() + .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/<jvm root>/f.html +<jvm root> + 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/<jvm root>/f.html +$dokka.location:/Fancy/equals/#kotlin.Any?//root/<jvm root>/-fancy/equals.html +$dokka.location:/Fancy/hashCode/#//root/<jvm root>/-fancy/hash-code.html +$dokka.location:/Fancy/toString/#//root/<jvm root>/-fancy/to-string.html +<jvm root> + 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/<jvm root>/fn.html +<jvm root> + 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/<jvm root>/function.html +$dokka.location:/Fancy/equals/#kotlin.Any?//root/<jvm root>/-fancy/equals.html +$dokka.location:/Fancy/hashCode/#//root/<jvm root>/-fancy/hash-code.html +$dokka.location:/Fancy/toString/#//root/<jvm root>/-fancy/to-string.html +<jvm root> + 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/<jvm root>/f.html +<jvm root> + 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/<jvm root>/function.html +<jvm root> + 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/<jvm root>/f.html +<jvm root> + 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/<jvm root>/function.html +<jvm root> + 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/<jvm root>/fn.html +$dokka.location://fn/kotlin.String#kotlin.Int//root/<jvm root>/fn.html +<jvm root> + 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/<jvm root>/generic.html +<jvm root> + 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/<jvm root>/generic.html +<jvm root> + 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/<jvm root>/f.html +<jvm root> + 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/<jvm root>/f.html +<jvm root> + 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/<jvm root>/available-since1.1.html +<jvm root> + 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/<jvm root>/f.html +<jvm root> + 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/<jvm root>/f.html +<jvm root> + diff --git a/plugins/gfm/src/main/kotlin/GfmPlugin.kt b/plugins/gfm/src/main/kotlin/GfmPlugin.kt index 7c0ce509..91b6f7b5 100644 --- a/plugins/gfm/src/main/kotlin/GfmPlugin.kt +++ b/plugins/gfm/src/main/kotlin/GfmPlugin.kt @@ -3,17 +3,23 @@ package org.jetbrains.dokka.gfm import org.jetbrains.dokka.CoreExtensions import org.jetbrains.dokka.base.DokkaBase import org.jetbrains.dokka.base.renderers.DefaultRenderer -import org.jetbrains.dokka.base.renderers.OutputWriter +import org.jetbrains.dokka.base.renderers.PackageListCreator +import org.jetbrains.dokka.base.renderers.RootCreator import org.jetbrains.dokka.base.resolvers.local.DefaultLocationProvider import org.jetbrains.dokka.base.resolvers.local.LocationProviderFactory import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.plugability.DokkaPlugin +import org.jetbrains.dokka.plugability.plugin +import org.jetbrains.dokka.plugability.query +import org.jetbrains.dokka.transformers.pages.PageTransformer import java.lang.StringBuilder class GfmPlugin : DokkaPlugin() { + val gfmPreprocessors by extensionPoint<PageTransformer>() + val renderer by extending { CoreExtensions.renderer providing { CommonmarkRenderer(it) } applyIf { format == "gfm" } } @@ -21,11 +27,28 @@ class GfmPlugin : DokkaPlugin() { val locationProvider by extending { plugin<DokkaBase>().locationProviderFactory providing { MarkdownLocationProviderFactory(it) } applyIf { format == "gfm" } } + + val rootCreator by extending { + gfmPreprocessors with RootCreator + } + + val packageListCreator by extending { + gfmPreprocessors providing { + PackageListCreator( + it, + "gfm", + "md" + ) + } order { after(rootCreator) } + } } open class CommonmarkRenderer( context: DokkaContext ) : DefaultRenderer<StringBuilder>(context) { + + override val preprocessors = context.plugin<GfmPlugin>().query { gfmPreprocessors } + override fun StringBuilder.buildHeader(level: Int, content: StringBuilder.() -> Unit) { buildParagraph() append("#".repeat(level) + " ") diff --git a/plugins/jekyll/src/main/kotlin/JekyllPlugin.kt b/plugins/jekyll/src/main/kotlin/JekyllPlugin.kt index c4f7f2c7..b9f5e1a5 100644 --- a/plugins/jekyll/src/main/kotlin/JekyllPlugin.kt +++ b/plugins/jekyll/src/main/kotlin/JekyllPlugin.kt @@ -1,24 +1,47 @@ package org.jetbrains.dokka.jekyll import org.jetbrains.dokka.CoreExtensions +import org.jetbrains.dokka.base.renderers.PackageListCreator +import org.jetbrains.dokka.base.renderers.RootCreator import org.jetbrains.dokka.gfm.CommonmarkRenderer import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.plugability.DokkaPlugin +import org.jetbrains.dokka.plugability.plugin +import org.jetbrains.dokka.plugability.query +import org.jetbrains.dokka.transformers.pages.PageTransformer import java.lang.StringBuilder class JekyllPlugin : DokkaPlugin() { + val jekyllPreprocessors by extensionPoint<PageTransformer>() + val renderer by extending { CoreExtensions.renderer providing { JekyllRenderer(it) } applyIf { format == "jekyll" } } + + val rootCreator by extending { + jekyllPreprocessors with RootCreator + } + + val packageListCreator by extending { + jekyllPreprocessors providing { + PackageListCreator( + it, + "jekyll", + "md" + ) + } order { after(rootCreator) } + } } class JekyllRenderer( context: DokkaContext ) : CommonmarkRenderer(context) { + override val preprocessors = context.plugin<JekyllPlugin>().query { jekyllPreprocessors } + override fun buildPage(page: ContentPage, content: (StringBuilder, ContentPage) -> Unit): String { val builder = StringBuilder() builder.append("---\n") |