Basic method setup and calling
This commit is contained in:
29
Sources/HummingbirdMustache/Method.swift
Normal file
29
Sources/HummingbirdMustache/Method.swift
Normal file
@@ -0,0 +1,29 @@
|
||||
|
||||
protocol HBMustacheBaseMethods {
|
||||
func runMethod(_ name: String) -> Any?
|
||||
}
|
||||
protocol HBMustacheMethods {
|
||||
typealias Method = (Self) -> Any
|
||||
static var methods: [String: Method] { get set }
|
||||
}
|
||||
|
||||
extension HBMustacheMethods {
|
||||
static func addMethod(named name: String, method: @escaping Method) {
|
||||
Self.methods[name] = method
|
||||
}
|
||||
func runMethod(_ name: String) -> Any? {
|
||||
guard let method = Self.methods[name] else { return nil }
|
||||
return method(self)
|
||||
}
|
||||
}
|
||||
|
||||
extension String: HBMustacheBaseMethods {
|
||||
func runMethod(_ name: String) -> Any? {
|
||||
switch name {
|
||||
case "lowercased":
|
||||
return self.lowercased()
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@ extension HBMustacheTemplate {
|
||||
switch parser.current() {
|
||||
case "#":
|
||||
parser.unsafeAdvance()
|
||||
let name = try parseName(&parser)
|
||||
let (name, _) = try parseName(&parser)
|
||||
if parser.current() == "\n" {
|
||||
parser.unsafeAdvance()
|
||||
}
|
||||
@@ -33,7 +33,7 @@ extension HBMustacheTemplate {
|
||||
|
||||
case "^":
|
||||
parser.unsafeAdvance()
|
||||
let name = try parseName(&parser)
|
||||
let (name, _) = try parseName(&parser)
|
||||
if parser.current() == "\n" {
|
||||
parser.unsafeAdvance()
|
||||
}
|
||||
@@ -42,7 +42,7 @@ extension HBMustacheTemplate {
|
||||
|
||||
case "/":
|
||||
parser.unsafeAdvance()
|
||||
let name = try parseName(&parser)
|
||||
let (name, _) = try parseName(&parser)
|
||||
guard name == sectionName else {
|
||||
throw Error.sectionCloseNameIncorrect
|
||||
}
|
||||
@@ -53,7 +53,7 @@ extension HBMustacheTemplate {
|
||||
|
||||
case "{":
|
||||
parser.unsafeAdvance()
|
||||
let name = try parseName(&parser)
|
||||
let (name, _) = try parseName(&parser)
|
||||
guard try parser.read("}") else { throw Error.unfinishedName }
|
||||
tokens.append(.unescapedVariable(name))
|
||||
|
||||
@@ -63,12 +63,12 @@ extension HBMustacheTemplate {
|
||||
|
||||
case ">":
|
||||
parser.unsafeAdvance()
|
||||
let name = try parseName(&parser)
|
||||
let (name, _) = try parseName(&parser)
|
||||
tokens.append(.partial(name))
|
||||
|
||||
default:
|
||||
let name = try parseName(&parser)
|
||||
tokens.append(.variable(name))
|
||||
let (name, method) = try parseName(&parser)
|
||||
tokens.append(.variable(name, method))
|
||||
}
|
||||
}
|
||||
// should never get here if reading section
|
||||
@@ -78,12 +78,24 @@ extension HBMustacheTemplate {
|
||||
return tokens
|
||||
}
|
||||
|
||||
static func parseName(_ parser: inout HBParser) throws -> String {
|
||||
static func parseName(_ parser: inout HBParser) throws -> (String, String?) {
|
||||
parser.read(while: \.isWhitespace)
|
||||
let text = parser.read(while: sectionNameChars )
|
||||
var text = parser.read(while: sectionNameChars )
|
||||
parser.read(while: \.isWhitespace)
|
||||
guard try parser.read("}"), try parser.read("}") else { throw Error.unfinishedName }
|
||||
return text.string
|
||||
// does the name include brackets. If so this is a method call
|
||||
let string = text.read(while: sectionNameCharsWithoutBrackets)
|
||||
if text.reachedEnd() {
|
||||
return (text.string, nil)
|
||||
} else {
|
||||
guard text.current() == "(" else { throw Error.unfinishedName }
|
||||
text.unsafeAdvance()
|
||||
let string2 = text.read(while: sectionNameCharsWithoutBrackets)
|
||||
guard text.current() == ")" else { throw Error.unfinishedName }
|
||||
text.unsafeAdvance()
|
||||
guard text.reachedEnd() else { throw Error.unfinishedName }
|
||||
return (string2.string, string.string)
|
||||
}
|
||||
}
|
||||
|
||||
static func parseComment(_ parser: inout HBParser) throws -> String {
|
||||
@@ -91,5 +103,6 @@ extension HBMustacheTemplate {
|
||||
return text.string
|
||||
}
|
||||
|
||||
private static let sectionNameChars = Set<Unicode.Scalar>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._?")
|
||||
private static let sectionNameCharsWithoutBrackets = Set<Unicode.Scalar>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._?")
|
||||
private static let sectionNameChars = Set<Unicode.Scalar>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._?()")
|
||||
}
|
||||
|
||||
@@ -6,12 +6,18 @@ extension HBMustacheTemplate {
|
||||
switch token {
|
||||
case .text(let text):
|
||||
string += text
|
||||
case .variable(let variable):
|
||||
if let child = getChild(named: variable, from: object) {
|
||||
case .variable(let variable, let method):
|
||||
if var child = getChild(named: variable, from: object) {
|
||||
if let method = method,
|
||||
let runnable = child as? HBMustacheBaseMethods {
|
||||
if let result = runnable.runMethod(method) {
|
||||
child = result
|
||||
}
|
||||
}
|
||||
if let template = child as? HBMustacheTemplate {
|
||||
string += template.render(object, library: library)
|
||||
} else {
|
||||
string += encodedEscapedCharacters(String(describing: child))
|
||||
string += htmlEscape(String(describing: child))
|
||||
}
|
||||
}
|
||||
case .unescapedVariable(let variable):
|
||||
@@ -81,16 +87,16 @@ extension HBMustacheTemplate {
|
||||
return _getChild(named: nameSplit[...], from: object)
|
||||
}
|
||||
|
||||
private static let escapedCharacters: [Character: String] = [
|
||||
private static let htmlEscapedCharacters: [Character: String] = [
|
||||
"<": "<",
|
||||
">": ">",
|
||||
"&": "&",
|
||||
]
|
||||
func encodedEscapedCharacters(_ string: String) -> String {
|
||||
func htmlEscape(_ string: String) -> String {
|
||||
var newString = ""
|
||||
newString.reserveCapacity(string.count)
|
||||
for c in string {
|
||||
if let replacement = Self.escapedCharacters[c] {
|
||||
if let replacement = Self.htmlEscapedCharacters[c] {
|
||||
newString += replacement
|
||||
} else {
|
||||
newString.append(c)
|
||||
|
||||
@@ -10,7 +10,7 @@ public class HBMustacheTemplate {
|
||||
|
||||
enum Token {
|
||||
case text(String)
|
||||
case variable(String)
|
||||
case variable(String, String? = nil)
|
||||
case unescapedVariable(String)
|
||||
case section(String, HBMustacheTemplate)
|
||||
case invertedSection(String, HBMustacheTemplate)
|
||||
|
||||
Reference in New Issue
Block a user