@@ -20,7 +20,7 @@ private extension Array {
20
20
21
21
public extension Writer {
22
22
/// 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 {
24
24
return Writer ( run: { items, allItems, fileStorage, outputRoot, outputPrefix, fileIO in
25
25
try await withThrowingTaskGroup ( of: Void . self) { group in
26
26
for item in items {
@@ -30,7 +30,7 @@ public extension Writer {
30
30
. filter { $0. relativePath. parent ( ) == item. relativeSource. parent ( ) && !$0. handled }
31
31
. map { $0. path }
32
32
let context = ItemRenderingContext ( item: item, items: items, allItems: allItems, resources: resources)
33
- let stringToWrite = try renderer ( context)
33
+ let stringToWrite = try await renderer ( context)
34
34
try fileIO. write ( outputRoot + item. relativeDestination, stringToWrite)
35
35
}
36
36
}
@@ -40,7 +40,7 @@ public extension Writer {
40
40
}
41
41
42
42
/// 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 {
44
44
return Writer ( run: { items, allItems, fileStorage, outputRoot, outputPrefix, fileIO in
45
45
try await writePages ( renderer: renderer, items: items, allItems: allItems, outputRoot: outputRoot, outputPrefix: outputPrefix, output: output, paginate: paginate, paginatedOutput: paginatedOutput, fileIO: fileIO) {
46
46
ItemsRenderingContext ( items: $0, allItems: $1, paginator: $2, outputPath: $3)
@@ -54,7 +54,7 @@ public extension Writer {
54
54
///
55
55
/// The `output` path is a template where `[key]` will be replaced with the key used for the partition.
56
56
/// 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 {
58
58
return Writer ( run: { items, allItems, fileStorage, outputRoot, outputPrefix, fileIO in
59
59
let partitions = partitioner ( items)
60
60
@@ -74,7 +74,7 @@ public extension Writer {
74
74
}
75
75
76
76
/// 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 {
78
78
let partitioner : ( [ Item < M > ] ) -> [ Int : [ Item < M > ] ] = { items in
79
79
var itemsPerYear = [ Int : [ Item < M > ] ] ( )
80
80
@@ -97,7 +97,7 @@ public extension Writer {
97
97
/// A convenience version of `partitionedWriter` that splits items based on tags.
98
98
///
99
99
/// 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 {
101
101
let partitioner : ( [ Item < M > ] ) -> [ String : [ Item < M > ] ] = { items in
102
102
var itemsPerTag = [ String : [ Item < M > ] ] ( )
103
103
@@ -120,7 +120,7 @@ public extension Writer {
120
120
}
121
121
122
122
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 {
124
124
if let perPage = paginate {
125
125
let ranges = items. chunked ( into: perPage)
126
126
let numberOfPages = ranges. count
@@ -138,7 +138,7 @@ private extension Writer {
138
138
)
139
139
140
140
let context = getContext ( firstItems, allItems, paginator, outputPrefix + output)
141
- let stringToWrite = try renderer ( context)
141
+ let stringToWrite = try await renderer ( context)
142
142
try fileIO. write ( outputRoot + outputPrefix + output, stringToWrite)
143
143
}
144
144
@@ -160,15 +160,15 @@ private extension Writer {
160
160
161
161
let finishedOutputPath = Path ( paginatedOutput. string. replacingOccurrences ( of: " [page] " , with: " \( currentPage) " ) )
162
162
let context = getContext ( items, allItems, paginator, outputPrefix + finishedOutputPath)
163
- let stringToWrite = try renderer ( context)
163
+ let stringToWrite = try await renderer ( context)
164
164
try fileIO. write ( outputRoot + outputPrefix + finishedOutputPath, stringToWrite)
165
165
}
166
166
}
167
167
try await group. waitForAll ( )
168
168
}
169
169
} else {
170
170
let context = getContext ( items, allItems, nil , outputPrefix + output)
171
- let stringToWrite = try renderer ( context)
171
+ let stringToWrite = try await renderer ( context)
172
172
try fileIO. write ( outputRoot + outputPrefix + output, stringToWrite)
173
173
}
174
174
}
0 commit comments