From 4066712014617f2209e5627f4345908f51eb889d Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 26 Jun 2022 12:46:12 +0300 Subject: [PATCH] Final fix for static rendering --- src/main/kotlin/ru/mipt/spc/spcHome.kt | 4 ++-- .../kotlin/space/kscience/snark/SiteLayout.kt | 24 +++++++++++-------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/ru/mipt/spc/spcHome.kt b/src/main/kotlin/ru/mipt/spc/spcHome.kt index 1aef1de..2c22d09 100644 --- a/src/main/kotlin/ru/mipt/spc/spcHome.kt +++ b/src/main/kotlin/ru/mipt/spc/spcHome.kt @@ -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()) { data as Data - page { + page(name) { spcPageContent(data.meta) { htmlData(data) } diff --git a/src/main/kotlin/space/kscience/snark/SiteLayout.kt b/src/main/kotlin/space/kscience/snark/SiteLayout.kt index a964c71..f04f4f1 100644 --- a/src/main/kotlin/space/kscience/snark/SiteLayout.kt +++ b/src/main/kotlin/space/kscience/snark/SiteLayout.kt @@ -51,13 +51,15 @@ internal fun SiteBuilder.assetsFrom(rootMeta: Meta) { } } +typealias DataRenderer = SiteBuilder.(name: Name, data: Data) -> 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()) { - pages(item, dataRenderer) + } 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()) { - page { + page(name) { head { title = data.meta["title"].string ?: "Untitled page" }