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 space.kscience.dataforge.names.NameToken
import kotlin.reflect.KType 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> { DataTree<T> {
override val dataType: KType get() = tree.dataType 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) -> languageMapCache.forEach { (nodeName, languageMap) ->
val languageMapMeta = Meta { val languageMapMeta = Meta {
languageMap.keys.forEach { language -> languageMap.keys.forEach { language ->
set(language.key, (language.route + nodeName).toWebPath()) set(language.key, (language.route + nodeName).toString())
} }
} }
languageMap.forEach { (language, node) -> 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 * @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) public fun resolveRelativePageRef(pageName: Name): String = resolvePageRef(pageRoute + pageName)
@ -66,8 +66,8 @@ context(PageContext)
public val page: PageContext public val page: PageContext
get() = this@PageContext get() = this@PageContext
public fun PageContext.resolvePageRef(pageName: String): String = public fun PageContext.resolvePageRef(pageName: String, targetSite: SiteContext = site): String =
resolvePageRef(pageName.parseAsName()) resolvePageRef(pageName.parseAsName(), targetSite)
public val PageContext.homeRef: String get() = resolvePageRef(Name.EMPTY) public val PageContext.homeRef: String get() = resolvePageRef(Name.EMPTY)

View File

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

View File

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

View File

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