aboutsummaryrefslogtreecommitdiff
path: root/plugins/templating/src/main/kotlin/templates/ReplaceVersionCommandHandler.kt
blob: 025708492602fa249164edcf92b45f846e4605aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package templates

import org.jetbrains.dokka.base.templating.Command
import org.jetbrains.dokka.base.templating.ReplaceVersionsCommand
import org.jetbrains.dokka.plugability.DokkaContext
import org.jetbrains.dokka.templates.CommandHandler
import org.jsoup.nodes.Element
import org.jsoup.nodes.TextNode
import java.io.File

class ReplaceVersionCommandHandler(private val context: DokkaContext) : CommandHandler {

    override fun canHandle(command: Command): Boolean = command is ReplaceVersionsCommand

    override fun handleCommandAsTag(command: Command, body: Element, input: File, output: File) {
        val position = body.elementSiblingIndex()
        val parent = body.parent()
        body.remove()
        context.configuration.moduleVersion?.takeIf { it.isNotEmpty() }
            ?.let { parent.insertChildren(position, TextNode(it)) }
    }
}