Skip to content

Commit

Permalink
[Project] Use 2 spaces for indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
kylef committed Jun 29, 2015
1 parent 59bab00 commit 53d5a4f
Show file tree
Hide file tree
Showing 20 changed files with 1,097 additions and 1,095 deletions.
2 changes: 2 additions & 0 deletions Stencil.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@
F0837D60120FB15CC00B9EBD /* Pods */,
BE2E2DF8F488C4669126E920 /* Frameworks */,
);
indentWidth = 2;
sourceTree = "<group>";
tabWidth = 2;
};
77FAAE5319F91E480029DC5E /* Products */ = {
isa = PBXGroup;
Expand Down
74 changes: 37 additions & 37 deletions Stencil/Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,51 @@ import Foundation

/// A container for template variables.
public class Context : Equatable {
var dictionaries:[Dictionary<String, AnyObject>]

public init(dictionary:Dictionary<String, AnyObject>) {
dictionaries = [dictionary]
}

public init() {
dictionaries = []
}

public subscript(key: String) -> AnyObject? {
/// Retrieves a variable's value, starting at the current context and going upwards
get {
for dictionary in reverse(dictionaries) {
if let value:AnyObject = dictionary[key] {
return value
}
}

return nil
var dictionaries:[Dictionary<String, AnyObject>]

public init(dictionary:Dictionary<String, AnyObject>) {
dictionaries = [dictionary]
}

public init() {
dictionaries = []
}

public subscript(key: String) -> AnyObject? {
/// Retrieves a variable's value, starting at the current context and going upwards
get {
for dictionary in reverse(dictionaries) {
if let value:AnyObject = dictionary[key] {
return value
}
}

/// Set a variable in the current context, deleting the variable if it's nil
set(value) {
if dictionaries.count > 0 {
var dictionary = dictionaries.removeLast()
dictionary[key] = value
dictionaries.append(dictionary)
}
}
}

public func push() {
push(Dictionary<String, String>())
return nil
}

public func push(dictionary:Dictionary<String, String>) {
/// Set a variable in the current context, deleting the variable if it's nil
set(value) {
if dictionaries.count > 0 {
var dictionary = dictionaries.removeLast()
dictionary[key] = value
dictionaries.append(dictionary)
}
}
}

public func pop() {
dictionaries.removeLast()
}
public func push() {
push(Dictionary<String, String>())
}

public func push(dictionary:Dictionary<String, String>) {
dictionaries.append(dictionary)
}

public func pop() {
dictionaries.removeLast()
}
}

public func ==(lhs:Context, rhs:Context) -> Bool {
return lhs.dictionaries == rhs.dictionaries
return lhs.dictionaries == rhs.dictionaries
}
82 changes: 41 additions & 41 deletions Stencil/Lexer.swift
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
import Foundation

public struct Lexer {
public let templateString:String
let regex = NSRegularExpression(pattern: "(\\{\\{.*?\\}\\}|\\{%.*?%\\}|\\{#.*?#\\})", options: nil, error: nil)!
public let templateString:String
let regex = NSRegularExpression(pattern: "(\\{\\{.*?\\}\\}|\\{%.*?%\\}|\\{#.*?#\\})", options: nil, error: nil)!

public init(templateString:String) {
self.templateString = templateString
}

func createToken(string:String) -> Token {
func strip() -> String {
return string[string.startIndex.successor().successor()..<string.endIndex.predecessor().predecessor()].stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
}
public init(templateString:String) {
self.templateString = templateString
}

if string.hasPrefix("{{") {
return Token.Variable(value: strip())
} else if string.hasPrefix("{%") {
return Token.Block(value: strip())
} else if string.hasPrefix("{#") {
return Token.Comment(value: strip())
}
func createToken(string:String) -> Token {
func strip() -> String {
return string[string.startIndex.successor().successor()..<string.endIndex.predecessor().predecessor()].stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
}

return Token.Text(value: string)
if string.hasPrefix("{{") {
return Token.Variable(value: strip())
} else if string.hasPrefix("{%") {
return Token.Block(value: strip())
} else if string.hasPrefix("{#") {
return Token.Comment(value: strip())
}

/// Returns an array of tokens from a given template string.
public func tokenize() -> [Token] {
// Unfortunately NSRegularExpression doesn't have a split.
// So here's a really terrible implementation
return Token.Text(value: string)
}

var tokens = [Token]()
/// Returns an array of tokens from a given template string.
public func tokenize() -> [Token] {
// Unfortunately NSRegularExpression doesn't have a split.
// So here's a really terrible implementation

let range = NSMakeRange(0, count(templateString))
var lastIndex = 0
let nsTemplateString = templateString as NSString
let options = NSMatchingOptions(0)
regex.enumerateMatchesInString(templateString, options: options, range: range) { (result, flags, b) in
if result.range.location != lastIndex {
let previousMatch = nsTemplateString.substringWithRange(NSMakeRange(lastIndex, result.range.location - lastIndex))
tokens.append(self.createToken(previousMatch))
}
var tokens = [Token]()

let match = nsTemplateString.substringWithRange(result.range)
tokens.append(self.createToken(match))
let range = NSMakeRange(0, count(templateString))
var lastIndex = 0
let nsTemplateString = templateString as NSString
let options = NSMatchingOptions(0)
regex.enumerateMatchesInString(templateString, options: options, range: range) { (result, flags, b) in
if result.range.location != lastIndex {
let previousMatch = nsTemplateString.substringWithRange(NSMakeRange(lastIndex, result.range.location - lastIndex))
tokens.append(self.createToken(previousMatch))
}

lastIndex = result.range.location + result.range.length
}
let match = nsTemplateString.substringWithRange(result.range)
tokens.append(self.createToken(match))

if lastIndex < count(templateString) {
let substring = (templateString as NSString).substringFromIndex(lastIndex)
tokens.append(Token.Text(value: substring))
}
lastIndex = result.range.location + result.range.length
}

return tokens
if lastIndex < count(templateString) {
let substring = (templateString as NSString).substringFromIndex(lastIndex)
tokens.append(Token.Text(value: substring))
}

return tokens
}
}
Loading

0 comments on commit 53d5a4f

Please sign in to comment.