aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/main/kotlin/renderers
diff options
context:
space:
mode:
authorAndrzej Ratajczak <andrzej.ratajczak98@gmail.com>2020-04-03 13:57:33 +0200
committerKamil Doległo <kamilok1965@users.noreply.github.com>2020-04-15 14:33:24 +0200
commitfee86d79a3aa7357c68c2f8f519d187f43447e8c (patch)
tree7ba7ef24bf7733e7778f0a5344756ed39c364a3a /plugins/base/src/main/kotlin/renderers
parent64e600b126ae8edcfbb3c2716febb01d221520b5 (diff)
downloaddokka-fee86d79a3aa7357c68c2f8f519d187f43447e8c.tar.gz
dokka-fee86d79a3aa7357c68c2f8f519d187f43447e8c.tar.bz2
dokka-fee86d79a3aa7357c68c2f8f519d187f43447e8c.zip
Refactor package list to be a preprocessor
Diffstat (limited to 'plugins/base/src/main/kotlin/renderers')
-rw-r--r--plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt19
-rw-r--r--plugins/base/src/main/kotlin/renderers/PackageListService.kt51
-rw-r--r--plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt29
-rw-r--r--plugins/base/src/main/kotlin/renderers/preprocessors.kt27
4 files changed, 92 insertions, 34 deletions
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))
+ )
+}