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.html.*
import space.kscience.dataforge.data.DataTree
import space.kscience.dataforge.data.await
import space.kscience.dataforge.data.getByType
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()) {
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("images", dataPath.resolve("images"))
@ -356,57 +357,57 @@ internal fun SiteBuilder.spcMaster(dataPath: Path, prefix: Name = "magprog".asNa
magProgFooter()
}
}
}
val mentors = data.findByContentType("magprog_mentor").values.sortedBy {
it.order
}
val mentors = data.findByContentType("magprog_mentor").values.sortedBy {
it.order
}
mentors.forEach { mentor ->
page(mentor.mentorPageId.asName()) {
mentors.forEach { mentor ->
page(mentor.mentorPageId.asName()) {
magProgHead("Научное программирование: ${mentor.name}")
body("is-preload") {
header {
id = "header"
a(classes = "title") {
href = "$homeRef#mentors"
+"Научные руководители"
}
nav {
ul {
mentors.forEach {
li {
a {
href = resolvePageRef(it.mentorPageId)
+it.name.substringAfterLast(" ")
magProgHead("Научное программирование: ${mentor.name}")
body("is-preload") {
header {
id = "header"
a(classes = "title") {
href = "$homeRef#mentors"
+"Научные руководители"
}
nav {
ul {
mentors.forEach {
li {
a {
href = resolvePageRef(it.mentorPageId)
+it.name.substringAfterLast(" ")
}
}
}
}
}
}
}
div {
id = "wrapper"
section("wrapper") {
id = "main"
div("inner") {
h1("major") { +mentor.name }
val imageClass = mentor.meta["image.position"].string ?: "left"
span("image $imageClass") {
mentor.imagePath?.let { photoPath ->
img(
src = resolveRef(photoPath),
alt = mentor.name
)
div {
id = "wrapper"
section("wrapper") {
id = "main"
div("inner") {
h1("major") { +mentor.name }
val imageClass = mentor.meta["image.position"].string ?: "left"
span("image $imageClass") {
mentor.imagePath?.let { photoPath ->
img(
src = resolveRef(photoPath),
alt = mentor.name
)
}
}
htmlData(mentor)
}
htmlData(mentor)
}
}
magProgFooter()
}
magProgFooter()
}
}
}

View File

@ -150,7 +150,7 @@ context(PageBuilder) private fun HTML.spcHome() {
header("major") {
h3 {
a(classes = "link") {
href = resolvePageRef("magprog")
href = resolvePageRef("magprog.index")
+"""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.withDefault
import space.kscience.dataforge.names.Name
import space.kscience.dataforge.names.cutLast
import space.kscience.dataforge.names.endsWith
import java.nio.file.Path
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
@ -57,7 +59,11 @@ internal class KtorSiteBuilder(
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) {

View File

@ -25,7 +25,7 @@ interface PageBuilder : ContextAware {
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

View File

@ -56,19 +56,23 @@ internal class StaticSiteBuilder(
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 {
override val data: DataTree<*> get() = this@StaticSiteBuilder.data
override val meta: Meta get() = this@StaticSiteBuilder.meta
override val context: Context get() = this@StaticSiteBuilder.context
override fun resolveRef(ref: String): String {
TODO("Not yet implemented")
}
override fun resolveRef(ref: String): String = resolveRef(baseUrl, ref)
override fun resolvePageRef(pageName: Name): String {
TODO("Not yet implemented")
}
override fun resolvePageRef(pageName: Name): String = resolveRef(
pageName.tokens.joinToString(separator = "/", postfix = ".html")
)
}
@ -98,14 +102,23 @@ internal class StaticSiteBuilder(
snark = snark,
data = dataOverride ?: data,
meta = metaOverride?.withDefault(meta) ?: meta,
baseUrl = baseUrl,
baseUrl = if (setAsRoot) {
resolveRef(baseUrl, routeName.toWebPath())
} else {
baseUrl
},
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 {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
StaticSiteBuilder(this, data, meta, "", outputPath).block()
StaticSiteBuilder(this, data, meta, siteUrl, outputPath).block()
}