html output
This commit is contained in:
parent
875a5ecf54
commit
2aee38793c
28
dataforge-output/dataforge-output-html/build.gradle.kts
Normal file
28
dataforge-output/dataforge-output-html/build.gradle.kts
Normal file
@ -0,0 +1,28 @@
|
||||
plugins {
|
||||
kotlin("multiplatform")
|
||||
}
|
||||
|
||||
val htmlVersion by rootProject.extra("0.6.12")
|
||||
|
||||
kotlin {
|
||||
jvm()
|
||||
js()
|
||||
sourceSets {
|
||||
val commonMain by getting {
|
||||
dependencies {
|
||||
api(project(":dataforge-output"))
|
||||
api("org.jetbrains.kotlinx:kotlinx-html-common:$htmlVersion")
|
||||
}
|
||||
}
|
||||
val jsMain by getting {
|
||||
dependencies {
|
||||
api("org.jetbrains.kotlinx:kotlinx-html-js:$htmlVersion")
|
||||
}
|
||||
}
|
||||
val jvmMain by getting{
|
||||
dependencies {
|
||||
api("org.jetbrains.kotlinx:kotlinx-html-jvm:$htmlVersion")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
package hep.dataforge.output.html
|
||||
|
||||
import hep.dataforge.context.Context
|
||||
import hep.dataforge.meta.Meta
|
||||
import hep.dataforge.output.Output
|
||||
import hep.dataforge.output.TextRenderer
|
||||
import hep.dataforge.output.TextRenderer.Companion.TEXT_RENDERER_TYPE
|
||||
import hep.dataforge.output.html.HtmlBuilder.Companion.HTML_CONVERTER_TYPE
|
||||
import hep.dataforge.provider.Type
|
||||
import hep.dataforge.provider.provideAll
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.html.TagConsumer
|
||||
import kotlinx.html.p
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
|
||||
class HtmlOutput<T : Any>(override val context: Context, private val consumer: TagConsumer<*>) : Output<T> {
|
||||
private val cache = HashMap<KClass<*>, HtmlBuilder<*>>()
|
||||
|
||||
/**
|
||||
* Find the first [TextRenderer] matching the given object type.
|
||||
*/
|
||||
override fun render(obj: T, meta: Meta) {
|
||||
|
||||
val builder: HtmlBuilder<*> = if (obj is CharSequence) {
|
||||
DefaultHtmlBuilder
|
||||
} else {
|
||||
val value = cache[obj::class]
|
||||
if (value == null) {
|
||||
val answer = context.provideAll(HTML_CONVERTER_TYPE).filterIsInstance<HtmlBuilder<*>>()
|
||||
.filter { it.type.isInstance(obj) }.firstOrNull()
|
||||
if (answer != null) {
|
||||
cache[obj::class] = answer
|
||||
answer
|
||||
} else {
|
||||
DefaultHtmlBuilder
|
||||
}
|
||||
} else {
|
||||
value
|
||||
}
|
||||
}
|
||||
context.launch(Dispatchers.Output) {
|
||||
(builder as HtmlBuilder<T>).run { consumer.render(obj) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A text or binary renderer based on [kotlinx.io.core.Output]
|
||||
*/
|
||||
@Type(HTML_CONVERTER_TYPE)
|
||||
interface HtmlBuilder<T : Any> {
|
||||
/**
|
||||
* The priority of this renderer compared to other renderers
|
||||
*/
|
||||
val priority: Int
|
||||
|
||||
/**
|
||||
* The type of the content served by this renderer
|
||||
*/
|
||||
val type: KClass<T>
|
||||
|
||||
suspend fun TagConsumer<*>.render(obj: T)
|
||||
|
||||
companion object {
|
||||
const val HTML_CONVERTER_TYPE = "dataforge.htmlBuilder"
|
||||
}
|
||||
}
|
||||
|
||||
object DefaultHtmlBuilder : HtmlBuilder<Any> {
|
||||
override val priority: Int = Int.MAX_VALUE
|
||||
override val type: KClass<Any> = Any::class
|
||||
|
||||
override suspend fun TagConsumer<*>.render(obj: Any) {
|
||||
p { +obj.toString() }
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package hep.dataforge.io
|
||||
package hep.dataforge.output
|
||||
|
||||
import hep.dataforge.context.ContextAware
|
||||
import hep.dataforge.meta.EmptyMeta
|
@ -1,4 +1,4 @@
|
||||
package hep.dataforge.io
|
||||
package hep.dataforge.output
|
||||
|
||||
import hep.dataforge.context.AbstractPlugin
|
||||
import hep.dataforge.context.Context
|
@ -1,7 +1,7 @@
|
||||
package hep.dataforge.io
|
||||
package hep.dataforge.output
|
||||
|
||||
import hep.dataforge.context.Context
|
||||
import hep.dataforge.io.TextRenderer.Companion.TEXT_RENDERER_TYPE
|
||||
import hep.dataforge.output.TextRenderer.Companion.TEXT_RENDERER_TYPE
|
||||
import hep.dataforge.meta.Meta
|
||||
import hep.dataforge.provider.Type
|
||||
import hep.dataforge.provider.provideAll
|
@ -1,4 +1,4 @@
|
||||
package hep.dataforge.io
|
||||
package hep.dataforge.output
|
||||
|
||||
import hep.dataforge.context.Context
|
||||
import hep.dataforge.context.Global
|
@ -1,4 +1,4 @@
|
||||
package hep.dataforge.io
|
||||
package hep.dataforge.output
|
||||
|
||||
import hep.dataforge.context.Global
|
||||
import kotlinx.coroutines.Dispatchers
|
@ -25,6 +25,7 @@ include(
|
||||
":dataforge-context",
|
||||
":dataforge-data",
|
||||
":dataforge-output",
|
||||
":dataforge-output:dataforge-output-html",
|
||||
":dataforge-workspace",
|
||||
":dataforge-scripting"
|
||||
)
|
Loading…
Reference in New Issue
Block a user