Add support for proper lambdas (#48)

* Add support for proper lambdas

* Get rid of recursion

Remove renderSectionLambda as I can use renderUnescapedLambda for that.
This commit is contained in:
Adam Fowler
2024-09-19 17:17:50 +01:00
committed by GitHub
parent 8fba85e28c
commit 933fa3d60f
9 changed files with 249 additions and 44 deletions

View File

@@ -13,12 +13,14 @@
//===----------------------------------------------------------------------===//
/// Class holding Mustache template
public struct MustacheTemplate: Sendable {
public struct MustacheTemplate: Sendable, CustomStringConvertible {
/// Initialize template
/// - Parameter string: Template text
/// - Throws: MustacheTemplate.Error
public init(string: String) throws {
self.tokens = try Self.parse(string)
let template = try Self.parse(string)
self.tokens = template.tokens
self.text = string
self.filename = nil
}
@@ -54,12 +56,15 @@ public struct MustacheTemplate: Sendable {
return self.render(context: .init(object, library: library))
}
internal init(_ tokens: [Token]) {
internal init(_ tokens: [Token], text: String) {
self.tokens = tokens
self.filename = nil
self.text = text
}
enum Token: Sendable {
public var description: String { self.text }
enum Token: Sendable /* , CustomStringConvertible */ {
case text(String)
case variable(name: String, transforms: [String] = [])
case unescapedVariable(name: String, transforms: [String] = [])
@@ -73,5 +78,6 @@ public struct MustacheTemplate: Sendable {
}
var tokens: [Token]
let text: String
let filename: String?
}