0001 import Foundation 0002 0003 public struct TemplateSyntaxError: ErrorType, Equatable, CustomStringConvertible { 0004 public let description
Include.swift:11 throw TemplateSyntaxError("'include' tag takes one argument, the template file to be included")Include.swift:23 throw TemplateSyntaxError("Template loader not in context")Include.swift:27 throw TemplateSyntaxError("'\(self.templateName)' could not be resolved as a string")Include.swift:32 throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)")Inheritence.swift:37 throw TemplateSyntaxError("'extends' takes one argument, the template file to be extended")Inheritence.swift:42 throw TemplateSyntaxError("'extends' cannot appear more than once in the same template")Inheritence.swift:64 throw TemplateSyntaxError("Template loader not in context")Inheritence.swift:68 throw TemplateSyntaxError("'\(self.templateName)' could not be resolved as a string")Inheritence.swift:73 throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)")Inheritence.swift:93 throw TemplateSyntaxError("'block' tag takes one argument, the template file to be included")Node.swift:11 public func ==(lhs:TemplateSyntaxError, rhs:TemplateSyntaxError) -> Bool {Node.swift:11 public func ==(lhs:TemplateSyntaxError, rhs:TemplateSyntaxError) -> Bool {Node.swift:87 throw TemplateSyntaxError("'now' tags may only have one argument: the format string `\(token.contents)`.")Node.swift:128 throw TemplateSyntaxError("'for' statements should use the following 'for x in y' `\(token.contents)`.")Node.swift:139 throw TemplateSyntaxError("`endfor` was not found.")Node.swift:189 throw TemplateSyntaxError("'if' statements should use the following 'if condition' `\(token.contents)`.")Node.swift:198 throw TemplateSyntaxError("`endif` was not found.")Node.swift:212 throw TemplateSyntaxError("'ifnot' statements should use the following 'if condition' `\(token.contents)`.")Node.swift:221 throw TemplateSyntaxError("`endif` was not found.")Parser.swift:57 throw TemplateSyntaxError("Unknown template tag '\(tag)'")Parser.swift:85 throw TemplateSyntaxError("Invalid filter '\(name)'")Variable.swift:13 throw TemplateSyntaxError("Variable tags must include at least 1 argument"):String 0005 0006 public init
Node.swift:7 self.description = descriptionNode.swift:12 return lhs.description == rhs.descriptionNode.swift:12 return lhs.description == rhs.description(_ description:String) { 0007 self.description = description 0008 } 0009 } 0010 0011 public func ==(lhs:TemplateSyntaxError, rhs:TemplateSyntaxError) -> Bool { 0012 return lhs.description == rhs.description 0013 } 0014 0015 public protocol NodeType
Include.swift:11 throw TemplateSyntaxError("'include' tag takes one argument, the template file to be included")Include.swift:23 throw TemplateSyntaxError("Template loader not in context")Include.swift:27 throw TemplateSyntaxError("'\(self.templateName)' could not be resolved as a string")Inheritence.swift:37 throw TemplateSyntaxError("'extends' takes one argument, the template file to be extended")Inheritence.swift:42 throw TemplateSyntaxError("'extends' cannot appear more than once in the same template")Inheritence.swift:64 throw TemplateSyntaxError("Template loader not in context")Inheritence.swift:68 throw TemplateSyntaxError("'\(self.templateName)' could not be resolved as a string")Inheritence.swift:73 throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)")Inheritence.swift:93 throw TemplateSyntaxError("'block' tag takes one argument, the template file to be included")Node.swift:87 throw TemplateSyntaxError("'now' tags may only have one argument: the format string `\(token.contents)`.")Node.swift:128 throw TemplateSyntaxError("'for' statements should use the following 'for x in y' `\(token.contents)`.")Node.swift:139 throw TemplateSyntaxError("`endfor` was not found.")Node.swift:189 throw TemplateSyntaxError("'if' statements should use the following 'if condition' `\(token.contents)`.")Node.swift:198 throw TemplateSyntaxError("`endif` was not found.")Node.swift:212 throw TemplateSyntaxError("'ifnot' statements should use the following 'if condition' `\(token.contents)`.")Node.swift:221 throw TemplateSyntaxError("`endif` was not found.")Parser.swift:57 throw TemplateSyntaxError("Unknown template tag '\(tag)'")Parser.swift:85 throw TemplateSyntaxError("Invalid filter '\(name)'")Variable.swift:13 throw TemplateSyntaxError("Variable tags must include at least 1 argument"){ 0016 /// Render the node in the given context 0017 func render
Include.swift:4 public class IncludeNode : NodeType {Include.swift:7 public class func parse(parser: TokenParser, token: Token) throws -> NodeType {Inheritence.swift:29 class ExtendsNode : NodeType {Inheritence.swift:33 class func parse(parser: TokenParser, token: Token) throws -> NodeType {Inheritence.swift:47 let nodes = blockNodes.reduce([String:BlockNode]()) { (accumulator, node:NodeType) -> [String:BlockNode] inInheritence.swift:85 class BlockNode : NodeType {Inheritence.swift:87 let nodes: [NodeType]Inheritence.swift:89 class func parse(parser: TokenParser, token: Token) throws -> NodeType {Inheritence.swift:102 init(name: String, nodes: [NodeType]) {Namespace.swift:2 public typealias TagParser = (TokenParser, Token) throws -> NodeTypeNode.swift:21 public func renderNodes(nodes:[NodeType], _ context:Context) throws -> String {Node.swift:25 public class SimpleNode : NodeType {Node.swift:37 public class TextNode : NodeType {Node.swift:53 public class VariableNode : NodeType {Node.swift:79 public class NowNode : NodeType {Node.swift:82 public class func parse(parser:TokenParser, token:Token) throws -> NodeType {Node.swift:118 public class ForNode : NodeType {Node.swift:121 let nodes:[NodeType]Node.swift:122 let emptyNodes: [NodeType]Node.swift:124 public class func parse(parser:TokenParser, token:Token) throws -> NodeType {Node.swift:134 var emptyNodes = [NodeType]()Node.swift:150 public init(variable:String, loopVariable:String, nodes:[NodeType], emptyNodes:[NodeType]) {Node.swift:150 public init(variable:String, loopVariable:String, nodes:[NodeType], emptyNodes:[NodeType]) {Node.swift:181 public class IfNode : NodeType {Node.swift:183 public let trueNodes:[NodeType]Node.swift:184 public let falseNodes:[NodeType]Node.swift:186 public class func parse(parser:TokenParser, token:Token) throws -> NodeType {Node.swift:192 var trueNodes = [NodeType]()Node.swift:193 var falseNodes = [NodeType]()Node.swift:209 public class func parse_ifnot(parser:TokenParser, token:Token) throws -> NodeType {Node.swift:215 var trueNodes = [NodeType]()Node.swift:216 var falseNodes = [NodeType]()Node.swift:232 public init(variable:String, trueNodes:[NodeType], falseNodes:[NodeType]) {Node.swift:232 public init(variable:String, trueNodes:[NodeType], falseNodes:[NodeType]) {Parser.swift:19 public typealias TagParser = (TokenParser, Token) throws -> NodeTypeParser.swift:30 public func parse() throws -> [NodeType] {Parser.swift:34 public func parse(parse_until:((parser:TokenParser, token:Token) -> (Bool))?) throws -> [NodeType] {Parser.swift:35 var nodes = [NodeType]()(context:Context) throws -> String 0018 } 0019 0020 /// Render the collection of nodes in the given context 0021 public func renderNodes
Node.swift:22 return try nodes.map { try $0.render(context) }.joinWithSeparator("")(nodes:[NodeType], _ context:Context) throws -> String { 0022 return try nodes.map { try $0.render(context) }.joinWithSeparator("") 0023 } 0024 0025 public class SimpleNode
Inheritence.swift:112 return try renderNodes(nodes, context)Node.swift:170 try renderNodes(nodes, context)Node.swift:176 try renderNodes(emptyNodes, context)Node.swift:253 output = try renderNodes(trueNodes, context)Node.swift:255 output = try renderNodes(falseNodes, context)Template.swift:42 return try renderNodes(nodes, context ?? Context()): NodeType { 0026 let handler
Namespace.swift:36 return SimpleNode(handler: handler):Context throws -> String 0027 0028 public init
Node.swift:29 self.handler = handlerNode.swift:33 return try handler(context)(handler:Context throws -> String) { 0029 self.handler = handler 0030 } 0031 0032 public func render(context: Context) throws -> String { 0033 return try handler(context) 0034 } 0035 } 0036 0037 public class TextNode
Namespace.swift:36 return SimpleNode(handler: handler): NodeType { 0038 public let text
Parser.swift:42 nodes.append(TextNode(text: text)):String 0039 0040 public init
Node.swift:41 self.text = textNode.swift:45 return self.text(text:String) { 0041 self.text = text 0042 } 0043 0044 public func render(context:Context) throws -> String { 0045 return self.text 0046 } 0047 } 0048 0049 public protocol Resolvable
Parser.swift:42 nodes.append(TextNode(text: text)){ 0050 func resolve
Node.swift:54 public let variable: ResolvableNode.swift:56 public init(variable: Resolvable) {Parser.swift:88 func compileFilter(token: String) throws -> Resolvable {Variable.swift:4 class FilterExpression : Resolvable {Variable.swift:37 public struct Variable : Equatable, Resolvable {(context: Context) throws -> Any? 0051 } 0052 0053 public class VariableNode
Node.swift:65 let result = try variable.resolve(context): NodeType { 0054 public let variable
Parser.swift:44 nodes.append(VariableNode(variable: try compileFilter(token.contents))): Resolvable 0055 0056 public init
Node.swift:57 self.variable = variableNode.swift:61 self.variable = Variable(variable)Node.swift:65 let result = try variable.resolve(context)(variable: Resolvable) { 0057 self.variable = variable 0058 } 0059 0060 public init(variable: String) { 0061 self.variable = Variable(variable) 0062 } 0063 0064 public func render(context: Context) throws -> String { 0065 let result = try variable.resolve(context) 0066 0067 if let result = result as? String { 0068 return result 0069 } else if let result = result as? CustomStringConvertible { 0070 return result.description 0071 } else if let result = result as? NSObject { 0072 return result.description 0073 } 0074 0075 return "" 0076 } 0077 } 0078 0079 public class NowNode
Parser.swift:44 nodes.append(VariableNode(variable: try compileFilter(token.contents))): NodeType { 0080 public let format
Namespace.swift:16 registerTag("now", parser: NowNode.parse)Node.swift:93 return NowNode(format:format):Variable 0081 0082 public class func parse
Node.swift:97 self.format = format ?? Variable("\"yyyy-MM-dd 'at' HH:mm\"")Node.swift:102 let format = try self.format.resolve(context)(parser:TokenParser, token:Token) throws -> NodeType { 0083 var format:Variable? 0084 0085 let components = token.components() 0086 guard components.count <= 2 else { 0087 throw TemplateSyntaxError("'now' tags may only have one argument: the format string `\(token.contents)`.") 0088 } 0089 if components.count == 2 { 0090 format = Variable(components[1]) 0091 } 0092 0093 return NowNode(format:format) 0094 } 0095 0096 public init
Namespace.swift:16 registerTag("now", parser: NowNode.parse)(format:Variable?) { 0097 self.format = format ?? Variable("\"yyyy-MM-dd 'at' HH:mm\"") 0098 } 0099 0100 public func render(context: Context) throws -> String { 0101 let date = NSDate() 0102 let format = try self.format.resolve(context) 0103 var formatter:NSDateFormatter? 0104 0105 if let format = format as? NSDateFormatter { 0106 formatter = format 0107 } else if let format = format as? String { 0108 formatter = NSDateFormatter() 0109 formatter!.dateFormat = format 0110 } else { 0111 return "" 0112 } 0113 0114 return formatter!.stringFromDate(date) 0115 } 0116 } 0117 0118 public class ForNode
Node.swift:93 return NowNode(format:format): NodeType { 0119 let variable
Namespace.swift:13 registerTag("for", parser: ForNode.parse)Node.swift:147 return ForNode(variable: variable, loopVariable: loopVariable, nodes: forNodes, emptyNodes:emptyNodes):Variable 0120 let loopVariable
Node.swift:151 self.variable = Variable(variable)Node.swift:158 let values = try variable.resolve(context):String 0121 let nodes
Node.swift:152 self.loopVariable = loopVariableNode.swift:169 return try context.push([loopVariable: item, "forloop": forContext]) {:[NodeType] 0122 let emptyNodes
Node.swift:153 self.nodes = nodesNode.swift:170 try renderNodes(nodes, context): [NodeType] 0123 0124 public class func parse
Node.swift:154 self.emptyNodes = emptyNodesNode.swift:176 try renderNodes(emptyNodes, context)(parser:TokenParser, token:Token) throws -> NodeType { 0125 let components = token.components() 0126 0127 guard components.count == 4 && components[2] == "in" else { 0128 throw TemplateSyntaxError("'for' statements should use the following 'for x in y' `\(token.contents)`.") 0129 } 0130 0131 let loopVariable = components[1] 0132 let variable = components[3] 0133 0134 var emptyNodes = [NodeType]() 0135 0136 let forNodes = try parser.parse(until(["endfor", "empty"])) 0137 0138 guard let token = parser.nextToken() else { 0139 throw TemplateSyntaxError("`endfor` was not found.") 0140 } 0141 0142 if token.contents == "empty" { 0143 emptyNodes = try parser.parse(until(["endfor"])) 0144 parser.nextToken() 0145 } 0146 0147 return ForNode(variable: variable, loopVariable: loopVariable, nodes: forNodes, emptyNodes:emptyNodes) 0148 } 0149 0150 public init
Namespace.swift:13 registerTag("for", parser: ForNode.parse)(variable:String, loopVariable:String, nodes:[NodeType], emptyNodes:[NodeType]) { 0151 self.variable = Variable(variable) 0152 self.loopVariable = loopVariable 0153 self.nodes = nodes 0154 self.emptyNodes = emptyNodes 0155 } 0156 0157 public func render(context: Context) throws -> String { 0158 let values = try variable.resolve(context) 0159 0160 if let values = values as? [Any] where values.count > 0 { 0161 let count = values.count 0162 return try values.enumerate().map { index, item in 0163 let forContext: [String: Any] = [ 0164 "first": index == 0, 0165 "last": index == (count - 1), 0166 "counter": index + 1, 0167 ] 0168 0169 return try context.push([loopVariable: item, "forloop": forContext]) { 0170 try renderNodes(nodes, context) 0171 } 0172 }.joinWithSeparator("") 0173 } 0174 0175 return try context.push { 0176 try renderNodes(emptyNodes, context) 0177 } 0178 } 0179 } 0180 0181 public class IfNode
Node.swift:147 return ForNode(variable: variable, loopVariable: loopVariable, nodes: forNodes, emptyNodes:emptyNodes): NodeType { 0182 public let variable
Namespace.swift:14 registerTag("if", parser: IfNode.parse)Namespace.swift:15 registerTag("ifnot", parser: IfNode.parse_ifnot)Node.swift:206 return IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes)Node.swift:229 return IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes):Variable 0183 public let trueNodes
Node.swift:233 self.variable = Variable(variable)Node.swift:239 let result = try variable.resolve(context):[NodeType] 0184 public let falseNodes
Node.swift:234 self.trueNodes = trueNodesNode.swift:253 output = try renderNodes(trueNodes, context):[NodeType] 0185 0186 public class func parse
Node.swift:235 self.falseNodes = falseNodesNode.swift:255 output = try renderNodes(falseNodes, context)(parser:TokenParser, token:Token) throws -> NodeType { 0187 let components = token.components() 0188 guard components.count == 2 else { 0189 throw TemplateSyntaxError("'if' statements should use the following 'if condition' `\(token.contents)`.") 0190 } 0191 let variable = components[1] 0192 var trueNodes = [NodeType]() 0193 var falseNodes = [NodeType]() 0194 0195 trueNodes = try parser.parse(until(["endif", "else"])) 0196 0197 guard let token = parser.nextToken() else { 0198 throw TemplateSyntaxError("`endif` was not found.") 0199 } 0200 0201 if token.contents == "else" { 0202 falseNodes = try parser.parse(until(["endif"])) 0203 parser.nextToken() 0204 } 0205 0206 return IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes) 0207 } 0208 0209 public class func parse_ifnot
Namespace.swift:14 registerTag("if", parser: IfNode.parse)(parser:TokenParser, token:Token) throws -> NodeType { 0210 let components = token.components() 0211 guard components.count == 2 else { 0212 throw TemplateSyntaxError("'ifnot' statements should use the following 'if condition' `\(token.contents)`.") 0213 } 0214 let variable = components[1] 0215 var trueNodes = [NodeType]() 0216 var falseNodes = [NodeType]() 0217 0218 falseNodes = try parser.parse(until(["endif", "else"])) 0219 0220 guard let token = parser.nextToken() else { 0221 throw TemplateSyntaxError("`endif` was not found.") 0222 } 0223 0224 if token.contents == "else" { 0225 trueNodes = try parser.parse(until(["endif"])) 0226 parser.nextToken() 0227 } 0228 0229 return IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes) 0230 } 0231 0232 public init
Namespace.swift:15 registerTag("ifnot", parser: IfNode.parse_ifnot)(variable:String, trueNodes:[NodeType], falseNodes:[NodeType]) { 0233 self.variable = Variable(variable) 0234 self.trueNodes = trueNodes 0235 self.falseNodes = falseNodes 0236 } 0237 0238 public func render(context: Context) throws -> String { 0239 let result = try variable.resolve(context) 0240 var truthy = false 0241 0242 if let result = result as? [Any] { 0243 truthy = !result.isEmpty 0244 } else if let result = result as? [String:Any] { 0245 truthy = !result.isEmpty 0246 } else if result != nil { 0247 truthy = true 0248 } 0249 0250 context.push() 0251 let output:String 0252 if truthy { 0253 output = try renderNodes(trueNodes, context) 0254 } else { 0255 output = try renderNodes(falseNodes, context) 0256 } 0257 context.pop() 0258 0259 return output 0260 } 0261 } 0262
Node.swift:206 return IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes)Node.swift:229 return IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes)