Skip to content

Commit

Permalink
Add contents support to RSS template (#59)
Browse files Browse the repository at this point in the history
- Use full page bundle context for RSS feed items
- Now it is possible to use `{{& contents}}`.
  • Loading branch information
tib authored Dec 18, 2024
1 parent cfde038 commit 73f78fa
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
46 changes: 38 additions & 8 deletions Sources/ToucanSDK/Renderers/RSSRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -28,7 +56,7 @@ struct RSSRenderer {
title: item.title,
description: item.description,
publicationDate: item.date.rss,
userDefined: item.baseContext
userDefined: contextStore.fullContext(for: item)
)
}

Expand All @@ -39,22 +67,24 @@ 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(
template: "rss",
with: rssCtx.context,
to:
destinationUrl
.appendingPathComponent(sourceConfig.config.contents.rss.output)
.appendingPathComponent(
source.sourceConfig.config.contents.rss.output
)
)
}
}
5 changes: 3 additions & 2 deletions Sources/ToucanSDK/Toucan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 73f78fa

Please sign in to comment.