aboutsummaryrefslogtreecommitdiff
path: root/plugins/all-modules-page/src/main/kotlin/AllModulesPageGeneration.kt
blob: 21c624efa4a9d5b3905b9f66fc40a76c1aead57e (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
66
67
68
69
70
71
72
73
package org.jetbrains.dokka.allModulesPage

import org.jetbrains.dokka.CoreExtensions
import org.jetbrains.dokka.Timer
import org.jetbrains.dokka.generation.Generation
import org.jetbrains.dokka.pages.RootPageNode
import org.jetbrains.dokka.plugability.DokkaContext
import org.jetbrains.dokka.plugability.plugin
import org.jetbrains.dokka.plugability.query
import org.jetbrains.dokka.plugability.querySingle
import org.jetbrains.dokka.templates.TemplatingPlugin
import org.jetbrains.dokka.templates.TemplatingResult
import org.jetbrains.dokka.transformers.pages.CreationContext
import org.jetbrains.dokka.versioning.VersioningPlugin

class AllModulesPageGeneration(private val context: DokkaContext) : Generation {

    private val allModulesPagePlugin by lazy { context.plugin<AllModulesPagePlugin>() }
    private val templatingPlugin by lazy { context.plugin<TemplatingPlugin>() }
    private val versioningPlugin by lazy { context.plugin<VersioningPlugin>() }

    override fun Timer.generate() {
        report("Processing submodules")
        val generationContext = processSubmodules()

        report("Creating all modules page")
        val pages = createAllModulesPage(generationContext)

        report("Copy previous documentation")
        handlePreviousDocs()

        report("Transforming pages")
        val transformedPages = transformAllModulesPage(pages)

        report("Rendering")
        render(transformedPages)

        report("Processing multimodule")
        processMultiModule(transformedPages)

        report("Finish submodule processing")
        finishProcessingSubmodules()
    }

    override val generationName = "index page for project"

    fun handlePreviousDocs() = versioningPlugin.querySingle { versioningHandler }.invoke()

    fun createAllModulesPage(allModulesContext: DefaultAllModulesContext) =
        allModulesPagePlugin.querySingle { allModulesPageCreator }.invoke(allModulesContext)

    fun transformAllModulesPage(pages: RootPageNode) =
        allModulesPagePlugin.query { allModulesPageTransformer }.fold(pages) { acc, t -> t(acc) }

    fun render(transformedPages: RootPageNode) {
        context.single(CoreExtensions.renderer).render(transformedPages)
    }

    fun processSubmodules() =
        templatingPlugin.querySingle { submoduleTemplateProcessor }
            .process(context.configuration.modules)
            .let { DefaultAllModulesContext(it) }

    fun processMultiModule(root: RootPageNode) =
        templatingPlugin.querySingle { multimoduleTemplateProcessor }.process(root)

    fun finishProcessingSubmodules() =
        templatingPlugin.query { templateProcessingStrategy }.forEach { it.finish(context.configuration.outputDir) }

    data class DefaultAllModulesContext(val nonEmptyModules: List<String>) : CreationContext {
        constructor(templating: TemplatingResult) : this(templating.modules)
    }
}