Added sections tests. Allow for searching of context stack
This commit is contained in:
@@ -2,37 +2,45 @@
|
||||
/// Protocol for objects that can be rendered as a sequence in Mustache
|
||||
public protocol HBMustacheSequence {
|
||||
/// Render section using template
|
||||
func renderSection(with template: HBMustacheTemplate) -> String
|
||||
func renderSection(with template: HBMustacheTemplate, stack: [Any]) -> String
|
||||
/// Render inverted section using template
|
||||
func renderInvertedSection(with template: HBMustacheTemplate) -> String
|
||||
func renderInvertedSection(with template: HBMustacheTemplate, stack: [Any]) -> String
|
||||
}
|
||||
|
||||
public extension Sequence {
|
||||
/// Render section using template
|
||||
func renderSection(with template: HBMustacheTemplate) -> String {
|
||||
func renderSection(with template: HBMustacheTemplate, stack: [Any]) -> String {
|
||||
var string = ""
|
||||
var context = HBMustacheContext(first: true)
|
||||
|
||||
var iterator = makeIterator()
|
||||
guard var currentObject = iterator.next() else { return "" }
|
||||
|
||||
while let object = iterator.next() {
|
||||
string += template.render(currentObject, context: context)
|
||||
var stack = stack
|
||||
stack.append(currentObject)
|
||||
string += template.render(stack, context: context)
|
||||
currentObject = object
|
||||
context.first = false
|
||||
context.index += 1
|
||||
}
|
||||
|
||||
context.last = true
|
||||
string += template.render(currentObject, context: context)
|
||||
var stack = stack
|
||||
stack.append(currentObject)
|
||||
string += template.render(stack, context: context)
|
||||
|
||||
return string
|
||||
}
|
||||
|
||||
/// Render inverted section using template
|
||||
func renderInvertedSection(with template: HBMustacheTemplate) -> String {
|
||||
func renderInvertedSection(with template: HBMustacheTemplate, stack: [Any]) -> String {
|
||||
var stack = stack
|
||||
stack.append(self)
|
||||
|
||||
var iterator = makeIterator()
|
||||
if iterator.next() == nil {
|
||||
return template.render(self)
|
||||
return template.render(stack)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ extension HBMustacheTemplate {
|
||||
/// - object: Object
|
||||
/// - context: Context that render is occurring in. Contains information about position in sequence
|
||||
/// - Returns: Rendered text
|
||||
func render(_ object: Any, context: HBMustacheContext? = nil, indentation: String? = nil) -> String {
|
||||
func render(_ object: [Any], context: HBMustacheContext? = nil, indentation: String? = nil) -> String {
|
||||
var string = ""
|
||||
for token in tokens {
|
||||
if let indentation = indentation, string.last == "\n" {
|
||||
@@ -28,11 +28,11 @@ extension HBMustacheTemplate {
|
||||
}
|
||||
case let .section(variable, method, template):
|
||||
let child = getChild(named: variable, from: object, method: method, context: context)
|
||||
string += renderSection(child, parent: object, with: template)
|
||||
string += renderSection(child, stack: object, with: template)
|
||||
|
||||
case let .invertedSection(variable, method, template):
|
||||
let child = getChild(named: variable, from: object, method: method, context: context)
|
||||
string += renderInvertedSection(child, parent: object, with: template)
|
||||
string += renderInvertedSection(child, stack: object, with: template)
|
||||
|
||||
case let .partial(name, indentation):
|
||||
if let template = library?.getTemplate(named: name) {
|
||||
@@ -49,16 +49,16 @@ extension HBMustacheTemplate {
|
||||
/// - parent: Current object being rendered
|
||||
/// - template: Template to render with
|
||||
/// - Returns: Rendered text
|
||||
func renderSection(_ child: Any?, parent: Any, with template: HBMustacheTemplate) -> String {
|
||||
func renderSection(_ child: Any?, stack: [Any], with template: HBMustacheTemplate) -> String {
|
||||
switch child {
|
||||
case let array as HBMustacheSequence:
|
||||
return array.renderSection(with: template)
|
||||
return array.renderSection(with: template, stack: stack + [array])
|
||||
case let bool as Bool:
|
||||
return bool ? template.render(parent) : ""
|
||||
return bool ? template.render(stack) : ""
|
||||
case let lambda as HBMustacheLambda:
|
||||
return lambda.run(parent, template)
|
||||
return lambda.run(stack.last!, template)
|
||||
case let .some(value):
|
||||
return template.render(value)
|
||||
return template.render(stack + [value])
|
||||
case .none:
|
||||
return ""
|
||||
}
|
||||
@@ -70,33 +70,46 @@ extension HBMustacheTemplate {
|
||||
/// - parent: Current object being rendered
|
||||
/// - template: Template to render with
|
||||
/// - Returns: Rendered text
|
||||
func renderInvertedSection(_ child: Any?, parent: Any, with template: HBMustacheTemplate) -> String {
|
||||
func renderInvertedSection(_ child: Any?, stack: [Any], with template: HBMustacheTemplate) -> String {
|
||||
switch child {
|
||||
case let array as HBMustacheSequence:
|
||||
return array.renderInvertedSection(with: template)
|
||||
return array.renderInvertedSection(with: template, stack: stack)
|
||||
case let bool as Bool:
|
||||
return bool ? "" : template.render(parent)
|
||||
return bool ? "" : template.render(stack)
|
||||
case .some:
|
||||
return ""
|
||||
case .none:
|
||||
return template.render(parent)
|
||||
return template.render(stack)
|
||||
}
|
||||
}
|
||||
|
||||
/// Get child object from variable name
|
||||
func getChild(named name: String, from object: Any, method: String?, context: HBMustacheContext?) -> Any? {
|
||||
func _getChild(named names: ArraySlice<String>, from object: Any) -> Any? {
|
||||
guard let name = names.first else { return object }
|
||||
let childObject: Any?
|
||||
func getChild(named name: String, from stack: [Any], method: String?, context: HBMustacheContext?) -> Any? {
|
||||
func _getImmediateChild(named name: String, from object: Any) -> Any? {
|
||||
if let customBox = object as? HBMustacheParent {
|
||||
childObject = customBox.child(named: name)
|
||||
return customBox.child(named: name)
|
||||
} else {
|
||||
let mirror = Mirror(reflecting: object)
|
||||
childObject = mirror.getValue(forKey: name)
|
||||
return mirror.getValue(forKey: name)
|
||||
}
|
||||
guard childObject != nil else { return nil }
|
||||
}
|
||||
|
||||
func _getChild(named names: ArraySlice<String>, from object: Any) -> Any? {
|
||||
guard let name = names.first else { return object }
|
||||
guard let childObject = _getImmediateChild(named: name, from: object) else { return nil }
|
||||
let names2 = names.dropFirst()
|
||||
return _getChild(named: names2, from: childObject!)
|
||||
return _getChild(named: names2, from: childObject)
|
||||
}
|
||||
|
||||
func _getChildInStack(named names: ArraySlice<String>, from stack: [Any]) -> Any? {
|
||||
guard let name = names.first else { return stack.last }
|
||||
for object in stack.reversed() {
|
||||
if let childObject = _getImmediateChild(named: name, from: object) {
|
||||
let names2 = names.dropFirst()
|
||||
return _getChild(named: names2, from: childObject)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// work out which object to access. "." means the current object, if the variable name is ""
|
||||
@@ -104,12 +117,12 @@ extension HBMustacheTemplate {
|
||||
// the name is split by "." and we use mirror to get the correct child object
|
||||
let child: Any?
|
||||
if name == "." {
|
||||
child = object
|
||||
child = stack.last!
|
||||
} else if name == "", method != nil {
|
||||
child = context
|
||||
} else {
|
||||
let nameSplit = name.split(separator: ".").map { String($0) }
|
||||
child = _getChild(named: nameSplit[...], from: object)
|
||||
child = _getChildInStack(named: nameSplit[...], from: stack)
|
||||
}
|
||||
// if we want to run a method and the current child can have methods applied to it then
|
||||
// run method on the current child
|
||||
|
||||
@@ -11,7 +11,7 @@ public final class HBMustacheTemplate {
|
||||
/// - Parameter object: Object to render
|
||||
/// - Returns: Rendered text
|
||||
public func render(_ object: Any) -> String {
|
||||
render(object, context: nil)
|
||||
render([object], context: nil)
|
||||
}
|
||||
|
||||
internal init(_ tokens: [Token]) {
|
||||
|
||||
Reference in New Issue
Block a user