aboutsummaryrefslogtreecommitdiff
path: root/plugins/xml/src/main/kotlin/utils.kt
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/utils.kt
parentca3d34cc9e5be2d9616cfde3d2cac489c07bf102 (diff)
downloaddokka-4f59d3df7a0a3525bf3a0dab8617746a4db30f67.tar.gz
dokka-4f59d3df7a0a3525bf3a0dab8617746a4db30f67.tar.bz2
dokka-4f59d3df7a0a3525bf3a0dab8617746a4db30f67.zip
XML plugin refactoring
Diffstat (limited to 'plugins/xml/src/main/kotlin/utils.kt')
-rw-r--r--plugins/xml/src/main/kotlin/utils.kt135
1 files changed, 135 insertions, 0 deletions
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()
+
+