From 671ae6357fc5a23856ce7e896854fd6196695830 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 19 Feb 2024 19:47:37 +0300 Subject: [PATCH] Update to DataForge 0.8 --- .gitignore | 2 +- gradle.properties | 2 +- src/main/kotlin/center/sciprog/Application.kt | 28 ++----------------- .../kotlin/center/sciprog/spcCollection.kt | 12 ++++---- src/main/kotlin/center/sciprog/spcHome.kt | 2 +- src/main/kotlin/center/sciprog/spcSite.kt | 16 +++++------ .../kotlin/center/sciprog/staticRender.kt | 6 ++-- 7 files changed, 21 insertions(+), 47 deletions(-) diff --git a/.gitignore b/.gitignore index c0cdf6a..d4a4628 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ .gradle/ build/ .idea/ -/logs/ +logs/ !gradle/wrapper/gradle-wrapper.jar /deploy.ps1 diff --git a/gradle.properties b/gradle.properties index 936d461..0b1abbe 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ kotlin.code.style=official -toolsVersion=0.15.2-kotlin-1.9.21 +toolsVersion=0.15.2-kotlin-1.9.22 snarkVersion=0.1.0-dev-1 \ No newline at end of file diff --git a/src/main/kotlin/center/sciprog/Application.kt b/src/main/kotlin/center/sciprog/Application.kt index 0cf8574..9926172 100644 --- a/src/main/kotlin/center/sciprog/Application.kt +++ b/src/main/kotlin/center/sciprog/Application.kt @@ -11,14 +11,8 @@ import io.ktor.server.routing.get import io.ktor.server.routing.routing import space.kscience.dataforge.context.Context import space.kscience.dataforge.context.request -import space.kscience.dataforge.data.DataSourceBuilder -import space.kscience.dataforge.io.IOPlugin -import space.kscience.dataforge.misc.DFExperimental -import space.kscience.dataforge.names.Name -import space.kscience.dataforge.workspace.monitorRawDirectory -import space.kscience.dataforge.workspace.readResources +import space.kscience.dataforge.workspace.resources import space.kscience.snark.html.SnarkHtml -import space.kscience.snark.html.fill import space.kscience.snark.html.readSiteData import space.kscience.snark.ktor.site import java.nio.file.Path @@ -59,21 +53,6 @@ import java.nio.file.Path // } //} -context(IOPlugin) -@OptIn(DFExperimental::class) -fun DataSourceBuilder.directory(path: Path, name: Name = Name.EMPTY) { - node(name, monitorRawDirectory(path)) -} - -context(IOPlugin) -@OptIn(DFExperimental::class) -fun DataSourceBuilder.resource( - resource: String, - classLoader: ClassLoader = Thread.currentThread().contextClassLoader, -) { - fill(readResources(resource, classLoader = classLoader)) -} - @Suppress("unused") fun Application.spcModule() { @@ -92,10 +71,7 @@ fun Application.spcModule() { ) val siteData = snark.readSiteData(context) { - resource("common") - resource("home") - resource("magprog") - //directory(dataDirectory) + resources(snark.io, "common", "home", "magprog") } routing { diff --git a/src/main/kotlin/center/sciprog/spcCollection.kt b/src/main/kotlin/center/sciprog/spcCollection.kt index a5826a6..08f32da 100644 --- a/src/main/kotlin/center/sciprog/spcCollection.kt +++ b/src/main/kotlin/center/sciprog/spcCollection.kt @@ -7,7 +7,10 @@ import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.get import space.kscience.dataforge.meta.int import space.kscience.dataforge.meta.string -import space.kscience.dataforge.names.* +import space.kscience.dataforge.names.Name +import space.kscience.dataforge.names.NameToken +import space.kscience.dataforge.names.parseAsName +import space.kscience.dataforge.names.replaceLast import space.kscience.snark.html.* import kotlin.collections.component1 import kotlin.collections.component2 @@ -90,11 +93,8 @@ internal fun SiteContextWithData.spcSpotlight( contentFilter: (Name, Meta) -> Boolean, ) { val pageName = address.parseAsName() - val languagePrefix = languagePrefix - val body = siteData.resolveHtmlOrNull(languagePrefix + pageName) - ?: siteData.resolveHtmlOrNull(pageName) ?: error("Could not find body for $pageName") - val content: Map> = - siteData.resolveAllHtml { name, meta -> name.startsWith(languagePrefix) && contentFilter(name, meta) } + val body = siteData.resolveHtmlOrNull(pageName) ?: error("Could not find body for $pageName") + val content: Map> = siteData.resolveAllHtml { name, meta -> contentFilter(name, meta) } val meta = body.meta page(pageName) { diff --git a/src/main/kotlin/center/sciprog/spcHome.kt b/src/main/kotlin/center/sciprog/spcHome.kt index 819658c..954eb8c 100644 --- a/src/main/kotlin/center/sciprog/spcHome.kt +++ b/src/main/kotlin/center/sciprog/spcHome.kt @@ -234,7 +234,7 @@ internal val spcHomePage = HtmlPage { } private fun SiteContextWithData.allPagesIn(location: String){ - siteData.filterByType { name, meta -> + siteData.filterByType { name, meta, _ -> name.startsWith(location) && meta["type"].string == "page" }.forEach { (name, content) -> page(name, content = spcPage(content)) diff --git a/src/main/kotlin/center/sciprog/spcSite.kt b/src/main/kotlin/center/sciprog/spcSite.kt index acce458..cfad57b 100644 --- a/src/main/kotlin/center/sciprog/spcSite.kt +++ b/src/main/kotlin/center/sciprog/spcSite.kt @@ -1,17 +1,17 @@ package center.sciprog -import space.kscience.dataforge.data.* -import space.kscience.dataforge.misc.DFExperimental +import space.kscience.dataforge.data.DataTree +import space.kscience.dataforge.data.branch +import space.kscience.dataforge.data.putAll import space.kscience.dataforge.names.Name import space.kscience.snark.html.HtmlSite import space.kscience.snark.html.site -@OptIn(DFExperimental::class) -private fun DataSet.contentFor(branchName: String): DataTree = DataTree(dataType) { - populateFrom(branch(Name.of(branchName, "content"))) - node("common", branch("common")) - node("assets", branch(Name.of(branchName, "assets"))) - node("images", branch(Name.of(branchName, "images"))) +private inline fun DataTree.contentFor(branchName: String): DataTree = DataTree{ + putAll(branch(Name.of(branchName, "content")) ?: DataTree.EMPTY) + branch("common", branch("common") ?: DataTree.EMPTY) + branch("assets", branch(Name.of(branchName, "assets")) ?: DataTree.EMPTY) + branch("images", branch(Name.of(branchName, "images")) ?: DataTree.EMPTY) } val spcSite = HtmlSite { diff --git a/src/main/kotlin/center/sciprog/staticRender.kt b/src/main/kotlin/center/sciprog/staticRender.kt index d095c95..d39df02 100644 --- a/src/main/kotlin/center/sciprog/staticRender.kt +++ b/src/main/kotlin/center/sciprog/staticRender.kt @@ -3,6 +3,7 @@ package center.sciprog import kotlinx.coroutines.coroutineScope import space.kscience.dataforge.context.Context import space.kscience.dataforge.context.request +import space.kscience.dataforge.workspace.resources import space.kscience.snark.html.SnarkHtml import space.kscience.snark.html.readSiteData import space.kscience.snark.html.static.staticSite @@ -17,10 +18,7 @@ suspend fun main(args: Array) = coroutineScope { val snark = context.request(SnarkHtml) val siteData = snark.readSiteData(context) { - resource("common") - resource("home") - resource("magprog") - + resources(snark.io,"common", "home", "magprog") } snark.staticSite(siteData, Path.of(destinationPath), content = spcSite)