From ca3d34cc9e5be2d9616cfde3d2cac489c07bf102 Mon Sep 17 00:00:00 2001
From: Kamil Doległo <kamilok1965@interia.pl>
Date: Wed, 20 Nov 2019 15:20:04 +0100
Subject: Initial XML plugin 2

---
 plugins/xml/src/main/kotlin/XmlPlugin.kt           | 104 +++++++++++++++++++++
 .../org.jetbrains.dokka.plugability.DokkaPlugin    |   1 +
 2 files changed, 105 insertions(+)
 create mode 100644 plugins/xml/src/main/kotlin/XmlPlugin.kt
 create mode 100644 plugins/xml/src/main/resources/META-INF/services/org.jetbrains.dokka.plugability.DokkaPlugin

(limited to 'plugins/xml/src')

diff --git a/plugins/xml/src/main/kotlin/XmlPlugin.kt b/plugins/xml/src/main/kotlin/XmlPlugin.kt
new file mode 100644
index 00000000..4a932062
--- /dev/null
+++ b/plugins/xml/src/main/kotlin/XmlPlugin.kt
@@ -0,0 +1,104 @@
+package org.jetbrains.dokka.xml
+
+import org.jetbrains.dokka.CoreExtensions
+import org.jetbrains.dokka.DefaultExtra
+import org.jetbrains.dokka.Model.DocumentationNode
+import org.jetbrains.dokka.Model.dfs
+import org.jetbrains.dokka.links.DRI
+import org.jetbrains.dokka.pages.*
+import org.jetbrains.dokka.plugability.DokkaContext
+import org.jetbrains.dokka.plugability.DokkaPlugin
+import org.jetbrains.dokka.transformers.PageNodeTransformer
+
+class XmlPlugin : DokkaPlugin() {
+    val transformer by extending {
+        CoreExtensions.pageTransformer with XmlTransformer
+    }
+}
+
+private const val ANNOTATION = "@usesMathJax"
+private const val LIB_PATH = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.6/MathJax.js?config=TeX-AMS_SVG&latest"
+
+object XmlTransformer : PageNodeTransformer {
+    enum class XMLKind : Kind {
+        Main, XmlList
+    }
+
+    override fun invoke(original: ModulePageNode, dokkaContext: DokkaContext): ModulePageNode {
+        original.findAll { it is ClassPageNode }.forEach { node ->
+            val refs = node.documentationNode?.extra?.filterIsInstance<DefaultExtra>()?.filter { it.key == "@attr ref"}.orEmpty()
+            val elementsToAdd = mutableListOf<DocumentationNode<*>>()
+
+            refs.forEach { ref ->
+                val toFind = DRI.from(ref.value)
+                original.documentationNode?.dfs { it.dri == toFind }?.let { elementsToAdd.add(it) }
+            }
+
+            val refTable = ContentGroup(
+                listOf(
+                    ContentHeader(
+                        listOf(
+                            ContentText(
+                                "XML Attributes",
+                                DCI(node.dri, XMLKind.XmlList),
+                                node.platforms().toSet(), emptySet(), emptySet()
+                            )
+                        ),
+                        2,
+                        DCI(node.dri, XMLKind.Main),
+                        node.platforms().toSet(), emptySet(), emptySet()
+                    ),
+                    ContentTable(
+                        emptyList(),
+                        elementsToAdd.map {
+                            ContentGroup(
+                                listOf(
+                                    ContentDRILink(
+                                        listOf(
+                                            ContentText(
+                                                it.descriptors.first().name.toString(),
+                                                DCI(node.dri, XMLKind.Main),
+                                                node.platforms().toSet(), emptySet(), emptySet()
+                                            )
+                                        ),
+                                        it.dri,
+                                        DCI(it.dri, XMLKind.XmlList),
+                                        it.platformData.toSet(), emptySet(), emptySet()
+                                    ),
+                                    ContentText(
+                                        it.briefDocstring,
+                                        DCI(it.dri, XMLKind.XmlList),
+                                        it.platformData.toSet(), emptySet(), emptySet()
+                                    )
+                                ),
+                                DCI(node.dri, XMLKind.XmlList),
+                                node.platforms().toSet(), emptySet(), emptySet()
+                            )
+                        },
+                        DCI(node.dri, XMLKind.XmlList),
+                        node.platforms().toSet(), emptySet(), emptySet()
+                    )
+                ),
+                DCI(node.dri, XMLKind.XmlList),
+                node.platforms().toSet(), emptySet(), emptySet()
+            )
+
+            val content = node.content as ContentGroup
+            val children = (node.content as ContentGroup).children
+            node.content = content.copy(children = children + refTable)
+        }
+        return original
+    }
+
+    private fun PageNode.findAll(predicate: (PageNode) -> Boolean): Set<PageNode> {
+        val found = mutableSetOf<PageNode>()
+        if (predicate(this)) {
+            found.add(this)
+        } else {
+            this.children.asSequence().mapNotNull { it.findAll(predicate) }.forEach { found.addAll(it) }
+        }
+        return found
+    }
+
+    private fun PageNode.platforms() = this.content.platforms.toList()
+}
\ No newline at end of file
diff --git a/plugins/xml/src/main/resources/META-INF/services/org.jetbrains.dokka.plugability.DokkaPlugin b/plugins/xml/src/main/resources/META-INF/services/org.jetbrains.dokka.plugability.DokkaPlugin
new file mode 100644
index 00000000..ebc3e551
--- /dev/null
+++ b/plugins/xml/src/main/resources/META-INF/services/org.jetbrains.dokka.plugability.DokkaPlugin
@@ -0,0 +1 @@
+org.jetbrains.dokka.xml.XmlPlugin
-- 
cgit