Skip to content

Commit

Permalink
feat: support throwing renderers
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinrenskers committed Feb 27, 2021
1 parent 2cc40d0 commit 032132d
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions Sources/Saga/Writer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ private extension Array {

public extension Writer {
/// Writes a single Item to a single output file, using Item.destination as the destination path
static func itemWriter(_ renderer: @escaping (ItemRenderingContext<M, SiteMetadata>) -> String) -> Self {
static func itemWriter(_ renderer: @escaping (ItemRenderingContext<M, SiteMetadata>) throws -> String) -> Self {
Writer { items, allItems, siteMetadata, outputRoot, outputPrefix, fileIO in
for item in items {
let context = ItemRenderingContext(item: item, items: items, allItems: allItems, siteMetadata: siteMetadata)
let node = renderer(context)
let node = try renderer(context)
try fileIO.write(outputRoot + item.relativeDestination, node)
}
}
}

/// Writes an array of Items into a single output file.
/// As such, it needs an output path, for example "articles/index.html".
static func listWriter(_ renderer: @escaping (ItemsRenderingContext<M, SiteMetadata>) -> String, output: Path = "index.html", paginate: Int? = nil, paginatedOutput: Path = "page/[page]/index.html") -> Self {
static func listWriter(_ renderer: @escaping (ItemsRenderingContext<M, SiteMetadata>) throws -> String, output: Path = "index.html", paginate: Int? = nil, paginatedOutput: Path = "page/[page]/index.html") -> Self {
return Self { items, allItems, siteMetadata, outputRoot, outputPrefix, fileIO in
try writePages(renderer: renderer, items: items, allItems: allItems, siteMetadata: siteMetadata, outputRoot: outputRoot, outputPrefix: outputPrefix, output: output, paginate: paginate, paginatedOutput: paginatedOutput, fileIO: fileIO) {
return ItemsRenderingContext(items: $0, allItems: $1, siteMetadata: $2, paginator: $3)
Expand All @@ -62,7 +62,7 @@ public extension Writer {
/// Use this to partition an array of Items into a dictionary of Items, with a custom key.
/// The output path is a template where [key] will be replaced with the key uses for the partition.
/// Example: "articles/[key]/index.html"
static func partitionedWriter<T>(_ renderer: @escaping (PartitionedRenderingContext<T, M, SiteMetadata>) -> String, output: Path = "[key]/index.html", paginate: Int? = nil, paginatedOutput: Path = "[key]/page/[page]/index.html", partitioner: @escaping ([Item<M>]) -> [T: [Item<M>]]) -> Self {
static func partitionedWriter<T>(_ renderer: @escaping (PartitionedRenderingContext<T, M, SiteMetadata>) 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 {
return Self { items, allItems, siteMetadata, outputRoot, outputPrefix, fileIO in
let partitions = partitioner(items)

Expand All @@ -77,7 +77,7 @@ public extension Writer {
}

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

Expand All @@ -99,7 +99,7 @@ public extension Writer {

/// A convenience version of partitionedWriter that splits Items based on tags.
/// (tags can be any [String] array)
static func tagWriter(_ renderer: @escaping (PartitionedRenderingContext<String, M, SiteMetadata>) -> String, output: Path = "tag/[key]/index.html", paginate: Int? = nil, paginatedOutput: Path = "tag/[key]/page/[page]/index.html", tags: @escaping (Item<M>) -> [String]) -> Self {
static func tagWriter(_ renderer: @escaping (PartitionedRenderingContext<String, M, SiteMetadata>) 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 {
let partitioner: ([Item<M>]) -> [String: [Item<M>]] = { items in
var itemsPerTag = [String: [Item<M>]]()

Expand All @@ -122,7 +122,7 @@ public extension Writer {
}

private extension Writer {
static func writePages<Context>(renderer: @escaping (Context) -> String, items: [Item<M>], allItems: [AnyItem], siteMetadata: SiteMetadata, outputRoot: Path, outputPrefix: Path, output: Path, paginate: Int?, paginatedOutput: Path, fileIO: FileIO, getContext: ([Item<M>], [AnyItem], SiteMetadata, Paginator?) -> Context) throws {
static func writePages<Context>(renderer: @escaping (Context) throws -> String, items: [Item<M>], allItems: [AnyItem], siteMetadata: SiteMetadata, outputRoot: Path, outputPrefix: Path, output: Path, paginate: Int?, paginatedOutput: Path, fileIO: FileIO, getContext: ([Item<M>], [AnyItem], SiteMetadata, Paginator?) -> Context) throws {
if let perPage = paginate {
let ranges = items.chunked(into: perPage)
let numberOfPages = ranges.count
Expand All @@ -139,7 +139,7 @@ private extension Writer {
)

let context = getContext(firstItems, allItems, siteMetadata, paginator)
let node = renderer(context)
let node = try renderer(context)
try fileIO.write(outputRoot + outputPrefix + output, node)
}

Expand All @@ -158,12 +158,12 @@ private extension Writer {

let finishedOutput = Path(paginatedOutput.string.replacingOccurrences(of: "[page]", with: "\(currentPage)"))
let context = getContext(items, allItems, siteMetadata, paginator)
let node = renderer(context)
let node = try renderer(context)
try fileIO.write(outputRoot + outputPrefix + finishedOutput, node)
}
} else {
let context = getContext(items, allItems, siteMetadata, nil)
let node = renderer(context)
let node = try renderer(context)
try fileIO.write(outputRoot + outputPrefix + output, node)
}
}
Expand Down

0 comments on commit 032132d

Please sign in to comment.