Implement parent context and proper language switch

This commit is contained in:
Alexander Nozik 2024-02-29 09:29:24 +03:00
parent eddeea8758
commit 395fea432e
6 changed files with 22 additions and 11 deletions

View File

@ -6,7 +6,7 @@ import space.kscience.dataforge.data.GenericDataTree
import space.kscience.dataforge.names.NameToken
import kotlin.reflect.KType
public class DataTreeWithDefault<T>(public val tree: DataTree<T>, val default: DataTree<T>) :
public class DataTreeWithDefault<T>(public val tree: DataTree<T>, public val default: DataTree<T>) :
DataTree<T> {
override val dataType: KType get() = tree.dataType

View File

@ -115,7 +115,7 @@ private class LanguageMapAction(val languages: Set<Language>) : AbstractAction<A
languageMapCache.forEach { (nodeName, languageMap) ->
val languageMapMeta = Meta {
languageMap.keys.forEach { language ->
set(language.key, (language.route + nodeName).toWebPath())
set(language.key, (language.route + nodeName).toString())
}
}
languageMap.forEach { (language, node) ->

View File

@ -56,7 +56,7 @@ public interface PageContext : SnarkContext {
*
* @param relative if true, add [SiteContext] route to the absolute page name
*/
public fun resolvePageRef(pageName: Name): String
public fun resolvePageRef(pageName: Name, targetSite: SiteContext = site): String
public fun resolveRelativePageRef(pageName: Name): String = resolvePageRef(pageRoute + pageName)
@ -66,8 +66,8 @@ context(PageContext)
public val page: PageContext
get() = this@PageContext
public fun PageContext.resolvePageRef(pageName: String): String =
resolvePageRef(pageName.parseAsName())
public fun PageContext.resolvePageRef(pageName: String, targetSite: SiteContext = site): String =
resolvePageRef(pageName.parseAsName(), targetSite)
public val PageContext.homeRef: String get() = resolvePageRef(Name.EMPTY)

View File

@ -15,6 +15,9 @@ import space.kscience.snark.SnarkContext
*/
@SnarkBuilder
public interface SiteContext : SnarkContext {
public val parent: SiteContext?
/**
* A context path segments for this site
*/

View File

@ -26,6 +26,7 @@ internal class StaticSiteContext(
private val baseUrl: Url,
override val path: List<String>,
override val siteRoute: Name,
override val parent: SiteContext?,
private val outputPath: Path,
) : SiteContext {
@ -98,8 +99,9 @@ internal class StaticSiteContext(
override val pageMeta: Meta,
) : PageContext {
override fun resolvePageRef(pageName: Name): String = resolveRef(
pageName.toWebPath() + ".html"
override fun resolvePageRef(pageName: Name, targetSite: SiteContext): String = resolveRef(
pageName.toWebPath() + ".html",
targetSite
)
}
@ -129,6 +131,7 @@ internal class StaticSiteContext(
baseUrl = baseUrl,
path = emptyList(),
siteRoute = route,
parent = parent,
outputPath = outputPath.resolve(route.toWebPath())
),
data ?: DataTree.EMPTY
@ -147,6 +150,7 @@ internal class StaticSiteContext(
baseUrl = baseUrl,
path = emptyList(),
siteRoute = route,
parent = this,
outputPath = outputPath.resolve(route.toWebPath())
),
data ?: DataTree.EMPTY
@ -174,7 +178,7 @@ public suspend fun SnarkHtml.staticSite(
content: HtmlSite,
) {
val siteContextWithData = SiteContextWithData(
StaticSiteContext(siteMeta, siteUrl, emptyList(), Name.EMPTY, outputPath),
StaticSiteContext(siteMeta, siteUrl, emptyList(), Name.EMPTY, null, outputPath),
data ?: DataTree.EMPTY
)
with(content) {

View File

@ -39,6 +39,7 @@ internal class KtorSiteContext(
override val siteMeta: Meta,
override val path: List<String>,
override val siteRoute: Name,
override val parent: SiteContext?,
private val ktorRoute: Route,
) : SiteContext, ContextAware {
@ -84,11 +85,12 @@ internal class KtorSiteContext(
override fun resolvePageRef(
pageName: Name,
targetSite: SiteContext,
): String {
return if (pageName.endsWith(SiteContext.INDEX_PAGE_TOKEN)) {
resolveRef(pageName.cutLast().toWebPath())
resolveRef(pageName.cutLast().toWebPath(), targetSite)
} else {
resolveRef(pageName.toWebPath())
resolveRef(pageName.toWebPath(), targetSite)
}
}
}
@ -135,6 +137,7 @@ internal class KtorSiteContext(
siteMeta = Laminate(siteMeta, this@KtorSiteContext.siteMeta),
path = path,
siteRoute = route,
parent,
ktorRoute = ktorRoute.createRouteFromPath(route.toWebPath())
),
data ?: DataTree.EMPTY
@ -153,6 +156,7 @@ internal class KtorSiteContext(
siteMeta = Laminate(siteMeta, this@KtorSiteContext.siteMeta),
path = path + route.tokens.map { it.toStringUnescaped() },
siteRoute = Name.EMPTY,
this,
ktorRoute = ktorRoute.createRouteFromPath(route.toWebPath())
),
data ?: DataTree.EMPTY
@ -174,7 +178,7 @@ public fun Route.site(
content: HtmlSite,
) {
val siteContext = SiteContextWithData(
KtorSiteContext(context, siteMeta, path = path, siteRoute = Name.EMPTY, this@Route),
KtorSiteContext(context, siteMeta, path = path, siteRoute = Name.EMPTY, null, this@Route),
data ?: DataTree.EMPTY
)
with(content) {