@@ -79,10 +79,13 @@ public class VariableNode : NodeType {
|
|||||||
public class NowNode : NodeType {
|
public class NowNode : NodeType {
|
||||||
public let format:Variable
|
public let format:Variable
|
||||||
|
|
||||||
public class func parse(parser:TokenParser, token:Token) -> NodeType {
|
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
||||||
var format:Variable?
|
var format:Variable?
|
||||||
|
|
||||||
let components = token.components()
|
let components = token.components()
|
||||||
|
guard components.count <= 2 else {
|
||||||
|
throw TemplateSyntaxError("'now' tags may only have one argument: the format string `\(token.contents)`.")
|
||||||
|
}
|
||||||
if components.count == 2 {
|
if components.count == 2 {
|
||||||
format = Variable(components[1])
|
format = Variable(components[1])
|
||||||
}
|
}
|
||||||
@@ -91,11 +94,7 @@ public class NowNode : NodeType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public init(format:Variable?) {
|
public init(format:Variable?) {
|
||||||
if let format = format {
|
self.format = format ?? Variable("\"yyyy-MM-dd 'at' HH:mm\"")
|
||||||
self.format = format
|
|
||||||
} else {
|
|
||||||
self.format = Variable("\"yyyy-MM-dd 'at' HH:mm\"")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func render(context: Context) throws -> String {
|
public func render(context: Context) throws -> String {
|
||||||
@@ -124,7 +123,10 @@ public class ForNode : NodeType {
|
|||||||
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
||||||
let components = token.components()
|
let components = token.components()
|
||||||
|
|
||||||
if components.count == 4 && components[2] == "in" {
|
guard components.count == 4 && components[2] == "in" else {
|
||||||
|
throw TemplateSyntaxError("'for' statements should use the following 'for x in y' `\(token.contents)`.")
|
||||||
|
}
|
||||||
|
|
||||||
let loopVariable = components[1]
|
let loopVariable = components[1]
|
||||||
let variable = components[3]
|
let variable = components[3]
|
||||||
|
|
||||||
@@ -132,25 +134,23 @@ public class ForNode : NodeType {
|
|||||||
|
|
||||||
let forNodes = try parser.parse(until(["endfor", "empty"]))
|
let forNodes = try parser.parse(until(["endfor", "empty"]))
|
||||||
|
|
||||||
if let token = parser.nextToken() {
|
guard let token = parser.nextToken() else {
|
||||||
|
throw TemplateSyntaxError("`endfor` was not found.")
|
||||||
|
}
|
||||||
|
|
||||||
if token.contents == "empty" {
|
if token.contents == "empty" {
|
||||||
emptyNodes = try parser.parse(until(["endfor"]))
|
emptyNodes = try parser.parse(until(["endfor"]))
|
||||||
parser.nextToken()
|
parser.nextToken()
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
throw TemplateSyntaxError("`endfor` was not found.")
|
|
||||||
}
|
|
||||||
|
|
||||||
return ForNode(variable: variable, loopVariable: loopVariable, nodes: forNodes, emptyNodes:emptyNodes)
|
return ForNode(variable: variable, loopVariable: loopVariable, nodes: forNodes, emptyNodes:emptyNodes)
|
||||||
}
|
}
|
||||||
|
|
||||||
throw TemplateSyntaxError("'for' statements should use the following 'for x in y' `\(token.contents)`.")
|
|
||||||
}
|
|
||||||
|
|
||||||
public init(variable:String, loopVariable:String, nodes:[NodeType], emptyNodes:[NodeType]) {
|
public init(variable:String, loopVariable:String, nodes:[NodeType], emptyNodes:[NodeType]) {
|
||||||
self.variable = Variable(variable)
|
self.variable = Variable(variable)
|
||||||
self.loopVariable = loopVariable
|
self.loopVariable = loopVariable
|
||||||
self.nodes = nodes
|
self.nodes = nodes
|
||||||
|
// TODO: Handle emptyNodes
|
||||||
}
|
}
|
||||||
|
|
||||||
public func render(context: Context) throws -> String {
|
public func render(context: Context) throws -> String {
|
||||||
@@ -175,39 +175,47 @@ public class IfNode : NodeType {
|
|||||||
public let falseNodes:[NodeType]
|
public let falseNodes:[NodeType]
|
||||||
|
|
||||||
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
||||||
let variable = token.components()[1]
|
let components = token.components()
|
||||||
|
guard components.count == 2 else {
|
||||||
|
throw TemplateSyntaxError("'if' statements should use the following 'if condition' `\(token.contents)`.")
|
||||||
|
}
|
||||||
|
let variable = components[1]
|
||||||
var trueNodes = [NodeType]()
|
var trueNodes = [NodeType]()
|
||||||
var falseNodes = [NodeType]()
|
var falseNodes = [NodeType]()
|
||||||
|
|
||||||
trueNodes = try parser.parse(until(["endif", "else"]))
|
trueNodes = try parser.parse(until(["endif", "else"]))
|
||||||
|
|
||||||
if let token = parser.nextToken() {
|
guard let token = parser.nextToken() else {
|
||||||
|
throw TemplateSyntaxError("`endif` was not found.")
|
||||||
|
}
|
||||||
|
|
||||||
if token.contents == "else" {
|
if token.contents == "else" {
|
||||||
falseNodes = try parser.parse(until(["endif"]))
|
falseNodes = try parser.parse(until(["endif"]))
|
||||||
parser.nextToken()
|
parser.nextToken()
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
throw TemplateSyntaxError("`endif` was not found.")
|
|
||||||
}
|
|
||||||
|
|
||||||
return IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes)
|
return IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes)
|
||||||
}
|
}
|
||||||
|
|
||||||
public class func parse_ifnot(parser:TokenParser, token:Token) throws -> NodeType {
|
public class func parse_ifnot(parser:TokenParser, token:Token) throws -> NodeType {
|
||||||
let variable = token.components()[1]
|
let components = token.components()
|
||||||
|
guard components.count == 2 else {
|
||||||
|
throw TemplateSyntaxError("'ifnot' statements should use the following 'if condition' `\(token.contents)`.")
|
||||||
|
}
|
||||||
|
let variable = components[1]
|
||||||
var trueNodes = [NodeType]()
|
var trueNodes = [NodeType]()
|
||||||
var falseNodes = [NodeType]()
|
var falseNodes = [NodeType]()
|
||||||
|
|
||||||
falseNodes = try parser.parse(until(["endif", "else"]))
|
falseNodes = try parser.parse(until(["endif", "else"]))
|
||||||
|
|
||||||
if let token = parser.nextToken() {
|
guard let token = parser.nextToken() else {
|
||||||
|
throw TemplateSyntaxError("`endif` was not found.")
|
||||||
|
}
|
||||||
|
|
||||||
if token.contents == "else" {
|
if token.contents == "else" {
|
||||||
trueNodes = try parser.parse(until(["endif"]))
|
trueNodes = try parser.parse(until(["endif"]))
|
||||||
parser.nextToken()
|
parser.nextToken()
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
throw TemplateSyntaxError("`endif` was not found.")
|
|
||||||
}
|
|
||||||
|
|
||||||
return IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes)
|
return IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,12 +69,10 @@ public class TokenParser {
|
|||||||
case .Block:
|
case .Block:
|
||||||
let tag = token.components().first
|
let tag = token.components().first
|
||||||
|
|
||||||
if let parse_until = parse_until {
|
if let parse_until = parse_until where parse_until(parser: self, token: token) {
|
||||||
if parse_until(parser: self, token: token) {
|
|
||||||
prependToken(token)
|
prependToken(token)
|
||||||
return nodes
|
return nodes
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if let tag = tag, let parser = self.tags[tag] {
|
if let tag = tag, let parser = self.tags[tag] {
|
||||||
nodes.append(try parser(self, token))
|
nodes.append(try parser(self, token))
|
||||||
|
|||||||
@@ -9,11 +9,11 @@ public class Template {
|
|||||||
/// Create a template with the given name inside the given bundle
|
/// Create a template with the given name inside the given bundle
|
||||||
public convenience init(named:String, inBundle bundle:NSBundle? = nil) throws {
|
public convenience init(named:String, inBundle bundle:NSBundle? = nil) throws {
|
||||||
let useBundle = bundle ?? NSBundle.mainBundle()
|
let useBundle = bundle ?? NSBundle.mainBundle()
|
||||||
if let url = useBundle.URLForResource(named, withExtension: nil) {
|
guard let url = useBundle.URLForResource(named, withExtension: nil) else {
|
||||||
try self.init(URL:url)
|
|
||||||
} else {
|
|
||||||
throw NSError(domain: NSCocoaErrorDomain, code: NSFileNoSuchFileError, userInfo: nil)
|
throw NSError(domain: NSCocoaErrorDomain, code: NSFileNoSuchFileError, userInfo: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try self.init(URL:url)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a template with a file found at the given URL
|
/// Create a template with a file found at the given URL
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ public class IncludeNode : NodeType {
|
|||||||
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
||||||
let bits = token.contents.componentsSeparatedByString("\"")
|
let bits = token.contents.componentsSeparatedByString("\"")
|
||||||
|
|
||||||
if bits.count != 3 {
|
guard bits.count == 3 else {
|
||||||
throw TemplateSyntaxError("'include' tag takes one argument, the template file to be included")
|
throw TemplateSyntaxError("'include' tag takes one argument, the template file to be included")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,18 +20,16 @@ public class IncludeNode : NodeType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func render(context: Context) throws -> String {
|
public func render(context: Context) throws -> String {
|
||||||
if let loader = context["loader"] as? TemplateLoader {
|
guard let loader = context["loader"] as? TemplateLoader else {
|
||||||
if let template = loader.loadTemplate(templateName) {
|
throw TemplateSyntaxError("Template loader not in context")
|
||||||
return try template.render(context)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let paths:String = loader.paths.map { path in
|
guard let template = loader.loadTemplate(templateName) else {
|
||||||
return path.description
|
let paths:String = loader.paths.map { $0.description }.joinWithSeparator(", ")
|
||||||
}.joinWithSeparator(", ")
|
|
||||||
throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)")
|
throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)")
|
||||||
}
|
}
|
||||||
|
|
||||||
throw TemplateSyntaxError("Template loader not in context")
|
return try template.render(context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,12 +31,12 @@ class ExtendsNode : NodeType {
|
|||||||
class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
||||||
let bits = token.contents.componentsSeparatedByString("\"")
|
let bits = token.contents.componentsSeparatedByString("\"")
|
||||||
|
|
||||||
if bits.count != 3 {
|
guard bits.count == 3 else {
|
||||||
throw TemplateSyntaxError("'extends' takes one argument, the template file to be extended")
|
throw TemplateSyntaxError("'extends' takes one argument, the template file to be extended")
|
||||||
}
|
}
|
||||||
|
|
||||||
let parsedNodes = try parser.parse()
|
let parsedNodes = try parser.parse()
|
||||||
if (any(parsedNodes) { ($0 as? ExtendsNode) != nil }) != nil {
|
guard (any(parsedNodes) { $0 is ExtendsNode }) == nil else {
|
||||||
throw TemplateSyntaxError("'extends' cannot appear more than once in the same template")
|
throw TemplateSyntaxError("'extends' cannot appear more than once in the same template")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,23 +58,21 @@ class ExtendsNode : NodeType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func render(context: Context) throws -> String {
|
func render(context: Context) throws -> String {
|
||||||
if let loader = context["loader"] as? TemplateLoader {
|
guard let loader = context["loader"] as? TemplateLoader else {
|
||||||
if let template = loader.loadTemplate(templateName) {
|
throw TemplateSyntaxError("Template loader not in context")
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let template = loader.loadTemplate(templateName) else {
|
||||||
|
let paths:String = loader.paths.map { $0.description }.joinWithSeparator(", ")
|
||||||
|
throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)")
|
||||||
|
}
|
||||||
|
|
||||||
let blockContext = BlockContext(blocks: blocks)
|
let blockContext = BlockContext(blocks: blocks)
|
||||||
context.push([BlockContext.contextKey: blockContext])
|
context.push([BlockContext.contextKey: blockContext])
|
||||||
let result = try template.render(context)
|
let result = try template.render(context)
|
||||||
context.pop()
|
context.pop()
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
let paths:String = loader.paths.map { path in
|
|
||||||
return path.description
|
|
||||||
}.joinWithSeparator(", ")
|
|
||||||
throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)")
|
|
||||||
}
|
|
||||||
|
|
||||||
throw TemplateSyntaxError("Template loader not in context")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class BlockNode : NodeType {
|
class BlockNode : NodeType {
|
||||||
@@ -84,7 +82,7 @@ class BlockNode : NodeType {
|
|||||||
class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
||||||
let bits = token.components()
|
let bits = token.components()
|
||||||
|
|
||||||
if bits.count != 2 {
|
guard bits.count == 2 else {
|
||||||
throw TemplateSyntaxError("'block' tag takes one argument, the template file to be included")
|
throw TemplateSyntaxError("'block' tag takes one argument, the template file to be included")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user