All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlinx.jupyter.api.outputs.MetadataModifiers.kt Maven / Gradle / Ivy

There is a newer version: 0.12.0-335
Show newest version
package org.jetbrains.kotlinx.jupyter.api.outputs

import kotlinx.serialization.json.JsonObjectBuilder
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
import org.jetbrains.kotlinx.jupyter.api.MimeTypedResultEx
import org.jetbrains.kotlinx.jupyter.api.MimeTypes
import kotlin.reflect.KProperty

fun interface MetadataModifier {
    fun JsonObjectBuilder.modifyMetadata()
}

fun standardMetadataModifiers(
    isolatedHtml: Boolean = false,
    expandedJson: Boolean = false,
): List {
    return buildList {
        if (isolatedHtml) add(IsolatedHtmlMarker)
        if (expandedJson) add(ExpandedJsonMarker)
    }
}

object IsolatedHtmlMarker : MetadataModifier {
    private val marker =
        buildJsonObject {
            put("isolated", true)
        }

    override fun JsonObjectBuilder.modifyMetadata() {
        put(MimeTypes.HTML, marker)
    }
}

var MimeTypedResultEx.isIsolatedHtml by hasModifier(IsolatedHtmlMarker)

object ExpandedJsonMarker : MetadataModifier {
    private val marker =
        buildJsonObject {
            put("expanded", true)
        }

    override fun JsonObjectBuilder.modifyMetadata() {
        put(MimeTypes.JSON, marker)
    }
}

var MimeTypedResultEx.isExpandedJson by hasModifier(ExpandedJsonMarker)

fun hasModifier(modifier: MetadataModifier) = MetadataModifierIsSet(modifier)

class MetadataModifierIsSet(private val modifier: MetadataModifier) {
    operator fun getValue(
        result: MimeTypedResultEx,
        property: KProperty<*>,
    ): Boolean {
        return result.hasMetadataModifiers { it === modifier }
    }

    operator fun setValue(
        result: MimeTypedResultEx,
        property: KProperty<*>,
        value: Boolean,
    ) {
        if (value) {
            result.addMetadataModifier(modifier)
        } else {
            result.removeMetadataModifier(modifier)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy