aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/pages/utils.kt
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/main/kotlin/pages/utils.kt')
-rw-r--r--core/src/main/kotlin/pages/utils.kt31
1 files changed, 31 insertions, 0 deletions
diff --git a/core/src/main/kotlin/pages/utils.kt b/core/src/main/kotlin/pages/utils.kt
new file mode 100644
index 00000000..c9039416
--- /dev/null
+++ b/core/src/main/kotlin/pages/utils.kt
@@ -0,0 +1,31 @@
+package org.jetbrains.dokka.pages
+
+import kotlin.reflect.KClass
+
+inline fun <reified T : ContentNode, R : ContentNode> R.mapTransform(noinline operation: (T) -> T): R =
+ mapTransform(T::class, operation)
+
+@PublishedApi
+@Suppress("UNCHECKED_CAST")
+internal fun <T : ContentNode, R : ContentNode> R.mapTransform(type: KClass<T>, operation: (T) -> T): R {
+ if (this::class == type) {
+ return operation(this as T) as R
+ }
+ val new = when (this) {
+ is ContentGroup -> this.copy(children.map { it.mapTransform(type, operation) })
+ is ContentHeader -> this.copy(children.map { it.mapTransform(type, operation) })
+ is ContentCodeBlock -> this.copy(children.map { it.mapTransform(type, operation) })
+ is ContentCodeInline -> this.copy(children.map { it.mapTransform(type, operation) })
+ is ContentTable -> this.copy(children.map { it.mapTransform(type, operation) })
+ is ContentList -> this.copy(children.map { it.mapTransform(type, operation) })
+ is ContentDivergentGroup -> this.copy(children.map { it.mapTransform(type, operation) })
+ is ContentDivergentInstance -> this.copy(
+ before = before?.mapTransform(type, operation),
+ divergent = divergent.mapTransform(type, operation),
+ after = after?.mapTransform(type, operation)
+ )
+ is PlatformHintedContent -> this.copy(inner.mapTransform(type, operation))
+ else -> this
+ }
+ return new as R
+}