Update language management
This commit is contained in:
parent
8746360f14
commit
35cd0e828a
@ -6,8 +6,8 @@ import space.kscience.dataforge.data.*
|
|||||||
import space.kscience.dataforge.meta.*
|
import space.kscience.dataforge.meta.*
|
||||||
import space.kscience.dataforge.names.*
|
import space.kscience.dataforge.names.*
|
||||||
import space.kscience.snark.SnarkBuilder
|
import space.kscience.snark.SnarkBuilder
|
||||||
import space.kscience.snark.html.Language.Companion.SITE_LANGUAGES_KEY
|
|
||||||
import space.kscience.snark.html.Language.Companion.SITE_LANGUAGE_KEY
|
import space.kscience.snark.html.Language.Companion.SITE_LANGUAGE_KEY
|
||||||
|
import space.kscience.snark.html.Language.Companion.SITE_LANGUAGE_MAP_KEY
|
||||||
import kotlin.reflect.typeOf
|
import kotlin.reflect.typeOf
|
||||||
|
|
||||||
|
|
||||||
@ -45,7 +45,7 @@ public class Language : Scheme() {
|
|||||||
|
|
||||||
public val SITE_LANGUAGE_KEY: Name = SiteContext.SITE_META_KEY + LANGUAGE_KEY
|
public val SITE_LANGUAGE_KEY: Name = SiteContext.SITE_META_KEY + LANGUAGE_KEY
|
||||||
|
|
||||||
public val SITE_LANGUAGES_KEY: Name = SiteContext.SITE_META_KEY + LANGUAGE_MAP_KEY
|
public val SITE_LANGUAGE_MAP_KEY: Name = SiteContext.SITE_META_KEY + LANGUAGE_MAP_KEY
|
||||||
|
|
||||||
public const val DEFAULT_LANGUAGE: String = "en"
|
public const val DEFAULT_LANGUAGE: String = "en"
|
||||||
|
|
||||||
@ -75,14 +75,16 @@ public class Language : Scheme() {
|
|||||||
|
|
||||||
public fun Language(prefix: String): Language = Language { this.prefix = prefix }
|
public fun Language(prefix: String): Language = Language { this.prefix = prefix }
|
||||||
|
|
||||||
public val SiteContext.languages: Map<String, Meta>
|
public val SiteContext.languageMap: Map<String, Language>
|
||||||
get() = siteMeta[SITE_LANGUAGES_KEY]?.items?.mapKeys { it.key.toStringUnescaped() } ?: emptyMap()
|
get() = siteMeta[SITE_LANGUAGE_MAP_KEY]?.items?.map {
|
||||||
|
it.key.toStringUnescaped() to Language.read(it.value)
|
||||||
|
}?.toMap() ?: emptyMap()
|
||||||
|
|
||||||
public val SiteContext.language: String
|
public val SiteContext.language: String
|
||||||
get() = siteMeta[SITE_LANGUAGE_KEY].string ?: Language.DEFAULT_LANGUAGE
|
get() = siteMeta[SITE_LANGUAGE_KEY].string ?: Language.DEFAULT_LANGUAGE
|
||||||
//
|
|
||||||
//public val SiteContext.languagePrefix: Name
|
public val SiteContext.languagePrefix: Name
|
||||||
// get() = languages[language]?.let { it[Language::prefix.name].string ?: language }?.parseAsName() ?: Name.EMPTY
|
get() = languageMap[language]?.let { it.prefix ?: language }?.parseAsName() ?: Name.EMPTY
|
||||||
|
|
||||||
|
|
||||||
private class LanguageBranchAction(val prefix: Name) : AbstractAction<Any, Any>(typeOf<Any>()) {
|
private class LanguageBranchAction(val prefix: Name) : AbstractAction<Any, Any>(typeOf<Any>()) {
|
||||||
@ -127,7 +129,7 @@ public fun SiteContext.multiLanguageSite(
|
|||||||
val prefix = language.prefix ?: languageKey
|
val prefix = language.prefix ?: languageKey
|
||||||
val languageSiteMeta = Meta {
|
val languageSiteMeta = Meta {
|
||||||
SITE_LANGUAGE_KEY put languageKey
|
SITE_LANGUAGE_KEY put languageKey
|
||||||
SITE_LANGUAGES_KEY put Meta {
|
SITE_LANGUAGE_MAP_KEY put Meta {
|
||||||
languageMap.forEach {
|
languageMap.forEach {
|
||||||
it.key put it.value
|
it.key put it.value
|
||||||
}
|
}
|
||||||
@ -151,11 +153,11 @@ public val PageContext.language: String
|
|||||||
/**
|
/**
|
||||||
* Mapping of language keys to other language versions of this page
|
* Mapping of language keys to other language versions of this page
|
||||||
*/
|
*/
|
||||||
public fun PageContext.getLanguageMap(): Map<String, Meta> =
|
public val PageContext.languageMap: Map<String, Meta>
|
||||||
pageMeta[Language.LANGUAGE_MAP_KEY]?.items?.mapKeys { it.key.toStringUnescaped() } ?: emptyMap()
|
get() = pageMeta[Language.LANGUAGE_MAP_KEY]?.items?.mapKeys { it.key.toStringUnescaped() } ?: emptyMap()
|
||||||
|
|
||||||
public fun PageContext.localisedPageRef(pageName: Name, relative: Boolean = false): String {
|
public fun PageContext.localisedPageRef(pageName: Name, relative: Boolean = false): String {
|
||||||
val prefix = getLanguageMap()[language]?.get(Language::prefix.name)?.string?.parseAsName() ?: Name.EMPTY
|
val prefix = languageMap[language]?.get(Language::prefix.name)?.string?.parseAsName() ?: Name.EMPTY
|
||||||
return resolvePageRef(prefix + pageName, relative)
|
return resolvePageRef(prefix + pageName, relative)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,9 +77,12 @@ public class Postprocessor<out R>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
context(PageContext)
|
context(PageContext)
|
||||||
public inline fun FlowContent.postprocess(block: FlowContent.() -> Unit) {
|
public inline fun FlowContent.postprocess(
|
||||||
|
processor: TextProcessor = WebPageTextProcessor(page),
|
||||||
|
block: FlowContent.() -> Unit,
|
||||||
|
) {
|
||||||
val fc = object : FlowContent by this {
|
val fc = object : FlowContent by this {
|
||||||
override val consumer: TagConsumer<*> = Postprocessor(page, this@postprocess.consumer)
|
override val consumer: TagConsumer<*> = Postprocessor(page, this@postprocess.consumer, processor)
|
||||||
}
|
}
|
||||||
fc.block()
|
fc.block()
|
||||||
}
|
}
|
@ -121,8 +121,11 @@ public class KtorSiteContext(
|
|||||||
"name" put route.toString()
|
"name" put route.toString()
|
||||||
"url" put url.buildString()
|
"url" put url.buildString()
|
||||||
}
|
}
|
||||||
val pageContext =
|
val pageContext = KtorPageContext(
|
||||||
KtorPageContext(this@KtorSiteContext, url.buildString(), Laminate(modifiedPageMeta, siteMeta))
|
site = this@KtorSiteContext,
|
||||||
|
pageBaseUrl = url.buildString(),
|
||||||
|
pageMeta = Laminate(modifiedPageMeta, siteMeta)
|
||||||
|
)
|
||||||
//render page in suspend environment
|
//render page in suspend environment
|
||||||
val html = HtmlPage.createHtmlString(pageContext, data, content)
|
val html = HtmlPage.createHtmlString(pageContext, data, content)
|
||||||
|
|
||||||
@ -157,7 +160,7 @@ public class KtorSiteContext(
|
|||||||
route = Name.EMPTY,
|
route = Name.EMPTY,
|
||||||
ktorRoute = ktorRoute.createRouteFromPath(route.toWebPath())
|
ktorRoute = ktorRoute.createRouteFromPath(route.toWebPath())
|
||||||
),
|
),
|
||||||
data?: DataTree.EMPTY
|
data ?: DataTree.EMPTY
|
||||||
)
|
)
|
||||||
with(content) {
|
with(content) {
|
||||||
with(siteContext) {
|
with(siteContext) {
|
||||||
@ -177,7 +180,7 @@ public fun Route.site(
|
|||||||
) {
|
) {
|
||||||
val siteContext = SiteContextWithData(
|
val siteContext = SiteContextWithData(
|
||||||
KtorSiteContext(context, siteMeta, baseUrl, route = Name.EMPTY, this@Route),
|
KtorSiteContext(context, siteMeta, baseUrl, route = Name.EMPTY, this@Route),
|
||||||
data?: DataTree.EMPTY
|
data ?: DataTree.EMPTY
|
||||||
)
|
)
|
||||||
with(content) {
|
with(content) {
|
||||||
with(siteContext) {
|
with(siteContext) {
|
||||||
|
Loading…
Reference in New Issue
Block a user