1
0
forked from SPC/spc-site

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