From 73f78fa56c7cce637fe44157ac66b355f7bcf155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tibor=20B=C3=B6decs?= Date: Wed, 18 Dec 2024 16:27:28 +0100 Subject: [PATCH] Add contents support to RSS template (#59) - Use full page bundle context for RSS feed items - Now it is possible to use `{{& contents}}`. --- Sources/ToucanSDK/Renderers/RSSRenderer.swift | 46 +++++++++++++++---- Sources/ToucanSDK/Toucan.swift | 5 +- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/Sources/ToucanSDK/Renderers/RSSRenderer.swift b/Sources/ToucanSDK/Renderers/RSSRenderer.swift index 845fa4c9..7e7a845d 100644 --- a/Sources/ToucanSDK/Renderers/RSSRenderer.swift +++ b/Sources/ToucanSDK/Renderers/RSSRenderer.swift @@ -6,14 +6,42 @@ // import Foundation +import Logging struct RSSRenderer { - let sourceConfig: SourceConfig + let source: Source let destinationUrl: URL let fileManager: FileManager let templateRenderer: MustacheToHTMLRenderer let pageBundles: [PageBundle] + let logger: Logger + + let contextStore: ContextStore + + init( + source: Source, + destinationUrl: URL, + fileManager: FileManager, + templateRenderer: MustacheToHTMLRenderer, + pageBundles: [PageBundle], + logger: Logger + ) { + self.source = source + self.destinationUrl = destinationUrl + self.fileManager = fileManager + self.templateRenderer = templateRenderer + self.pageBundles = pageBundles + self.logger = logger + + self.contextStore = .init( + sourceConfig: source.sourceConfig, + contentTypes: source.contentTypes, + pageBundles: source.pageBundles, + blockDirectives: source.blockDirectives, + logger: logger + ) + } func render() throws { guard !pageBundles.isEmpty else { @@ -28,7 +56,7 @@ struct RSSRenderer { title: item.title, description: item.description, publicationDate: item.date.rss, - userDefined: item.baseContext + userDefined: contextStore.fullContext(for: item) ) } @@ -39,14 +67,14 @@ struct RSSRenderer { ?? rssDateFormatter.string(from: .init()) let rssCtx = RSSContext( - title: sourceConfig.site.title, - description: sourceConfig.site.description, - baseUrl: sourceConfig.site.baseUrl, - language: sourceConfig.site.language, + title: source.sourceConfig.site.title, + description: source.sourceConfig.site.description, + baseUrl: source.sourceConfig.site.baseUrl, + language: source.sourceConfig.site.language, lastBuildDate: rssDateFormatter.string(from: .init()), publicationDate: publicationDate, items: items, - userDefined: sourceConfig.site.userDefined + userDefined: source.sourceConfig.site.userDefined ) try templateRenderer.render( @@ -54,7 +82,9 @@ struct RSSRenderer { with: rssCtx.context, to: destinationUrl - .appendingPathComponent(sourceConfig.config.contents.rss.output) + .appendingPathComponent( + source.sourceConfig.config.contents.rss.output + ) ) } } diff --git a/Sources/ToucanSDK/Toucan.swift b/Sources/ToucanSDK/Toucan.swift index 193b68e0..3c71f65c 100644 --- a/Sources/ToucanSDK/Toucan.swift +++ b/Sources/ToucanSDK/Toucan.swift @@ -152,11 +152,12 @@ public struct Toucan { try sitemapRenderer.render() let rssRenderer = RSSRenderer( - sourceConfig: source.sourceConfig, + source: source, destinationUrl: workDirUrl, fileManager: .default, templateRenderer: templateRenderer, - pageBundles: source.rssPageBundles() + pageBundles: source.rssPageBundles(), + logger: logger ) try rssRenderer.render()