Swiftpack.co - loopwerk/SagaSwimRenderer as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by loopwerk.
loopwerk/SagaSwimRenderer 0.7.0
A renderer for Saga that uses Swim to turn a RenderingContext into a String
⭐️ 3
🕓 21 weeks ago
macOS
.package(url: "https://github.com/loopwerk/SagaSwimRenderer.git", from: "0.7.0")

SagaSwimRenderer

A renderer for Saga that uses Swim to turn a RenderingContext into a String.

It comes with a free function named swim which takes a function that goes from RenderingContext to Node, and turns it into a function that goes from RenderingContext to String, which can then be plugged into Saga's writers — which take functions of signature (RenderingContext) -> String.

Example

The best example is Saga's Example project, although a simplified example app is included in this repo.

TLDR;

Package.swift

// swift-tools-version:5.5

import PackageDescription

let package = Package(
  name: "Example",
  platforms: [
    .macOS(.v12)
  ],
  dependencies: [
    .package(url: "https://github.com/loopwerk/Saga", from: "1.0.0"),
    .package(url: "https://github.com/loopwerk/SagaParsleyMarkdownReader", from: "0.5.0"),
    .package(url: "https://github.com/loopwerk/SagaSwimRenderer", from: "0.6.0")
  ],
  targets: [
    .target(
      name: "Example",
      dependencies: [
        "Saga",
        "SagaParsleyMarkdownReader",
        "SagaSwimRenderer"
      ]
    ),
  ]
)

main.swift:

import Saga
import SagaParsleyMarkdownReader
import SagaSwimRenderer

@main
struct Run {
  static func main() async throws {
    try await Saga(input: "content", output: "deploy")
      .register(
        metadata: EmptyMetadata.self,
        readers: [.parsleyMarkdownReader()],
        itemWriteMode: .keepAsFile,
        writers: [
          .itemWriter(swim(renderItem))
        ]
      )

      // Run the steps we registered above
      .run()

      // All the remaining files that were not parsed to markdown, so for example images, raw html files and css,
      // are copied as-is to the output folder.
      .staticFiles()
  }
}

And your renderItem template:

func renderItem(context: ItemRenderingContext<EmptyMetadata, SiteMetadata>) -> Node {
  html(lang: "en-US") {
    body {
      div(id: "item") {
        h1 { context.item.title }
        context.item.body
      }
    }
  }
}

GitHub

link
Stars: 3
Last commit: 21 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Dependencies

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics