aboutsummaryrefslogtreecommitdiff
path: root/plugins/all-modules-page/src
diff options
context:
space:
mode:
authorMarcin Aman <marcin.aman@gmail.com>2020-12-17 10:17:45 +0100
committerGitHub <noreply@github.com>2020-12-17 10:17:45 +0100
commit2f7ee2b82cda39f6bd94c5200b83563418b68dd7 (patch)
tree0942f00012ee7a90208c5d80ed3dd5ec6a3d9f92 /plugins/all-modules-page/src
parent9e344b2047f72051bed509fb4e7ac1ae53f8098e (diff)
downloaddokka-2f7ee2b82cda39f6bd94c5200b83563418b68dd7.tar.gz
dokka-2f7ee2b82cda39f6bd94c5200b83563418b68dd7.tar.bz2
dokka-2f7ee2b82cda39f6bd94c5200b83563418b68dd7.zip
Navigate to root after logo click, add data to searchbars on multimodule (#1631)
Diffstat (limited to 'plugins/all-modules-page/src')
-rw-r--r--plugins/all-modules-page/src/main/kotlin/AllModulesPagePlugin.kt12
-rw-r--r--plugins/all-modules-page/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt74
2 files changed, 86 insertions, 0 deletions
diff --git a/plugins/all-modules-page/src/main/kotlin/AllModulesPagePlugin.kt b/plugins/all-modules-page/src/main/kotlin/AllModulesPagePlugin.kt
index 95a94cf4..c99293ef 100644
--- a/plugins/all-modules-page/src/main/kotlin/AllModulesPagePlugin.kt
+++ b/plugins/all-modules-page/src/main/kotlin/AllModulesPagePlugin.kt
@@ -54,6 +54,18 @@ class AllModulesPagePlugin : DokkaPlugin() {
templateProcessingStrategy providing ::FallbackTemplateProcessingStrategy
}
+ val navigationSearchTemplateStrategy by extending {
+ templateProcessingStrategy providing ::NavigationSearchTemplateStrategy order {
+ before(fallbackProcessingStrategy)
+ }
+ }
+
+ val pagesSearchTemplateStrategy by extending {
+ templateProcessingStrategy providing ::PagesSearchTemplateStrategy order {
+ before(fallbackProcessingStrategy)
+ }
+ }
+
val pathToRootSubstitutor by extending {
substitutor providing ::PathToRootSubstitutor
}
diff --git a/plugins/all-modules-page/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt b/plugins/all-modules-page/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt
new file mode 100644
index 00000000..c6c67752
--- /dev/null
+++ b/plugins/all-modules-page/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt
@@ -0,0 +1,74 @@
+package org.jetbrains.dokka.allModulesPage.templates
+
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.coroutineScope
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
+import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
+import java.io.File
+import java.nio.file.Files
+import com.fasterxml.jackson.module.kotlin.treeToValue
+import org.jetbrains.dokka.base.renderers.html.SearchRecord
+import org.jetbrains.dokka.base.templating.*
+import org.jetbrains.dokka.plugability.DokkaContext
+import java.util.concurrent.ConcurrentHashMap
+
+abstract class BaseJsonNavigationTemplateProcessingStrategy(val context: DokkaContext) : TemplateProcessingStrategy {
+ abstract val navigationFileNameWithoutExtension: String
+ abstract val path: String
+
+ private val fragments = ConcurrentHashMap<String, List<SearchRecord>>()
+
+ open fun canProcess(file: File): Boolean =
+ file.extension == "json" && file.nameWithoutExtension == navigationFileNameWithoutExtension
+
+ override suspend fun process(input: File, output: File): Boolean = coroutineScope {
+ val canProcess = canProcess(input)
+ if (canProcess) {
+ launch {
+ withContext(Dispatchers.IO) {
+ runCatching { parseJson<AddToSearch>(input.readText()) }.getOrNull()
+ }?.let { command ->
+ fragments[command.moduleName] = command.elements
+ } ?: fallbackToCopy(input, output)
+ }
+ }
+ canProcess
+ }
+
+ override suspend fun finish(output: File) {
+ if (fragments.isNotEmpty()) {
+ val content = toJsonString(fragments.entries.flatMap { (moduleName, navigation) ->
+ navigation.map { it.withResolvedLocation(moduleName) }
+ })
+ withContext(Dispatchers.IO) {
+ output.resolve("$path/$navigationFileNameWithoutExtension.json").writeText(content)
+
+ fragments.keys.forEach {
+ output.resolve(it).resolve("$path/$navigationFileNameWithoutExtension.json").writeText(content)
+ }
+ }
+ }
+ }
+
+ private suspend fun fallbackToCopy(input: File, output: File) {
+ context.logger.warn("Falling back to just copying file for ${input.name} even thought it should process it")
+ withContext(Dispatchers.IO) { input.copyTo(output) }
+ }
+
+ private fun SearchRecord.withResolvedLocation(moduleName: String): SearchRecord =
+ copy(location = "$moduleName/$location")
+
+}
+
+class NavigationSearchTemplateStrategy(val dokkaContext: DokkaContext) :
+ BaseJsonNavigationTemplateProcessingStrategy(dokkaContext) {
+ override val navigationFileNameWithoutExtension: String = "navigation-pane"
+ override val path: String = "scripts"
+}
+
+class PagesSearchTemplateStrategy(val dokkaContext: DokkaContext) :
+ BaseJsonNavigationTemplateProcessingStrategy(dokkaContext) {
+ override val navigationFileNameWithoutExtension: String = "pages"
+ override val path: String = "scripts"
+} \ No newline at end of file