Skip to content

Commit

Permalink
Enable filtering of podcast items using a predicate (#90)
Browse files Browse the repository at this point in the history
This change enables a Predicate to be passed to the generatePodcastFeed
publishing step, which lets the user conditionally decide which items
to include in the feed. This feature was previously added to standard
RSS feeds, but now podcast feeds also have the same capability.
  • Loading branch information
JohnSundell committed Jun 15, 2020
1 parent 5406df9 commit e77aab7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Sources/Publish/API/PublishingStep.swift
Original file line number Diff line number Diff line change
Expand Up @@ -400,17 +400,21 @@ public extension PublishingStep where Site.ItemMetadata: PodcastCompatibleWebsit
/// Note that all of the items within the given section must define `podcast`
/// and `audio` metadata, or an error will be thrown.
/// - parameter section: The section to generate a podcast feed for.
/// - parameter itemPredicate: A predicate used to determine whether to
/// include a given item within the generated feed (default: include all).
/// - parameter config: The configuration to use when generating the feed.
/// - parameter date: The date that should act as the build and publishing
/// date for the generated feed (default: the current date).
static func generatePodcastFeed(
for section: Site.SectionID,
itemPredicate: Predicate<Item<Site>>? = nil,
config: PodcastFeedConfiguration<Site>,
date: Date = Date()
) -> Self {
step(named: "Generate podcast feed") { context in
let generator = PodcastFeedGenerator(
sectionID: section,
itemPredicate: itemPredicate,
config: config,
context: context,
date: date
Expand Down
7 changes: 6 additions & 1 deletion Sources/Publish/Internal/PodcastFeedGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Plot

internal struct PodcastFeedGenerator<Site: Website> where Site.ItemMetadata: PodcastCompatibleWebsiteItemMetadata {
let sectionID: Site.SectionID
let itemPredicate: Predicate<Item<Site>>?
let config: PodcastFeedConfiguration<Site>
let context: PublishingContext<Site>
let date: Date
Expand All @@ -18,7 +19,11 @@ internal struct PodcastFeedGenerator<Site: Website> where Site.ItemMetadata: Pod
let cacheFile = try context.cacheFile(named: "feed")
let oldCache = try? cacheFile.read().decoded() as Cache
let section = context.sections[sectionID]
let items = section.items.sorted(by: { $0.date > $1.date })
var items = section.items.sorted(by: { $0.date > $1.date })

if let predicate = itemPredicate?.inverse() {
items.removeAll(where: predicate.matches)
}

if let date = context.lastGenerationDate, let cache = oldCache {
if cache.config == config, cache.itemCount == items.count {
Expand Down
23 changes: 23 additions & 0 deletions Tests/PublishTests/Tests/PodcastFeedGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ final class PodcastFeedGenerationTests: PublishTestCase {
XCTAssertFalse(feed.contains("Not included"))
}

func testOnlyIncludingItemsMatchingPredicate() throws {
let folder = try Folder.createTemporary()

try generateFeed(
in: folder,
itemPredicate: \.path == "one/a",
content: [
"one/a.md": """
\(makeStubbedAudioMetadata())
# Included
""",
"one/b.md": "# Not included"
]
)

let feed = try folder.file(at: "Output/feed.rss").readAsString()
XCTAssertTrue(feed.contains("Included"))
XCTAssertFalse(feed.contains("Not included"))
}

func testConvertingRelativeLinksToAbsolute() throws {
let folder = try Folder.createTemporary()

Expand Down Expand Up @@ -148,6 +168,7 @@ extension PodcastFeedGenerationTests {
static var allTests: Linux.TestList<PodcastFeedGenerationTests> {
[
("testOnlyIncludingSpecifiedSection", testOnlyIncludingSpecifiedSection),
("testOnlyIncludingItemsMatchingPredicate", testOnlyIncludingItemsMatchingPredicate),
("testConvertingRelativeLinksToAbsolute", testConvertingRelativeLinksToAbsolute),
("testItemPrefixAndSuffix", testItemPrefixAndSuffix),
("testReusingPreviousFeedIfNoItemsWereModified", testReusingPreviousFeedIfNoItemsWereModified),
Expand Down Expand Up @@ -190,6 +211,7 @@ private extension PodcastFeedGenerationTests {
func generateFeed(
in folder: Folder,
config: Configuration? = nil,
itemPredicate: Predicate<Item<Site>>? = nil,
generationSteps: [PublishingStep<Site>] = [
.addMarkdownFiles()
],
Expand All @@ -200,6 +222,7 @@ private extension PodcastFeedGenerationTests {
.group(generationSteps),
.generatePodcastFeed(
for: .one,
itemPredicate: itemPredicate,
config: config ?? makeConfigStub(),
date: date
)
Expand Down

0 comments on commit e77aab7

Please sign in to comment.