Skip to content

Commit abb2663

Browse files
committed
feat: Renderers can now optionally be async
1 parent baa3388 commit abb2663

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

Sources/Saga/Writer.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ private extension Array {
2020

2121
public extension Writer {
2222
/// Writes a single ``Item`` to a single output file, using `Item.destination` as the destination path.
23-
static func itemWriter(_ renderer: @escaping (ItemRenderingContext<M>) throws -> String) -> Self {
23+
static func itemWriter(_ renderer: @escaping (ItemRenderingContext<M>) async throws -> String) -> Self {
2424
return Writer(run: { items, allItems, fileStorage, outputRoot, outputPrefix, fileIO in
2525
try await withThrowingTaskGroup(of: Void.self) { group in
2626
for item in items {
@@ -30,7 +30,7 @@ public extension Writer {
3030
.filter { $0.relativePath.parent() == item.relativeSource.parent() && !$0.handled }
3131
.map { $0.path }
3232
let context = ItemRenderingContext(item: item, items: items, allItems: allItems, resources: resources)
33-
let stringToWrite = try renderer(context)
33+
let stringToWrite = try await renderer(context)
3434
try fileIO.write(outputRoot + item.relativeDestination, stringToWrite)
3535
}
3636
}
@@ -40,7 +40,7 @@ public extension Writer {
4040
}
4141

4242
/// Writes an array of items into a single output file.
43-
static func listWriter(_ renderer: @escaping (ItemsRenderingContext<M>) throws -> String, output: Path = "index.html", paginate: Int? = nil, paginatedOutput: Path = "page/[page]/index.html") -> Self {
43+
static func listWriter(_ renderer: @escaping (ItemsRenderingContext<M>) async throws -> String, output: Path = "index.html", paginate: Int? = nil, paginatedOutput: Path = "page/[page]/index.html") -> Self {
4444
return Writer(run: { items, allItems, fileStorage, outputRoot, outputPrefix, fileIO in
4545
try await writePages(renderer: renderer, items: items, allItems: allItems, outputRoot: outputRoot, outputPrefix: outputPrefix, output: output, paginate: paginate, paginatedOutput: paginatedOutput, fileIO: fileIO) {
4646
ItemsRenderingContext(items: $0, allItems: $1, paginator: $2, outputPath: $3)
@@ -54,7 +54,7 @@ public extension Writer {
5454
///
5555
/// The `output` path is a template where `[key]` will be replaced with the key used for the partition.
5656
/// Example: `articles/[key]/index.html`
57-
static func partitionedWriter<T>(_ renderer: @escaping (PartitionedRenderingContext<T, M>) throws -> String, output: Path = "[key]/index.html", paginate: Int? = nil, paginatedOutput: Path = "[key]/page/[page]/index.html", partitioner: @escaping ([Item<M>]) -> [T: [Item<M>]]) -> Self {
57+
static func partitionedWriter<T>(_ renderer: @escaping (PartitionedRenderingContext<T, M>) async throws -> String, output: Path = "[key]/index.html", paginate: Int? = nil, paginatedOutput: Path = "[key]/page/[page]/index.html", partitioner: @escaping ([Item<M>]) -> [T: [Item<M>]]) -> Self {
5858
return Writer(run: { items, allItems, fileStorage, outputRoot, outputPrefix, fileIO in
5959
let partitions = partitioner(items)
6060

@@ -74,7 +74,7 @@ public extension Writer {
7474
}
7575

7676
/// A convenience version of `partitionedWriter` that splits items based on year.
77-
static func yearWriter(_ renderer: @escaping (PartitionedRenderingContext<Int, M>) throws -> String, output: Path = "[key]/index.html", paginate: Int? = nil, paginatedOutput: Path = "[key]/page/[page]/index.html") -> Self {
77+
static func yearWriter(_ renderer: @escaping (PartitionedRenderingContext<Int, M>) async throws -> String, output: Path = "[key]/index.html", paginate: Int? = nil, paginatedOutput: Path = "[key]/page/[page]/index.html") -> Self {
7878
let partitioner: ([Item<M>]) -> [Int: [Item<M>]] = { items in
7979
var itemsPerYear = [Int: [Item<M>]]()
8080

@@ -97,7 +97,7 @@ public extension Writer {
9797
/// A convenience version of `partitionedWriter` that splits items based on tags.
9898
///
9999
/// Tags can be any `[String]` array.
100-
static func tagWriter(_ renderer: @escaping (PartitionedRenderingContext<String, M>) throws -> String, output: Path = "tag/[key]/index.html", paginate: Int? = nil, paginatedOutput: Path = "tag/[key]/page/[page]/index.html", tags: @escaping (Item<M>) -> [String]) -> Self {
100+
static func tagWriter(_ renderer: @escaping (PartitionedRenderingContext<String, M>) async throws -> String, output: Path = "tag/[key]/index.html", paginate: Int? = nil, paginatedOutput: Path = "tag/[key]/page/[page]/index.html", tags: @escaping (Item<M>) -> [String]) -> Self {
101101
let partitioner: ([Item<M>]) -> [String: [Item<M>]] = { items in
102102
var itemsPerTag = [String: [Item<M>]]()
103103

@@ -120,7 +120,7 @@ public extension Writer {
120120
}
121121

122122
private extension Writer {
123-
static func writePages<Context>(renderer: @escaping (Context) throws -> String, items: [Item<M>], allItems: [AnyItem], outputRoot: Path, outputPrefix: Path, output: Path, paginate: Int?, paginatedOutput: Path, fileIO: FileIO, getContext: @escaping ([Item<M>], [AnyItem], Paginator?, Path) -> Context) async throws {
123+
static func writePages<Context>(renderer: @escaping (Context) async throws -> String, items: [Item<M>], allItems: [AnyItem], outputRoot: Path, outputPrefix: Path, output: Path, paginate: Int?, paginatedOutput: Path, fileIO: FileIO, getContext: @escaping ([Item<M>], [AnyItem], Paginator?, Path) -> Context) async throws {
124124
if let perPage = paginate {
125125
let ranges = items.chunked(into: perPage)
126126
let numberOfPages = ranges.count
@@ -138,7 +138,7 @@ private extension Writer {
138138
)
139139

140140
let context = getContext(firstItems, allItems, paginator, outputPrefix + output)
141-
let stringToWrite = try renderer(context)
141+
let stringToWrite = try await renderer(context)
142142
try fileIO.write(outputRoot + outputPrefix + output, stringToWrite)
143143
}
144144

@@ -160,15 +160,15 @@ private extension Writer {
160160

161161
let finishedOutputPath = Path(paginatedOutput.string.replacingOccurrences(of: "[page]", with: "\(currentPage)"))
162162
let context = getContext(items, allItems, paginator, outputPrefix + finishedOutputPath)
163-
let stringToWrite = try renderer(context)
163+
let stringToWrite = try await renderer(context)
164164
try fileIO.write(outputRoot + outputPrefix + finishedOutputPath, stringToWrite)
165165
}
166166
}
167167
try await group.waitForAll()
168168
}
169169
} else {
170170
let context = getContext(items, allItems, nil, outputPrefix + output)
171-
let stringToWrite = try renderer(context)
171+
let stringToWrite = try await renderer(context)
172172
try fileIO.write(outputRoot + outputPrefix + output, stringToWrite)
173173
}
174174
}

0 commit comments

Comments
 (0)