Shuffle some code around

This commit is contained in:
Sami Samhuri 2020-01-01 21:46:58 -08:00
parent 3bd2ff0a12
commit b277106ed5
5 changed files with 27 additions and 16 deletions

View file

@ -20,8 +20,24 @@ final class JSONFeedWriter {
self.fileWriter = fileWriter
}
func writeFeed(_ posts: [Post], for site: Site, to targetURL: URL, with renderer: JSONFeedRendering) throws {
let feed = Feed(
func writeFeed(site: Site, posts: [Post], to targetURL: URL, with renderer: JSONFeedRendering) throws {
let feed = try buildFeed(site: site, posts: posts, renderer: renderer)
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .iso8601
#if os(Linux)
encoder.outputFormatting = [.prettyPrinted]
#else
encoder.outputFormatting = [.prettyPrinted, .withoutEscapingSlashes]
#endif
let feedJSON = try encoder.encode(feed)
let feedURL = targetURL.appendingPathComponent(jsonFeed.path)
try fileWriter.write(data: feedJSON, to: feedURL)
}
}
private extension JSONFeedWriter {
func buildFeed(site: Site, posts: [Post], renderer: JSONFeedRendering) throws -> Feed {
Feed(
title: site.title,
home_page_url: site.url.absoluteString,
feed_url: site.url.appendingPathComponent(jsonFeed.path).absoluteString,
@ -46,16 +62,6 @@ final class JSONFeedWriter {
)
}
)
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .iso8601
#if os(Linux)
encoder.outputFormatting = [.prettyPrinted]
#else
encoder.outputFormatting = [.prettyPrinted, .withoutEscapingSlashes]
#endif
let feedJSON = try encoder.encode(feed)
let feedURL = targetURL.appendingPathComponent(jsonFeed.path)
try fileWriter.write(data: feedJSON, to: feedURL)
}
}

View file

@ -41,6 +41,6 @@ struct PostsByYear {
/// Returns an array of all posts.
func flattened() -> [Post] {
byYear.values.flatMap { $0.byMonth.values.flatMap { $0.posts } }
byYear.values.flatMap { $0.flattened() }
}
}

View file

@ -32,4 +32,9 @@ struct YearPosts {
byMonth[month] = newValue
}
}
/// Returns an array of all posts.
func flattened() -> [Post] {
byMonth.values.flatMap { $0.posts }
}
}

View file

@ -50,7 +50,7 @@ final class PostsPlugin: Plugin {
try postWriter.writeArchive(posts: postRepo.posts, for: site, to: targetURL, with: renderer)
try postWriter.writeYearIndexes(posts: postRepo.posts, for: site, to: targetURL, with: renderer)
try postWriter.writeMonthRollups(posts: postRepo.posts, for: site, to: targetURL, with: renderer)
try jsonFeedWriter?.writeFeed(postRepo.postsForFeed, for: site, to: targetURL, with: renderer)
try rssFeedWriter?.writeFeed(postRepo.postsForFeed, for: site, to: targetURL, with: renderer)
try jsonFeedWriter?.writeFeed(site: site, posts: postRepo.postsForFeed, to: targetURL, with: renderer)
try rssFeedWriter?.writeFeed(site: site, posts: postRepo.postsForFeed, to: targetURL, with: renderer)
}
}

View file

@ -20,7 +20,7 @@ final class RSSFeedWriter {
self.fileWriter = fileWriter
}
func writeFeed(_ posts: [Post], for site: Site, to targetURL: URL, with renderer: RSSFeedRendering) throws {
func writeFeed(site: Site, posts: [Post], to targetURL: URL, with renderer: RSSFeedRendering) throws {
let feedURL = site.url.appendingPathComponent(rssFeed.path)
let feedXML = try renderer.renderRSSFeed(posts: posts, feedURL: feedURL, site: site)
let feedFileURL = targetURL.appendingPathComponent(rssFeed.path)