aboutsummaryrefslogtreecommitdiff
path: root/plugins/templating/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt
blob: dec3779984e4ca22e96bccbaa1dc5cb835f7388e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package org.jetbrains.dokka.allModulesPage.templates

import org.jetbrains.dokka.base.renderers.html.SearchRecord
import org.jetbrains.dokka.base.templating.AddToSearch
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

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 fun process(input: File, output: File): Boolean {
        val canProcess = canProcess(input)
        if (canProcess) {
            runCatching { parseJson<AddToSearch>(input.readText()) }.getOrNull()?.let { command ->
                context.configuration.modules.find { it.name == command.moduleName }?.relativePathToOutputDirectory
                    ?.relativeToOrSelf(context.configuration.outputDir)
                    ?.let { key ->
                        fragments[key.toString()] = command.elements
                    }
            } ?: fallbackToCopy(input, output)
        }
        return canProcess
    }

    override fun finish(output: File) {
        if (fragments.isNotEmpty()) {
            val content = toJsonString(fragments.entries.flatMap { (moduleName, navigation) ->
                navigation.map { it.withResolvedLocation(moduleName) }
            })
            output.resolve(path).mkdirs()
            output.resolve("$path/$navigationFileNameWithoutExtension.json").writeText(content)
        }
    }

    private fun fallbackToCopy(input: File, output: File) {
        context.logger.warn("Falling back to just copying file for ${input.name} even thought it should process it")
        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"
}