aboutsummaryrefslogtreecommitdiff
path: root/plugins/xml/src/main/kotlin
diff options
context:
space:
mode:
authorKamil Doległo <kamilok1965@interia.pl>2019-11-20 17:42:45 +0100
committerBłażej Kardyś <bkardys@virtuslab.com>2019-11-25 16:24:16 +0100
commit4f59d3df7a0a3525bf3a0dab8617746a4db30f67 (patch)
tree4946e49ce708e8876c41fb921bcada4da860da30 /plugins/xml/src/main/kotlin
parentca3d34cc9e5be2d9616cfde3d2cac489c07bf102 (diff)
downloaddokka-4f59d3df7a0a3525bf3a0dab8617746a4db30f67.tar.gz
dokka-4f59d3df7a0a3525bf3a0dab8617746a4db30f67.tar.bz2
dokka-4f59d3df7a0a3525bf3a0dab8617746a4db30f67.zip
XML plugin refactoring
Diffstat (limited to 'plugins/xml/src/main/kotlin')
-rw-r--r--plugins/xml/src/main/kotlin/XmlPlugin.kt65
-rw-r--r--plugins/xml/src/main/kotlin/utils.kt135
2 files changed, 144 insertions, 56 deletions
diff --git a/plugins/xml/src/main/kotlin/XmlPlugin.kt b/plugins/xml/src/main/kotlin/XmlPlugin.kt
index 4a932062..4023e1cc 100644
--- a/plugins/xml/src/main/kotlin/XmlPlugin.kt
+++ b/plugins/xml/src/main/kotlin/XmlPlugin.kt
@@ -16,78 +16,31 @@ class XmlPlugin : DokkaPlugin() {
}
}
-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 ->
+ override fun invoke(input: ModulePageNode, dokkaContext: DokkaContext): ModulePageNode {
+ input.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) }
+ input.documentationNode?.dfs { it.dri == toFind }?.let { elementsToAdd.add(it) }
+ }
+ val refTable = group(node.platforms().toSet(), node.dri, XMLKind.XmlList) {
+ table("XML Attributes", 2, XMLKind.XmlList, elementsToAdd) {
+ elementsToAdd.map { row(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
+ return input
}
private fun PageNode.findAll(predicate: (PageNode) -> Boolean): Set<PageNode> {
diff --git a/plugins/xml/src/main/kotlin/utils.kt b/plugins/xml/src/main/kotlin/utils.kt
new file mode 100644
index 00000000..510f3328
--- /dev/null
+++ b/plugins/xml/src/main/kotlin/utils.kt
@@ -0,0 +1,135 @@
+package org.jetbrains.dokka.xml
+
+import org.jetbrains.dokka.Model.DocumentationNode
+import org.jetbrains.dokka.links.DRI
+import org.jetbrains.dokka.pages.*
+
+internal class ContentBuilder(
+ val platforms: Set<PlatformData>,
+ val dri: DRI,
+ val kind: Kind,
+ val styles: Set<Style> = emptySet(),
+ val extras: Set<Extra> = emptySet()
+) {
+ private val contents = mutableListOf<ContentNode>()
+
+ fun build() = ContentGroup(
+ contents.toList(),
+ DCI(dri, kind),
+ platforms,
+ styles,
+ extras
+ )
+
+ fun header(level: Int, block: ContentBuilder.() -> Unit) {
+ contents += ContentHeader(level, group(ContentKind.Symbol, block))
+ }
+
+ private fun createText(text: String) =
+ ContentText(text, DCI(dri, ContentKind.Symbol), platforms, styles, extras)
+
+ fun text(text: String) {
+ contents += createText(text)
+ }
+
+ inline fun <T : Any> block(
+ name: String,
+ level: Int,
+ kind: Kind,
+ elements: Iterable<T>,
+ platformData: Set<PlatformData>,
+ operation: ContentBuilder.(T) -> Unit
+ ) {
+ header(level) { text(name) }
+
+ contents += ContentTable(
+ emptyList(),
+ elements.map { group(platforms, dri, kind) { operation(it) } },
+ DCI(dri, kind),
+ platformData, styles, extras
+ )
+ }
+
+ inline fun <T> list(
+ elements: List<T>,
+ prefix: String = "",
+ suffix: String = "",
+ separator: String = ", ",
+ block: ContentBuilder.(T) -> Unit
+ ) {
+ if (elements.isNotEmpty()) {
+ if (prefix.isNotEmpty()) text(prefix)
+ elements.dropLast(1).forEach {
+ block(it)
+ text(separator)
+ }
+ block(elements.last())
+ if (suffix.isNotEmpty()) text(suffix)
+ }
+ }
+
+ fun link(text: String, address: DRI) {
+ contents += ContentDRILink(
+ listOf(createText(text)),
+ address,
+ DCI(dri, ContentKind.Symbol),
+ platforms
+ )
+ }
+
+ fun <T: DocumentationNode<*>> table(
+ name: String,
+ level: Int,
+ kind: Kind,
+ elements: Iterable<T>,
+ operation: ContentBuilder.(T) -> Unit
+ ) {
+ header(level) { text(name) }
+
+ contents += ContentTable(
+ emptyList(),
+ elements.map { row(it) }.toList(),
+ DCI(dri, kind),
+ platforms, emptySet(), emptySet()
+ )
+ }
+
+
+ fun row(element: DocumentationNode<*>) =
+ ContentGroup(
+ listOf(
+ ContentDRILink(
+ listOf(
+ ContentText(
+ element.descriptors.first().name.toString(),
+ DCI(dri, XmlTransformer.XMLKind.Main),
+ platforms, emptySet(), emptySet()
+ )
+ ),
+ element.dri,
+ DCI(element.dri, XmlTransformer.XMLKind.XmlList),
+ element.platformData.toSet(), emptySet(), emptySet()
+ ),
+ ContentText(
+ element.briefDocstring,
+ DCI(element.dri, XmlTransformer.XMLKind.XmlList),
+ element.platformData.toSet(), emptySet(), emptySet()
+ )
+ ),
+ DCI(dri, XmlTransformer.XMLKind.XmlList),
+ platforms, emptySet(), emptySet()
+ )
+
+
+ private inline fun group(kind: Kind, block: ContentBuilder.() -> Unit): ContentGroup =
+ group(platforms, dri, kind, block)
+}
+
+internal inline fun group(
+ platforms: Set<PlatformData>,
+ dri: DRI,
+ kind: Kind = ContentKind.Main,
+ block: ContentBuilder.() -> Unit
+) = ContentBuilder(platforms, dri, kind).apply(block).build()
+
+