aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/pages/RendererSpecificPage.kt
blob: a057b95e7bde14d91bf94c17a1a5a28b29be6dec (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
package org.jetbrains.dokka.pages

import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.model.DisplaySourceSet
import org.jetbrains.dokka.renderers.Renderer
import kotlin.reflect.KClass

fun interface DriResolver: (DRI, Set<DisplaySourceSet>) -> String?
fun interface PageResolver: (PageNode, PageNode?) -> String?

interface RendererSpecificPage : PageNode {
    val strategy: RenderingStrategy
}

class RendererSpecificRootPage(
    override val name: String,
    override val children: List<PageNode>,
    override val strategy: RenderingStrategy
) : RootPageNode(), RendererSpecificPage {
    override fun modified(name: String, children: List<PageNode>): RendererSpecificRootPage =
        RendererSpecificRootPage(name, children, strategy)
}

class RendererSpecificResourcePage(
    override val name: String,
    override val children: List<PageNode>,
    override val strategy: RenderingStrategy
): RendererSpecificPage {
    override fun modified(name: String, children: List<PageNode>): RendererSpecificResourcePage =
        RendererSpecificResourcePage(name, children, strategy)
}

sealed class RenderingStrategy {
    class Callback(val instructions: Renderer.(PageNode) -> String): RenderingStrategy()
    data class Copy(val from: String) : RenderingStrategy()
    data class Write(val text: String) : RenderingStrategy()
    data class DriLocationResolvableWrite(val contentToResolve: (DriResolver) -> String) : RenderingStrategy()
    data class PageLocationResolvableWrite(val contentToResolve: (PageResolver) -> String) : RenderingStrategy()
    object DoNothing : RenderingStrategy()

    companion object {
        inline operator fun <reified T: Renderer> invoke(crossinline instructions: T.(PageNode) -> String) =
            Callback { if (this is T) instructions(it) else throw WrongRendererTypeException(T::class) }
    }
}

data class WrongRendererTypeException(val expectedType: KClass<*>): Exception()