Merge pull request #9 from kylef/opinionated_changes
Opinionated Changes
This commit is contained in:
@@ -26,11 +26,12 @@ let context = Context(dictionary: [
|
|||||||
let template = Template(named: "template.stencil")
|
let template = Template(named: "template.stencil")
|
||||||
let result = template.render(context)
|
let result = template.render(context)
|
||||||
|
|
||||||
if let error = result.error {
|
switch result {
|
||||||
println("There was an error rendering your template (\(error)).")
|
case .Error(let error):
|
||||||
|
println("There was an error rendering your template (\(error)).")
|
||||||
|
case .Success(let string):
|
||||||
|
println("\(string)")
|
||||||
}
|
}
|
||||||
|
|
||||||
println("\(result.string)")
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Philosophy
|
## Philosophy
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
objects = {
|
objects = {
|
||||||
|
|
||||||
/* Begin PBXBuildFile section */
|
/* Begin PBXBuildFile section */
|
||||||
|
71CE4C0A19FD29D000B9E0C5 /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71CE4C0919FD29D000B9E0C5 /* Result.swift */; };
|
||||||
7725B3CB19F92B4F002CF74B /* VariableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7725B3CA19F92B4F002CF74B /* VariableTests.swift */; };
|
7725B3CB19F92B4F002CF74B /* VariableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7725B3CA19F92B4F002CF74B /* VariableTests.swift */; };
|
||||||
7725B3CD19F92B61002CF74B /* Variable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7725B3CC19F92B61002CF74B /* Variable.swift */; };
|
7725B3CD19F92B61002CF74B /* Variable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7725B3CC19F92B61002CF74B /* Variable.swift */; };
|
||||||
7725B3CF19F94214002CF74B /* Tokenizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7725B3CE19F94214002CF74B /* Tokenizer.swift */; };
|
7725B3CF19F94214002CF74B /* Tokenizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7725B3CE19F94214002CF74B /* Tokenizer.swift */; };
|
||||||
@@ -36,6 +37,7 @@
|
|||||||
/* End PBXContainerItemProxy section */
|
/* End PBXContainerItemProxy section */
|
||||||
|
|
||||||
/* Begin PBXFileReference section */
|
/* Begin PBXFileReference section */
|
||||||
|
71CE4C0919FD29D000B9E0C5 /* Result.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Result.swift; sourceTree = "<group>"; };
|
||||||
7725B3CA19F92B4F002CF74B /* VariableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VariableTests.swift; sourceTree = "<group>"; };
|
7725B3CA19F92B4F002CF74B /* VariableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VariableTests.swift; sourceTree = "<group>"; };
|
||||||
7725B3CC19F92B61002CF74B /* Variable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Variable.swift; sourceTree = "<group>"; };
|
7725B3CC19F92B61002CF74B /* Variable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Variable.swift; sourceTree = "<group>"; };
|
||||||
7725B3CE19F94214002CF74B /* Tokenizer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Tokenizer.swift; sourceTree = "<group>"; };
|
7725B3CE19F94214002CF74B /* Tokenizer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Tokenizer.swift; sourceTree = "<group>"; };
|
||||||
@@ -97,14 +99,15 @@
|
|||||||
77FAAE5419F91E480029DC5E /* Stencil */ = {
|
77FAAE5419F91E480029DC5E /* Stencil */ = {
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
77FAAE5719F91E480029DC5E /* Stencil.h */,
|
|
||||||
77FAAE6E19F920750029DC5E /* Context.swift */,
|
77FAAE6E19F920750029DC5E /* Context.swift */,
|
||||||
7725B3CC19F92B61002CF74B /* Variable.swift */,
|
77EB082A19FA8600001870F1 /* Lexer.swift */,
|
||||||
7725B3CE19F94214002CF74B /* Tokenizer.swift */,
|
|
||||||
7725B3D419F9438F002CF74B /* Node.swift */,
|
7725B3D419F9438F002CF74B /* Node.swift */,
|
||||||
7725B3D619F94A43002CF74B /* Parser.swift */,
|
7725B3D619F94A43002CF74B /* Parser.swift */,
|
||||||
77EB082A19FA8600001870F1 /* Lexer.swift */,
|
71CE4C0919FD29D000B9E0C5 /* Result.swift */,
|
||||||
|
77FAAE5719F91E480029DC5E /* Stencil.h */,
|
||||||
77EB082419F96E88001870F1 /* Template.swift */,
|
77EB082419F96E88001870F1 /* Template.swift */,
|
||||||
|
7725B3CE19F94214002CF74B /* Tokenizer.swift */,
|
||||||
|
7725B3CC19F92B61002CF74B /* Variable.swift */,
|
||||||
77FAAE5519F91E480029DC5E /* Supporting Files */,
|
77FAAE5519F91E480029DC5E /* Supporting Files */,
|
||||||
);
|
);
|
||||||
path = Stencil;
|
path = Stencil;
|
||||||
@@ -254,6 +257,7 @@
|
|||||||
7725B3D719F94A43002CF74B /* Parser.swift in Sources */,
|
7725B3D719F94A43002CF74B /* Parser.swift in Sources */,
|
||||||
77EB082519F96E88001870F1 /* Template.swift in Sources */,
|
77EB082519F96E88001870F1 /* Template.swift in Sources */,
|
||||||
7725B3CD19F92B61002CF74B /* Variable.swift in Sources */,
|
7725B3CD19F92B61002CF74B /* Variable.swift in Sources */,
|
||||||
|
71CE4C0A19FD29D000B9E0C5 /* Result.swift in Sources */,
|
||||||
7725B3D519F9438F002CF74B /* Node.swift in Sources */,
|
7725B3D519F9438F002CF74B /* Node.swift in Sources */,
|
||||||
);
|
);
|
||||||
runOnlyForDeploymentPostprocessing = 0;
|
runOnlyForDeploymentPostprocessing = 0;
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import Foundation
|
|||||||
|
|
||||||
public struct Lexer {
|
public struct Lexer {
|
||||||
public let templateString:String
|
public let templateString:String
|
||||||
let regex = NSRegularExpression(pattern: "(\\{\\{.*?\\}\\}|\\{%.*?%\\}|\\{#.*?#\\})", options: nil, error: nil)
|
let regex = NSRegularExpression(pattern: "(\\{\\{.*?\\}\\}|\\{%.*?%\\}|\\{#.*?#\\})", options: nil, error: nil)!
|
||||||
|
|
||||||
public init(templateString:String) {
|
public init(templateString:String) {
|
||||||
self.templateString = templateString
|
self.templateString = templateString
|
||||||
@@ -41,7 +41,8 @@ public struct Lexer {
|
|||||||
let range = NSMakeRange(0, countElements(templateString))
|
let range = NSMakeRange(0, countElements(templateString))
|
||||||
var lastIndex = 0
|
var lastIndex = 0
|
||||||
let nsTemplateString = templateString as NSString
|
let nsTemplateString = templateString as NSString
|
||||||
regex.enumerateMatchesInString(templateString, options: nil, range: range) { (result, flags, b) in
|
let options = NSMatchingOptions(0)
|
||||||
|
regex.enumerateMatchesInString(templateString, options: options, range: range) { (result, flags, b) in
|
||||||
if result.range.location != lastIndex {
|
if result.range.location != lastIndex {
|
||||||
let previousMatch = nsTemplateString.substringWithRange(NSMakeRange(lastIndex, result.range.location - lastIndex))
|
let previousMatch = nsTemplateString.substringWithRange(NSMakeRange(lastIndex, result.range.location - lastIndex))
|
||||||
tokens.append(self.createToken(previousMatch))
|
tokens.append(self.createToken(previousMatch))
|
||||||
|
|||||||
@@ -8,10 +8,6 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public protocol Error : Printable {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
struct NodeError : Error {
|
struct NodeError : Error {
|
||||||
let token:Token
|
let token:Token
|
||||||
let message:String
|
let message:String
|
||||||
|
|||||||
33
Stencil/Result.swift
Normal file
33
Stencil/Result.swift
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
//
|
||||||
|
// Result.swift
|
||||||
|
// Stencil
|
||||||
|
//
|
||||||
|
// Created by Marius Rackwitz on 26/10/14.
|
||||||
|
// Copyright (c) 2014 Cocode. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
public protocol Error : Printable {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public func ==(lhs:Error, rhs:Error) -> Bool {
|
||||||
|
return lhs.description == rhs.description
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum Result : Equatable {
|
||||||
|
case Success(string: String)
|
||||||
|
case Error(error: Stencil.Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
public func ==(lhs:Result, rhs:Result) -> Bool {
|
||||||
|
switch (lhs, rhs) {
|
||||||
|
case (.Success(let lhsValue), .Success(let rhsValue)):
|
||||||
|
return lhsValue == rhsValue
|
||||||
|
case (.Error(let lhsValue), .Error(let rhsValue)):
|
||||||
|
return lhsValue == rhsValue
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,11 +11,11 @@ import Foundation
|
|||||||
public class Template {
|
public class Template {
|
||||||
let parser:TokenParser
|
let parser:TokenParser
|
||||||
|
|
||||||
public convenience init(named:String) {
|
public convenience init?(named:String) {
|
||||||
self.init(named:named, inBundle:nil)
|
self.init(named:named, inBundle:nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
public convenience init(named:String, inBundle bundle:NSBundle?) {
|
public convenience init?(named:String, inBundle bundle:NSBundle?) {
|
||||||
var url:NSURL?
|
var url:NSURL?
|
||||||
|
|
||||||
if let bundle = bundle {
|
if let bundle = bundle {
|
||||||
@@ -27,10 +27,15 @@ public class Template {
|
|||||||
self.init(URL:url!)
|
self.init(URL:url!)
|
||||||
}
|
}
|
||||||
|
|
||||||
public convenience init(URL:NSURL) {
|
public convenience init?(URL:NSURL) {
|
||||||
var error:NSError?
|
var error:NSError?
|
||||||
let templateString = NSString.stringWithContentsOfURL(URL, encoding: NSUTF8StringEncoding, error: &error)
|
let maybeTemplateString = NSString(contentsOfURL: URL, encoding: NSUTF8StringEncoding, error: &error)
|
||||||
self.init(templateString:templateString)
|
if let templateString = maybeTemplateString {
|
||||||
|
self.init(templateString:templateString)
|
||||||
|
} else {
|
||||||
|
self.init(templateString:"")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(templateString:String) {
|
public init(templateString:String) {
|
||||||
@@ -39,15 +44,19 @@ public class Template {
|
|||||||
parser = TokenParser(tokens: tokens)
|
parser = TokenParser(tokens: tokens)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func render(context:Context) -> (string:String?, error:Error?) {
|
public func render(context:Context) -> Result {
|
||||||
let (nodes, error) = parser.parse()
|
let (nodes, error) = parser.parse()
|
||||||
|
|
||||||
if let error = error {
|
if let error = error {
|
||||||
return (nil, error)
|
return .Error(error: error)
|
||||||
} else if let nodes = nodes {
|
} else if let nodes = nodes {
|
||||||
return renderNodes(nodes, context)
|
let result = renderNodes(nodes, context)
|
||||||
|
if let string = result.0 {
|
||||||
|
return .Success(string: string)
|
||||||
|
} else {
|
||||||
|
return .Error(error: result.1!)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return .Success(string: "")
|
||||||
return (nil, nil)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,16 +18,20 @@ public enum Token : Equatable {
|
|||||||
func components() -> [String] {
|
func components() -> [String] {
|
||||||
// TODO: Make this smarter and treat quoted strings as a single component
|
// TODO: Make this smarter and treat quoted strings as a single component
|
||||||
let characterSet = NSCharacterSet.whitespaceAndNewlineCharacterSet()
|
let characterSet = NSCharacterSet.whitespaceAndNewlineCharacterSet()
|
||||||
|
|
||||||
|
func strip(value: String) -> [String] {
|
||||||
|
return value.stringByTrimmingCharactersInSet(characterSet).componentsSeparatedByCharactersInSet(characterSet)
|
||||||
|
}
|
||||||
|
|
||||||
switch self {
|
switch self {
|
||||||
case .Block(let value):
|
case .Block(let value):
|
||||||
return value.value.stringByTrimmingCharactersInSet(characterSet).componentsSeparatedByCharactersInSet(characterSet)
|
return strip(value)
|
||||||
case .Variable(let value):
|
case .Variable(let value):
|
||||||
return value.value.stringByTrimmingCharactersInSet(characterSet).componentsSeparatedByCharactersInSet(characterSet)
|
return strip(value)
|
||||||
case .Text(let value):
|
case .Text(let value):
|
||||||
return value.value.stringByTrimmingCharactersInSet(characterSet).componentsSeparatedByCharactersInSet(characterSet)
|
return strip(value)
|
||||||
case .Comment(let value):
|
case .Comment(let value):
|
||||||
return value.value.stringByTrimmingCharactersInSet(characterSet).componentsSeparatedByCharactersInSet(characterSet)
|
return strip(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,6 @@ class StencilTests: XCTestCase {
|
|||||||
" - Memory Management with ARC by Kyle Fuller.\n" +
|
" - Memory Management with ARC by Kyle Fuller.\n" +
|
||||||
"\n"
|
"\n"
|
||||||
|
|
||||||
XCTAssertEqual(result.string!, fixture)
|
XCTAssertEqual(result, Result.Success(string: fixture))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,9 +15,8 @@ class TemplateTests: XCTestCase {
|
|||||||
func testTemplate() {
|
func testTemplate() {
|
||||||
let context = Context(dictionary: [ "name": "Kyle" ])
|
let context = Context(dictionary: [ "name": "Kyle" ])
|
||||||
let template = Template(templateString: "Hello World")
|
let template = Template(templateString: "Hello World")
|
||||||
let (string, error) = template.render(context)
|
let result = template.render(context)
|
||||||
XCTAssertEqual(string!, "Hello World")
|
XCTAssertEqual(result, Result.Success(string: "Hello World"))
|
||||||
XCTAssertTrue(error == nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user