Skip to content

Commit

Permalink
Kitura/Kitura#675 removed extraneous function arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
vadimeisenbergibm committed Aug 15, 2016
1 parent f18c7cc commit 3b99ca1
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 126 deletions.
6 changes: 3 additions & 3 deletions Sources/Box.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ extension Collection {
info.enumerationItem = true

for item in self {
let boxRendering = try box(item).render(info: info)
let boxRendering = try box(item).render(info)
if contentType == nil
{
// First item: now we know our contentType
Expand Down Expand Up @@ -1491,7 +1491,7 @@ public func Box<T>(_ dictionary: [String: T]?) -> MustacheBox {
if let dictionary = dictionary {
return MustacheBox(
converter: MustacheBox.Converter(
dictionaryValue: dictionary.reduce([String: MustacheBox](), combine: { (boxDictionary, item: (key: String, value: T)) in
dictionaryValue: dictionary.reduce([String: MustacheBox](), { (boxDictionary, item: (key: String, value: T)) in
var boxDictionary = boxDictionary
boxDictionary[item.key] = BoxAny(item.value)
return boxDictionary
Expand Down Expand Up @@ -1651,7 +1651,7 @@ GRMustache provides built-in support for rendering `NSDictionary`.
}
}
#else
let dictionary = IteratorSequence(NSFastEnumerationIterator(value)).reduce([String: MustacheBox](), combine: { (boxDictionary, key) in
let dictionary = IteratorSequence(NSFastEnumerationIterator(value)).reduce([String: MustacheBox](), { (boxDictionary, key) in
var boxDictionary = boxDictionary
if let key = key as? String {
boxDictionary[key] = BoxAny(value[key as NSString])
Expand Down
2 changes: 1 addition & 1 deletion Sources/CoreFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public func Filter(_ filter: (Rendering) throws -> Rendering) -> FilterFunction
}
// Box a RenderFunction
return Box { (info: RenderingInfo) in
let rendering = try box.render(info: info)
let rendering = try box.render(info)
return try filter(rendering)
}
}
Expand Down
18 changes: 9 additions & 9 deletions Sources/EachFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
import Foundation

let EachFilter = Filter { (box: MustacheBox) -> MustacheBox in

// {{# each(nothing) }}...{{/ }}
if box.isEmpty {
return box
}

// {{# each(dictionary) }}...{{/ }}
//
// // Renders "firstName: Charles, lastName: Bronson."
Expand All @@ -54,17 +54,17 @@ let EachFilter = Filter { (box: MustacheBox) -> MustacheBox in
position["@first"] = Box(index == 0)
position["@last"] = Box((index == count - 1))
position["@key"] = Box(element.key)

var info = info
info.context = info.context.extendedContext(by: Box(position))
return try element.box.render(info: info)
return try element.box.render(info)
}
return Box(customRenderFunction)
}
return Box(transformedBoxes)
}


// {{# each(array) }}...{{/ }}
//
// // Renders "1: bread, 2: ham, 3: butter"
Expand All @@ -87,16 +87,16 @@ let EachFilter = Filter { (box: MustacheBox) -> MustacheBox in
position["@indexIsEven"] = Box(index % 2 == 0)
position["@first"] = Box(index == 0)
position["@last"] = Box((index == count - 1))

var info = info
info.context = info.context.extendedContext(by: Box(position))
return try box.render(info: info)
return try box.render(info)
}
return Box(customRenderFunction)
}
return Box(transformedBoxes)
}

// Non-iterable value
throw MustacheError(kind: .RenderError, message: "Non-enumerable argument in each filter: \(box.value)")
}
22 changes: 11 additions & 11 deletions Sources/ExpressionInvocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,43 +25,43 @@ import Foundation

struct ExpressionInvocation {
let expression: Expression

func invoke(with context: Context) throws -> MustacheBox {
return try evaluate(context: context, expression: expression)
}

private func evaluate(context: Context, expression: Expression) throws -> MustacheBox {
switch expression {
case .ImplicitIterator:
// {{ . }}

return context.topBox

case .Identifier(let identifier):
// {{ identifier }}

return context.mustacheBox(forKey: identifier)

case .Scoped(let baseExpression, let identifier):
// {{ <expression>.identifier }}

return try evaluate(context: context, expression: baseExpression).mustacheBox(forKey: identifier)

case .Filter(let filterExpression, let argumentExpression, let partialApplication):
// {{ <expression>(<expression>) }}

let filterBox = try evaluate(context: context, expression: filterExpression)

guard let filter = filterBox.filter else {
if filterBox.isEmpty {
throw MustacheError(kind: .RenderError, message: "Missing filter")
} else {
throw MustacheError(kind: .RenderError, message: "Not a filter")
}
}

let argumentBox = try evaluate(context: context, expression: argumentExpression)
return try filter(box: argumentBox, partialApplication: partialApplication)
return try filter(argumentBox, partialApplication)
}
}
}
14 changes: 7 additions & 7 deletions Sources/HTMLEscapeHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@
import Foundation

final class HTMLEscapeHelper : MustacheBoxable {

var mustacheBox: MustacheBox {
// Return a multi-facetted box, because HTMLEscape interacts in
// various ways with Mustache rendering.
return MustacheBox(
// It has a value:
value: self,

// HTMLEscape can be used as a filter: {{ HTMLEscape(x) }}:
filter: Filter(filter),

// HTMLEscape escapes all variable tags: {{# HTMLEscape }}...{{ x }}...{{/ HTMLEscape }}
willRender: willRender)
}

// This function is used for evaluating `HTMLEscape(x)` expressions.
private func filter(rendering: Rendering) throws -> Rendering {
return Rendering(escape(html: rendering.string), rendering.contentType)
}

// A WillRenderFunction: this function lets HTMLEscape change values that
// are about to be rendered to their escaped counterpart.
//
Expand All @@ -56,15 +56,15 @@ final class HTMLEscapeHelper : MustacheBoxable {
// We don't know if the box contains a String, so let's escape its
// rendering.
return Box({ (info: RenderingInfo) -> Rendering in
let rendering = try box.render(info: info)
let rendering = try box.render(info)
return try self.filter(rendering: rendering)
})
case .Section:
// {{# value }}...{{/ value }}
// {{^ value }}...{{/ value }}
// Leave sections untouched, so that loops and conditions are not
// affected by the formatter.

return box
}
}
Expand Down
16 changes: 8 additions & 8 deletions Sources/JavascriptEscapeHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@
import Foundation

final class JavascriptEscapeHelper : MustacheBoxable {

var mustacheBox: MustacheBox {
// Return a multi-facetted box, because JavascriptEscape interacts in
// various ways with Mustache rendering.
return MustacheBox(
// It has a value:
value: self,

// JavascriptEscape can be used as a filter: {{ javascriptEscape(x) }}:
filter: Filter(filter),

// JavascriptEscape escapes all variable tags: {{# javascriptEscape }}...{{ x }}...{{/ javascriptEscape }}
willRender: willRender)
}

// This function is used for evaluating `javascriptEscape(x)` expressions.
private func filter(rendering: Rendering) throws -> Rendering {
return Rendering(JavascriptEscapeHelper.escape(javascript: rendering.string), rendering.contentType)
}

// A WillRenderFunction: this function lets JavascriptEscape change values that
// are about to be rendered to their escaped counterpart.
//
Expand All @@ -55,14 +55,14 @@ final class JavascriptEscapeHelper : MustacheBoxable {
// We don't know if the box contains a String, so let's escape its
// rendering.
return Box({ (info: RenderingInfo) -> Rendering in
let rendering = try box.render(info: info)
let rendering = try box.render(info)
return try self.filter(rendering: rendering)
})
case .Section:
return box
}
}

private class func escape(javascript string: String) -> String {
// This table comes from https://github.com/django/django/commit/8c4a525871df19163d5bfdf5939eff33b544c2e2#django/template/defaultfilters.py
//
Expand All @@ -83,7 +83,7 @@ final class JavascriptEscapeHelper : MustacheBoxable {
//
// The initial django commit used `\xNN` syntax. The \u syntax was
// introduced later for JSON compatibility.

let escapeTable: [Character: String] = [
"\0": "\\u0000",
"\u{01}": "\\u0001",
Expand Down
Loading

0 comments on commit 3b99ca1

Please sign in to comment.