Keep pointer to Library in template

This commit is contained in:
Adam Fowler
2021-03-14 08:51:16 +00:00
parent aaf285154d
commit 06251739d7
6 changed files with 56 additions and 34 deletions

View File

@@ -1,2 +1,2 @@
public typealias HBMustacheLambda = (Any, HBMustacheTemplate, HBMustacheLibrary?) -> String public typealias HBMustacheLambda = (Any, HBMustacheTemplate) -> String

View File

@@ -10,6 +10,7 @@ public class HBMustacheLibrary {
} }
public func register(_ template: HBMustacheTemplate, named name: String) { public func register(_ template: HBMustacheTemplate, named name: String) {
template.setLibrary(self)
templates[name] = template templates[name] = template
} }
@@ -19,7 +20,7 @@ public class HBMustacheLibrary {
public func render(_ object: Any, withTemplateNamed name: String) -> String? { public func render(_ object: Any, withTemplateNamed name: String) -> String? {
guard let template = templates[name] else { return nil } guard let template = templates[name] else { return nil }
return template.render(object, library: self) return template.render(object)
} }
private var templates: [String: HBMustacheTemplate] private var templates: [String: HBMustacheTemplate]

View File

@@ -1,6 +1,6 @@
extension HBMustacheTemplate { extension HBMustacheTemplate {
func render(_ object: Any, library: HBMustacheLibrary? = nil, context: HBMustacheContext? = nil) -> String { func render(_ object: Any, context: HBMustacheContext? = nil) -> String {
var string = "" var string = ""
for token in tokens { for token in tokens {
switch token { switch token {
@@ -9,7 +9,7 @@ extension HBMustacheTemplate {
case .variable(let variable, let method): case .variable(let variable, let method):
if let child = getChild(named: variable, from: object, method: method) { if let child = getChild(named: variable, from: object, method: method) {
if let template = child as? HBMustacheTemplate { if let template = child as? HBMustacheTemplate {
string += template.render(object, library: library) string += template.render(object)
} else { } else {
string += htmlEscape(String(describing: child)) string += htmlEscape(String(describing: child))
} }
@@ -20,11 +20,11 @@ extension HBMustacheTemplate {
} }
case .section(let variable, let method, let template): case .section(let variable, let method, let template):
let child = getChild(named: variable, from: object, method: method) let child = getChild(named: variable, from: object, method: method)
string += renderSection(child, parent: object, with: template, library: library) string += renderSection(child, parent: object, with: template)
case .invertedSection(let variable, let method, let template): case .invertedSection(let variable, let method, let template):
let child = getChild(named: variable, from: object, method: method) let child = getChild(named: variable, from: object, method: method)
string += renderInvertedSection(child, parent: object, with: template, library: library) string += renderInvertedSection(child, parent: object, with: template)
case .partial(let name): case .partial(let name):
if let text = library?.render(object, withTemplateNamed: name) { if let text = library?.render(object, withTemplateNamed: name) {
@@ -35,31 +35,31 @@ extension HBMustacheTemplate {
return string return string
} }
func renderSection(_ child: Any?, parent: Any, with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String { func renderSection(_ child: Any?, parent: Any, with template: HBMustacheTemplate) -> String {
switch child { switch child {
case let array as HBSequence: case let array as HBSequence:
return array.renderSection(with: template, library: library) return array.renderSection(with: template)
case let bool as Bool: case let bool as Bool:
return bool ? template.render(parent, library: library) : "" return bool ? template.render(parent) : ""
case let lambda as HBMustacheLambda: case let lambda as HBMustacheLambda:
return lambda(parent, template, library) return lambda(parent, template)
case .some(let value): case .some(let value):
return template.render(value, library: library) return template.render(value)
case .none: case .none:
return "" return ""
} }
} }
func renderInvertedSection(_ child: Any?, parent: Any, with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String { func renderInvertedSection(_ child: Any?, parent: Any, with template: HBMustacheTemplate) -> String {
switch child { switch child {
case let array as HBSequence: case let array as HBSequence:
return array.renderInvertedSection(with: template, library: library) return array.renderInvertedSection(with: template)
case let bool as Bool: case let bool as Bool:
return bool ? "" : template.render(parent, library: library) return bool ? "" : template.render(parent)
case .some: case .some:
return "" return ""
case .none: case .none:
return template.render(parent, library: library) return template.render(parent)
} }
} }
@@ -127,56 +127,56 @@ extension Dictionary: HBMustacheParent where Key == String {
} }
protocol HBSequence { protocol HBSequence {
func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String func renderSection(with template: HBMustacheTemplate) -> String
func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String func renderInvertedSection(with template: HBMustacheTemplate) -> String
} }
extension Array: HBSequence { extension Array: HBSequence {
func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String { func renderSection(with template: HBMustacheTemplate) -> String {
var string = "" var string = ""
for obj in self { for obj in self {
string += template.render(obj, library: library) string += template.render(obj)
} }
return string return string
} }
func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String { func renderInvertedSection(with template: HBMustacheTemplate) -> String {
if count == 0 { if count == 0 {
return template.render(self, library: library) return template.render(self)
} }
return "" return ""
} }
} }
extension ReversedCollection: HBSequence { extension ReversedCollection: HBSequence {
func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String { func renderSection(with template: HBMustacheTemplate) -> String {
var string = "" var string = ""
for obj in self { for obj in self {
string += template.render(obj, library: library) string += template.render(obj)
} }
return string return string
} }
func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String { func renderInvertedSection(with template: HBMustacheTemplate) -> String {
if count == 0 { if count == 0 {
return template.render(self, library: library) return template.render(self)
} }
return "" return ""
} }
} }
extension EnumeratedSequence: HBSequence { extension EnumeratedSequence: HBSequence {
func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String { func renderSection(with template: HBMustacheTemplate) -> String {
var string = "" var string = ""
for obj in self { for obj in self {
string += template.render(obj, library: library) string += template.render(obj)
} }
return string return string
} }
func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String { func renderInvertedSection(with template: HBMustacheTemplate) -> String {
if self.underestimatedCount == 0 { if self.underestimatedCount == 0 {
return template.render(self, library: library) return template.render(self)
} }
return "" return ""
} }

View File

@@ -8,8 +8,20 @@ public class HBMustacheTemplate {
self.tokens = tokens self.tokens = tokens
} }
public func render(_ object: Any, library: HBMustacheLibrary? = nil) -> String { public func render(_ object: Any) -> String {
self.render(object, library: library, context: nil) self.render(object, context: nil)
}
internal func setLibrary(_ library: HBMustacheLibrary) {
self.library = library
for token in tokens {
switch token {
case .section(_, _, let template), .invertedSection(_, _, let template):
template.setLibrary(library)
default:
break
}
}
} }
enum Token { enum Token {
@@ -22,5 +34,6 @@ public class HBMustacheTemplate {
} }
let tokens: [Token] let tokens: [Token]
var library: HBMustacheLibrary?
} }

View File

@@ -4,7 +4,7 @@ import XCTest
final class LibraryTests: XCTestCase { final class LibraryTests: XCTestCase {
func testDirectoryLoad() throws { func testDirectoryLoad() throws {
let fs = FileManager() let fs = FileManager()
try fs.createDirectory(atPath: "./templates", withIntermediateDirectories: false) try fs.createDirectory(atPath: "templates", withIntermediateDirectories: false)
let mustache = "<test>{{#value}}<value>{{.}}</value>{{/value}}</test>" let mustache = "<test>{{#value}}<value>{{.}}</value>{{/value}}</test>"
let data = Data(mustache.utf8) let data = Data(mustache.utf8)
defer { XCTAssertNoThrow(try fs.removeItem(atPath: "templates")) } defer { XCTAssertNoThrow(try fs.removeItem(atPath: "templates")) }

View File

@@ -91,6 +91,7 @@ final class TemplateRendererTests: XCTestCase {
XCTAssertEqual(template.render(Test(test: .init(string: "sub"))), "test sub") XCTAssertEqual(template.render(Test(test: .init(string: "sub"))), "test sub")
} }
/// variables
func testMustacheManualExample1() throws { func testMustacheManualExample1() throws {
let template = try HBMustacheTemplate(string: """ let template = try HBMustacheTemplate(string: """
Hello {{name}} Hello {{name}}
@@ -108,6 +109,7 @@ final class TemplateRendererTests: XCTestCase {
""") """)
} }
/// test esacped and unescaped text
func testMustacheManualExample2() throws { func testMustacheManualExample2() throws {
let template = try HBMustacheTemplate(string: """ let template = try HBMustacheTemplate(string: """
*{{name}} *{{name}}
@@ -124,6 +126,7 @@ final class TemplateRendererTests: XCTestCase {
""") """)
} }
/// test boolean
func testMustacheManualExample3() throws { func testMustacheManualExample3() throws {
let template = try HBMustacheTemplate(string: """ let template = try HBMustacheTemplate(string: """
Shown. Shown.
@@ -138,6 +141,7 @@ final class TemplateRendererTests: XCTestCase {
""") """)
} }
/// test non-empty lists
func testMustacheManualExample4() throws { func testMustacheManualExample4() throws {
let template = try HBMustacheTemplate(string: """ let template = try HBMustacheTemplate(string: """
{{#repo}} {{#repo}}
@@ -153,12 +157,13 @@ final class TemplateRendererTests: XCTestCase {
""") """)
} }
/// test lambdas
func testMustacheManualExample5() throws { func testMustacheManualExample5() throws {
let template = try HBMustacheTemplate(string: """ let template = try HBMustacheTemplate(string: """
{{#wrapped}}{{name}} is awesome.{{/wrapped}} {{#wrapped}}{{name}} is awesome.{{/wrapped}}
""") """)
func wrapped(object: Any, template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String { func wrapped(object: Any, template: HBMustacheTemplate) -> String {
return "<b>\(template.render(object, library: library))</b>" return "<b>\(template.render(object))</b>"
} }
let object: [String: Any] = ["name": "Willy", "wrapped": wrapped] let object: [String: Any] = ["name": "Willy", "wrapped": wrapped]
XCTAssertEqual(template.render(object), """ XCTAssertEqual(template.render(object), """
@@ -166,6 +171,7 @@ final class TemplateRendererTests: XCTestCase {
""") """)
} }
/// test setting context object
func testMustacheManualExample6() throws { func testMustacheManualExample6() throws {
let template = try HBMustacheTemplate(string: """ let template = try HBMustacheTemplate(string: """
{{#person?}} {{#person?}}
@@ -179,6 +185,7 @@ final class TemplateRendererTests: XCTestCase {
""") """)
} }
/// test inverted sections
func testMustacheManualExample7() throws { func testMustacheManualExample7() throws {
let template = try HBMustacheTemplate(string: """ let template = try HBMustacheTemplate(string: """
{{#repo}} {{#repo}}
@@ -195,6 +202,7 @@ final class TemplateRendererTests: XCTestCase {
""") """)
} }
/// test comments
func testMustacheManualExample8() throws { func testMustacheManualExample8() throws {
let template = try HBMustacheTemplate(string: """ let template = try HBMustacheTemplate(string: """
<h1>Today{{! ignore me }}.</h1> <h1>Today{{! ignore me }}.</h1>