Update to DataForge 0.8

This commit is contained in:
Alexander Nozik 2024-02-19 19:47:37 +03:00
parent 72ac07db5a
commit 671ae6357f
7 changed files with 21 additions and 47 deletions

2
.gitignore vendored
View File

@ -1,7 +1,7 @@
.gradle/
build/
.idea/
/logs/
logs/
!gradle/wrapper/gradle-wrapper.jar
/deploy.ps1

View File

@ -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

View File

@ -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<Any>.directory(path: Path, name: Name = Name.EMPTY) {
node(name, monitorRawDirectory(path))
}
context(IOPlugin)
@OptIn(DFExperimental::class)
fun DataSourceBuilder<Any>.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 {

View File

@ -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<Name, Data<PageFragment>> =
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<Name, Data<PageFragment>> = siteData.resolveAllHtml { name, meta -> contentFilter(name, meta) }
val meta = body.meta
page(pageName) {

View File

@ -234,7 +234,7 @@ internal val spcHomePage = HtmlPage {
}
private fun SiteContextWithData.allPagesIn(location: String){
siteData.filterByType<PageFragment> { name, meta ->
siteData.filterByType<PageFragment> { name, meta, _ ->
name.startsWith(location) && meta["type"].string == "page"
}.forEach { (name, content) ->
page(name, content = spcPage(content))

View File

@ -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 <T : Any> DataSet<T>.contentFor(branchName: String): DataTree<T> = 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 <reified T> DataTree<T>.contentFor(branchName: String): DataTree<T> = 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 {

View File

@ -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<String>) = 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)