aboutsummaryrefslogtreecommitdiff
path: root/plugins/xml/src/main/kotlin/XmlPlugin.kt
blob: 839929e6d6bfe4b4cbc74a673526671a5ea14f79 (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
package org.jetbrains.dokka.xml

import org.jetbrains.dokka.CoreExtensions
import org.jetbrains.dokka.model.Documentable
import org.jetbrains.dokka.model.dfs
import org.jetbrains.dokka.pages.*
import org.jetbrains.dokka.plugability.DokkaContext
import org.jetbrains.dokka.plugability.DokkaPlugin
import org.jetbrains.dokka.plugability.single
import org.jetbrains.dokka.transformers.descriptors.XMLMega
import org.jetbrains.dokka.transformers.pages.PageNodeTransformer

class XmlPlugin : DokkaPlugin() {
    val transformer by extending {
        CoreExtensions.pageTransformer with XmlTransformer
    }
}

object XmlTransformer : PageNodeTransformer {
    enum class XMLKind : Kind {
        Main, XmlList
    }

    override fun invoke(input: ModulePageNode, dokkaContext: DokkaContext): ModulePageNode =
        input.transformPageNodeTree { node ->
            if (node !is ClassPageNode) node
            else {
                val refs =
                    node.documentable?.extra?.filterIsInstance<XMLMega>()?.filter { it.key == "@attr ref" }
                        .orEmpty()
                val elementsToAdd = mutableListOf<Documentable>()

                refs.forEach { ref ->
                    input.documentable?.dfs { it.dri == ref.dri }?.let { elementsToAdd.add(it) }
                }
                val platformData = node.platforms().toSet()
                val refTable = DefaultPageContentBuilder.group(
                    node.dri,
                    platformData,
                    XMLKind.XmlList,
                    dokkaContext.single(CoreExtensions.commentsToContentConverter),
                    dokkaContext.logger
                ) {
                    block("XML Attributes", 2, XMLKind.XmlList, elementsToAdd, platformData) { element ->
                        link(element.dri, XMLKind.XmlList) {
                            text(element.name ?: "<unnamed>", XMLKind.Main)
                        }
                        text(element.briefDocTagString, XMLKind.XmlList)
                    }
                }

                val content = node.content as ContentGroup
                val children = (node.content as ContentGroup).children
                node.modified(content = content.copy(children = children + refTable))
            }
        }

    private fun PageNode.platforms() = this.content.platforms.toList()
}