@@ -3,7 +3,9 @@ package org.jetbrains.kotlinx.jupyter.api.libraries
3
3
import org.jetbrains.kotlinx.jupyter.api.AfterCellExecutionCallback
4
4
import org.jetbrains.kotlinx.jupyter.api.ClassAnnotationHandler
5
5
import org.jetbrains.kotlinx.jupyter.api.ClassDeclarationsCallback
6
+ import org.jetbrains.kotlinx.jupyter.api.Code
6
7
import org.jetbrains.kotlinx.jupyter.api.CodeCell
8
+ import org.jetbrains.kotlinx.jupyter.api.CodePreprocessor
7
9
import org.jetbrains.kotlinx.jupyter.api.ExecutionCallback
8
10
import org.jetbrains.kotlinx.jupyter.api.FieldHandler
9
11
import org.jetbrains.kotlinx.jupyter.api.FieldHandlerByClass
@@ -54,6 +56,8 @@ abstract class JupyterIntegration : LibraryDefinitionProducer {
54
56
55
57
private val repositories = mutableListOf<String >()
56
58
59
+ private val codePreprocessors = mutableListOf<CodePreprocessor >()
60
+
57
61
fun addRenderer (handler : RendererTypeHandler ) {
58
62
renderers.add(handler)
59
63
}
@@ -70,6 +74,10 @@ abstract class JupyterIntegration : LibraryDefinitionProducer {
70
74
fileAnnotations.add(handler)
71
75
}
72
76
77
+ fun addCodePreprocessor (preprocessor : CodePreprocessor ) {
78
+ codePreprocessors.add(preprocessor)
79
+ }
80
+
73
81
inline fun <reified T : Any > render (noinline renderer : CodeCell .(T ) -> Any ) {
74
82
return renderWithHost { _, value: T -> renderer(this , value) }
75
83
}
@@ -152,6 +160,18 @@ abstract class JupyterIntegration : LibraryDefinitionProducer {
152
160
addFileAnnotationHanlder(FileAnnotationHandler (T ::class , callback))
153
161
}
154
162
163
+ fun preprocessCodeWithLibraries (callback : KotlinKernelHost .(Code ) -> CodePreprocessor .Result ) {
164
+ addCodePreprocessor(object : CodePreprocessor {
165
+ override fun process (code : String , host : KotlinKernelHost ): CodePreprocessor .Result {
166
+ return host.callback(code)
167
+ }
168
+ })
169
+ }
170
+
171
+ fun preprocessCode (callback : KotlinKernelHost .(Code ) -> Code ) {
172
+ preprocessCodeWithLibraries { CodePreprocessor .Result (this .callback(it)) }
173
+ }
174
+
155
175
internal fun getDefinition () =
156
176
LibraryDefinitionImpl (
157
177
init = init ,
@@ -166,6 +186,7 @@ abstract class JupyterIntegration : LibraryDefinitionProducer {
166
186
classAnnotations = classAnnotations,
167
187
fileAnnotations = fileAnnotations,
168
188
resources = resources,
189
+ codePreprocessors = codePreprocessors,
169
190
)
170
191
}
171
192
0 commit comments