1
0
forked from SPC/spc-site

Complete refactor to new routing API

This commit is contained in:
Alexander Nozik 2022-06-24 17:23:08 +03:00
parent c15a0ea948
commit 218e76a2a8
No known key found for this signature in database
GPG Key ID: F7FCF2DD25C71357
5 changed files with 71 additions and 51 deletions

View File

@ -2,6 +2,7 @@ package ru.mipt.spc
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.html.* import kotlinx.html.*
import space.kscience.dataforge.data.DataTree
import space.kscience.dataforge.data.await import space.kscience.dataforge.data.await
import space.kscience.dataforge.data.getByType import space.kscience.dataforge.data.getByType
import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.Meta
@ -280,9 +281,9 @@ private val HtmlData.mentorPageId get() = "mentor-${id}"
internal fun SiteBuilder.spcMaster(dataPath: Path, prefix: Name = "magprog".asName()) { internal fun SiteBuilder.spcMaster(dataPath: Path, prefix: Name = "magprog".asName()) {
val magProgSiteContext = snark.readDirectory(dataPath.resolve("content")) val magProgData: DataTree<Any> = snark.readDirectory(dataPath.resolve("content"))
route(prefix, magProgSiteContext, setAsRoot = true) { route(prefix, magProgData, setAsRoot = true) {
assetDirectory("assets", dataPath.resolve("assets")) assetDirectory("assets", dataPath.resolve("assets"))
assetDirectory("images", dataPath.resolve("images")) assetDirectory("images", dataPath.resolve("images"))
@ -356,7 +357,6 @@ internal fun SiteBuilder.spcMaster(dataPath: Path, prefix: Name = "magprog".asNa
magProgFooter() magProgFooter()
} }
} }
}
val mentors = data.findByContentType("magprog_mentor").values.sortedBy { val mentors = data.findByContentType("magprog_mentor").values.sortedBy {
@ -410,4 +410,5 @@ internal fun SiteBuilder.spcMaster(dataPath: Path, prefix: Name = "magprog".asNa
} }
} }
} }
}
} }

View File

@ -150,7 +150,7 @@ context(PageBuilder) private fun HTML.spcHome() {
header("major") { header("major") {
h3 { h3 {
a(classes = "link") { a(classes = "link") {
href = resolvePageRef("magprog") href = resolvePageRef("magprog.index")
+"""Master's program""" +"""Master's program"""
} }
} }

View File

@ -19,6 +19,8 @@ import space.kscience.dataforge.data.DataTree
import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.Meta
import space.kscience.dataforge.meta.withDefault import space.kscience.dataforge.meta.withDefault
import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.Name
import space.kscience.dataforge.names.cutLast
import space.kscience.dataforge.names.endsWith
import java.nio.file.Path import java.nio.file.Path
import kotlin.contracts.InvocationKind import kotlin.contracts.InvocationKind
import kotlin.contracts.contract import kotlin.contracts.contract
@ -57,7 +59,11 @@ internal class KtorSiteBuilder(
override fun resolveRef(ref: String): String = resolveRef(pageBaseUrl, ref) override fun resolveRef(ref: String): String = resolveRef(pageBaseUrl, ref)
override fun resolvePageRef(pageName: Name): String = resolveRef(pageName.tokens.joinToString(separator = "/")) override fun resolvePageRef(pageName: Name): String = if(pageName.endsWith(SiteBuilder.INDEX_PAGE_TOKEN)){
resolveRef(pageName.cutLast().tokens.joinToString(separator = "/"))
} else {
resolveRef(pageName.tokens.joinToString(separator = "/"))
}
} }
override fun page(route: Name, content: context(PageBuilder, HTML)() -> Unit) { override fun page(route: Name, content: context(PageBuilder, HTML)() -> Unit) {

View File

@ -25,7 +25,7 @@ interface PageBuilder : ContextAware {
fun PageBuilder.resolvePageRef(pageName: String) = resolvePageRef(pageName.parseAsName()) fun PageBuilder.resolvePageRef(pageName: String) = resolvePageRef(pageName.parseAsName())
val PageBuilder.homeRef get() = resolvePageRef(Name.EMPTY) val PageBuilder.homeRef get() = resolvePageRef(Name.EMPTY).removeSuffix("/")
/** /**
* Resolve a Html builder by its full name * Resolve a Html builder by its full name

View File

@ -56,19 +56,23 @@ internal class StaticSiteBuilder(
javaClass.getResource(resourcesPath)?.let { Path.of(it.toURI()) }?.copyRecursively(path) javaClass.getResource(resourcesPath)?.let { Path.of(it.toURI()) }?.copyRecursively(path)
} }
private fun resolveRef(baseUrl: String, ref: String) = if (baseUrl.isEmpty()) {
ref
} else {
"${baseUrl.removeSuffix("/")}/$ref"
}
inner class StaticPageBuilder : PageBuilder { inner class StaticPageBuilder : PageBuilder {
override val data: DataTree<*> get() = this@StaticSiteBuilder.data override val data: DataTree<*> get() = this@StaticSiteBuilder.data
override val meta: Meta get() = this@StaticSiteBuilder.meta override val meta: Meta get() = this@StaticSiteBuilder.meta
override val context: Context get() = this@StaticSiteBuilder.context override val context: Context get() = this@StaticSiteBuilder.context
override fun resolveRef(ref: String): String { override fun resolveRef(ref: String): String = resolveRef(baseUrl, ref)
TODO("Not yet implemented")
}
override fun resolvePageRef(pageName: Name): String { override fun resolvePageRef(pageName: Name): String = resolveRef(
TODO("Not yet implemented") pageName.tokens.joinToString(separator = "/", postfix = ".html")
} )
} }
@ -98,14 +102,23 @@ internal class StaticSiteBuilder(
snark = snark, snark = snark,
data = dataOverride ?: data, data = dataOverride ?: data,
meta = metaOverride?.withDefault(meta) ?: meta, meta = metaOverride?.withDefault(meta) ?: meta,
baseUrl = baseUrl, baseUrl = if (setAsRoot) {
resolveRef(baseUrl, routeName.toWebPath())
} else {
baseUrl
},
path = path.resolve(routeName.toWebPath()) path = path.resolve(routeName.toWebPath())
) )
} }
fun SnarkPlugin.static(outputPath: Path, data: DataTree<*> = DataTree.empty(), block: SiteBuilder.() -> Unit) { fun SnarkPlugin.static(
outputPath: Path,
data: DataTree<*> = DataTree.empty(),
siteUrl: String = outputPath.absolutePathString(),
block: SiteBuilder.() -> Unit,
) {
contract { contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE) callsInPlace(block, InvocationKind.EXACTLY_ONCE)
} }
StaticSiteBuilder(this, data, meta, "", outputPath).block() StaticSiteBuilder(this, data, meta, siteUrl, outputPath).block()
} }