org.jetbrains.kotlinx.jupyter.libraries.CssLibraryResourcesProcessor.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-jupyter-kernel Show documentation
Show all versions of kotlin-jupyter-kernel Show documentation
Kotlin Jupyter kernel published as artifact
package org.jetbrains.kotlinx.jupyter.libraries
import org.jetbrains.kotlinx.jupyter.api.libraries.LibraryResource
import org.jetbrains.kotlinx.jupyter.api.libraries.ResourceFallbacksBundle
import org.jetbrains.kotlinx.jupyter.api.libraries.ResourcePathType
import org.jetbrains.kotlinx.jupyter.common.getHttp
import org.jetbrains.kotlinx.jupyter.common.text
import java.io.File
import java.io.IOException
class CssLibraryResourcesProcessor : LibraryResourcesProcessor {
private fun loadCssAsText(bundle: ResourceFallbacksBundle, classLoader: ClassLoader): String {
val exceptions = mutableListOf()
for (resourceLocation in bundle.locations) {
val path = resourceLocation.path
fun wrapInTag(text: String) = """
""".trimIndent()
return try {
when (resourceLocation.type) {
ResourcePathType.URL -> """
""".trimIndent()
ResourcePathType.URL_EMBEDDED -> wrapInTag(getHttp(path).text)
ResourcePathType.LOCAL_PATH -> wrapInTag(File(path).readText())
ResourcePathType.CLASSPATH_PATH -> wrapInTag(classLoader.getResource(path)?.readText().orEmpty())
}
} catch (e: IOException) {
exceptions.add(e)
continue
}
}
throw Exception("No resource fallback found! Related exceptions: $exceptions")
}
override fun wrapLibrary(resource: LibraryResource, classLoader: ClassLoader): String {
return resource.bundles.joinToString("\n") { loadCssAsText(it, classLoader) }
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy