Final fix for static rendering

This commit is contained in:
Alexander Nozik 2022-06-26 12:46:12 +03:00
parent d3a6ab658a
commit 4066712014
No known key found for this signature in database
GPG Key ID: F7FCF2DD25C71357
2 changed files with 16 additions and 12 deletions

View File

@ -53,10 +53,10 @@ context(PageBuilder) internal fun HTML.spcPageContent(
}
@Suppress("UNCHECKED_CAST")
internal val FortyDataRenderer: SiteBuilder.(Data<*>) -> Unit = { data ->
internal val FortyDataRenderer: DataRenderer = { name, data ->
if (data.type == typeOf<HtmlFragment>()) {
data as Data<HtmlFragment>
page {
page(name) {
spcPageContent(data.meta) {
htmlData(data)
}

View File

@ -51,13 +51,15 @@ internal fun SiteBuilder.assetsFrom(rootMeta: Meta) {
}
}
typealias DataRenderer = SiteBuilder.(name: Name, data: Data<Any>) -> Unit
/**
* Recursively renders the data items in [data]. If [LAYOUT_KEY] is defined in an item, use it to load
* layout from the context, otherwise render children nodes as name segments and individual data items using [dataRenderer].
*/
fun SiteBuilder.pages(
data: DataTreeItem<*>,
dataRenderer: SiteBuilder.(Data<*>) -> Unit = SiteLayout.defaultDataRenderer,
dataRenderer: DataRenderer = SiteLayout.defaultDataRenderer,
) {
val layoutMeta = data.meta[LAYOUT_KEY]
if (layoutMeta != null) {
@ -70,14 +72,17 @@ fun SiteBuilder.pages(
//Don't apply index token
if (token == INDEX_PAGE_TOKEN) {
pages(item, dataRenderer)
}
route(token.toString()) {
} else if (item is DataTreeItem.Leaf) {
dataRenderer(this, token.asName(), item.data)
} else {
route(token.asName()) {
pages(item, dataRenderer)
}
}
}
}
is DataTreeItem.Leaf -> {
dataRenderer.invoke(this, data.data)
dataRenderer.invoke(this, Name.EMPTY, data.data)
}
}
data.meta[ASSETS_KEY]?.let {
@ -93,7 +98,7 @@ fun SiteBuilder.pages(
fun SiteBuilder.pages(
dataPath: Name,
remotePath: Name = dataPath,
dataRenderer: SiteBuilder.(Data<*>) -> Unit = SiteLayout.defaultDataRenderer,
dataRenderer: DataRenderer = SiteLayout.defaultDataRenderer,
) {
val item = data.getItem(dataPath) ?: error("No data found by name $dataPath")
route(remotePath) {
@ -104,13 +109,12 @@ fun SiteBuilder.pages(
fun SiteBuilder.pages(
dataPath: String,
remotePath: Name = dataPath.parseAsName(),
dataRenderer: SiteBuilder.(Data<*>) -> Unit = SiteLayout.defaultDataRenderer,
dataRenderer: DataRenderer = SiteLayout.defaultDataRenderer,
) {
pages(dataPath.parseAsName(), remotePath, dataRenderer = dataRenderer)
}
@Type(SiteLayout.TYPE)
fun interface SiteLayout {
@ -122,9 +126,9 @@ fun interface SiteLayout {
const val ASSETS_KEY = "assets"
val INDEX_PAGE_TOKEN = NameToken("index")
val defaultDataRenderer: SiteBuilder.(Data<*>) -> Unit = { data: Data<*> ->
val defaultDataRenderer: SiteBuilder.(name: Name, data: Data<*>) -> Unit = { name: Name, data: Data<*> ->
if (data.type == typeOf<HtmlData>()) {
page {
page(name) {
head {
title = data.meta["title"].string ?: "Untitled page"
}