diff --git a/plugins/base/src/main/kotlin/DokkaBase.kt b/plugins/base/src/main/kotlin/DokkaBase.kt index 5a7164d1d4..d84207a5a5 100644 --- a/plugins/base/src/main/kotlin/DokkaBase.kt +++ b/plugins/base/src/main/kotlin/DokkaBase.kt @@ -233,7 +233,7 @@ class DokkaBase : DokkaPlugin() { } val sourcesetDependencyAppender by extending { - htmlPreprocessors providing ::SourcesetDependencyAppender order { after(rootCreator) } applyIf { !delayTemplateSubstitution } + htmlPreprocessors providing ::SourcesetDependencyAppender order { after(rootCreator) } } val resolveLinkConsumer by extending { diff --git a/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt b/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt index c5a92009fe..92f7324c44 100644 --- a/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt +++ b/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt @@ -5,6 +5,8 @@ import org.jetbrains.dokka.base.DokkaBase import org.jetbrains.dokka.base.DokkaBaseConfiguration import org.jetbrains.dokka.base.renderers.sourceSets import org.jetbrains.dokka.base.templating.AddToSearch +import org.jetbrains.dokka.base.templating.AddToSourcesetDependencies +import org.jetbrains.dokka.base.templating.toJsonString import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.model.* import org.jetbrains.dokka.pages.* @@ -169,15 +171,15 @@ class SourcesetDependencyAppender(val context: DokkaContext) : PageTransformer { it.sourceSetID to it.dependentSourceSets }.toMap() - fun createDependenciesJson(): String = "sourceset_dependencies = '{${ - dependenciesMap.entries.joinToString(", ") { - "\"${it.key}\": [${ - it.value.joinToString(",") { - "\"$it\"" + fun createDependenciesJson(): String = + dependenciesMap.map { (key, values) -> key.toString() to values.map { it.toString() } }.toMap() + .let { content -> + if (context.configuration.delayTemplateSubstitution) { + toJsonString(AddToSourcesetDependencies(context.configuration.moduleName, content)) + } else { + "sourceset_dependencies='${toJsonString(content)}'" } - }]" - } - }}'" + } val deps = RendererSpecificResourcePage( name = name, diff --git a/plugins/base/src/main/kotlin/templating/AddToSourcesetDependencies.kt b/plugins/base/src/main/kotlin/templating/AddToSourcesetDependencies.kt new file mode 100644 index 0000000000..3f1680b78f --- /dev/null +++ b/plugins/base/src/main/kotlin/templating/AddToSourcesetDependencies.kt @@ -0,0 +1,3 @@ +package org.jetbrains.dokka.base.templating + +data class AddToSourcesetDependencies(val moduleName: String, val content: Map>) : Command \ No newline at end of file diff --git a/plugins/templating/src/main/kotlin/templates/SourcesetDependencyProcessingStrategy.kt b/plugins/templating/src/main/kotlin/templates/SourcesetDependencyProcessingStrategy.kt new file mode 100644 index 0000000000..11bbc39a75 --- /dev/null +++ b/plugins/templating/src/main/kotlin/templates/SourcesetDependencyProcessingStrategy.kt @@ -0,0 +1,33 @@ +package templates + +import org.jetbrains.dokka.base.templating.AddToSourcesetDependencies +import org.jetbrains.dokka.base.templating.parseJson +import org.jetbrains.dokka.base.templating.toJsonString +import org.jetbrains.dokka.plugability.DokkaContext +import org.jetbrains.dokka.templates.TemplateProcessingStrategy +import java.io.File +import java.util.concurrent.ConcurrentHashMap + +private typealias Entry = Map> + +class SourcesetDependencyProcessingStrategy(val context: DokkaContext) : TemplateProcessingStrategy { + private val fileName = "sourceset_dependencies.js" + private val fragments = ConcurrentHashMap() + + override fun finish(output: File) { + if (fragments.isNotEmpty()) { + val content = fragments.values.fold(emptyMap>()) { acc, e -> acc + e } + .let { "sourceset_dependencies = '${toJsonString(it)}'" } + output.resolve("scripts").mkdirs() + output.resolve("scripts/$fileName").writeText(content) + } + } + + override fun process(input: File, output: File): Boolean = + input.takeIf { it.name == fileName } + ?.runCatching { parseJson(input.readText()) } + ?.getOrNull() + ?.also { (moduleName, content) -> + fragments += (moduleName to content) + } != null +} \ No newline at end of file diff --git a/plugins/templating/src/main/kotlin/templates/TemplatingPlugin.kt b/plugins/templating/src/main/kotlin/templates/TemplatingPlugin.kt index 546a044338..562b12c6f5 100644 --- a/plugins/templating/src/main/kotlin/templates/TemplatingPlugin.kt +++ b/plugins/templating/src/main/kotlin/templates/TemplatingPlugin.kt @@ -3,6 +3,7 @@ package org.jetbrains.dokka.templates import org.jetbrains.dokka.allModulesPage.templates.NavigationSearchTemplateStrategy import org.jetbrains.dokka.allModulesPage.templates.PagesSearchTemplateStrategy import org.jetbrains.dokka.plugability.DokkaPlugin +import templates.SourcesetDependencyProcessingStrategy class TemplatingPlugin : DokkaPlugin() { @@ -32,6 +33,12 @@ class TemplatingPlugin : DokkaPlugin() { } } + val sourcesetDependencyProcessingStrategy by extending { + templateProcessingStrategy providing ::SourcesetDependencyProcessingStrategy order { + before(fallbackProcessingStrategy) + } + } + val pagesSearchTemplateStrategy by extending { templateProcessingStrategy providing ::PagesSearchTemplateStrategy order { before(fallbackProcessingStrategy)