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

org.jetbrains.kotlinx.jupyter.libraries.CssLibraryResourcesProcessor.kt Maven / Gradle / Ivy

Go to download

Implementation of REPL compiler and preprocessor for Jupyter dialect of Kotlin (IDE-compatible)

There is a newer version: 0.12.0-290
Show newest version
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.HttpClient
import org.jetbrains.kotlinx.jupyter.common.getHttp
import java.io.File
import java.io.IOException

class CssLibraryResourcesProcessor(
    private val httpClient: HttpClient,
) : 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(httpClient.getHttp(path).text)
                    ResourcePathType.LOCAL_PATH -> wrapInTag(File(path).readText())
                    ResourcePathType.CLASSPATH_PATH -> wrapInTag(loadResourceFromClassLoader(path, classLoader))
                }
            } 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 - 2024 Weber Informatics LLC | Privacy Policy