swift format

This commit is contained in:
Adam Fowler
2021-03-15 18:24:06 +00:00
parent 955e1eb9e4
commit 66edcba185
18 changed files with 306 additions and 313 deletions

View File

@@ -4,11 +4,11 @@ struct HBMustacheContext: HBMustacheMethods {
var first: Bool
var last: Bool
var index: Int
init(first: Bool = false, last: Bool = false) {
self.first = first
self.last = last
self.index = 0
index = 0
}
/// Apply method to `HBMustacheContext`. These are available when processing elements
@@ -25,18 +25,17 @@ struct HBMustacheContext: HBMustacheMethods {
func runMethod(_ name: String) -> Any? {
switch name {
case "first":
return self.first
return first
case "last":
return self.last
return last
case "index":
return self.index
return index
case "even":
return (self.index & 1) == 0
return (index & 1) == 0
case "odd":
return (self.index & 1) == 1
return (index & 1) == 1
default:
return nil
}
}
}

View File

@@ -27,7 +27,7 @@ public struct HBMustacheLambda {
/// Initialize `HBMustacheLambda`
/// - Parameter cb: function to be called by lambda
public init(_ cb: @escaping Callback) {
self.callback = cb
callback = cb
}
internal func run(_ object: Any, _ template: HBMustacheTemplate) -> String {

View File

@@ -13,7 +13,7 @@ extension HBMustacheLibrary {
guard let enumerator = fs.enumerator(atPath: directory) else { return }
for case let path as String in enumerator {
guard path.hasSuffix(extWithDot) else { continue }
guard let data = fs.contents(atPath: directory + path) else { continue}
guard let data = fs.contents(atPath: directory + path) else { continue }
let string = String(decoding: data, as: Unicode.UTF8.self)
guard let template = try? HBMustacheTemplate(string: string) else {
logger?.error("Failed to load \(path)")

View File

@@ -9,7 +9,7 @@ import Logging
public class HBMustacheLibrary {
/// Initialize empty library
public init() {
self.templates = [:]
templates = [:]
}
/// Initialize library with contents of folder.
@@ -19,8 +19,8 @@ public class HBMustacheLibrary {
/// - Parameter directory: Directory to look for mustache templates
/// - Parameter extension: Extension of files to look for
public init(directory: String, withExtension extension: String = "mustache", logger: Logger? = nil) {
self.templates = [:]
self.loadTemplates(from: directory, withExtension: `extension`, logger: logger)
templates = [:]
loadTemplates(from: directory, withExtension: `extension`, logger: logger)
}
/// Register template under name
@@ -48,6 +48,6 @@ public class HBMustacheLibrary {
guard let template = templates[name] else { return nil }
return template.render(object)
}
private var templates: [String: HBMustacheTemplate]
}

View File

@@ -17,22 +17,22 @@ public protocol HBMustacheMethods {
func runMethod(_ name: String) -> Any?
}
extension StringProtocol {
public extension StringProtocol {
/// Apply method to String/Substring
///
/// Methods available are `capitalized`, `lowercased`, `uppercased` and `reversed`
/// - Parameter name: Method name
/// - Returns: Result
public func runMethod(_ name: String) -> Any? {
func runMethod(_ name: String) -> Any? {
switch name {
case "capitalized":
return self.capitalized
return capitalized
case "lowercased":
return self.lowercased()
return lowercased()
case "uppercased":
return self.uppercased()
return uppercased()
case "reversed":
return self.reversed()
return reversed()
default:
return nil
}
@@ -57,13 +57,13 @@ extension Array: HBMustacheMethods {
public func runMethod(_ name: String) -> Any? {
switch name {
case "first":
return self.first
return first
case "last":
return self.last
return last
case "reversed":
return self.reversed()
return reversed()
case "count":
return self.count
return count
default:
if let comparableSeq = self as? HBComparableSequence {
return comparableSeq.runComparableMethod(name)
@@ -77,7 +77,7 @@ extension Array: HBComparableSequence where Element: Comparable {
func runComparableMethod(_ name: String) -> Any? {
switch name {
case "sorted":
return self.sorted()
return sorted()
default:
return nil
}
@@ -94,9 +94,9 @@ extension Dictionary: HBMustacheMethods {
public func runMethod(_ name: String) -> Any? {
switch name {
case "count":
return self.count
return count
case "enumerated":
return self.map { (key: $0.key, value: $0.value) }
return map { (key: $0.key, value: $0.value) }
default:
if let comparableSeq = self as? HBComparableSequence {
return comparableSeq.runComparableMethod(name)
@@ -110,20 +110,20 @@ extension Dictionary: HBComparableSequence where Key: Comparable {
func runComparableMethod(_ name: String) -> Any? {
switch name {
case "sorted":
return self.map { (key: $0.key, value: $0.value) }.sorted { $0.key < $1.key }
return map { (key: $0.key, value: $0.value) }.sorted { $0.key < $1.key }
default:
return nil
}
}
}
extension FixedWidthInteger {
public extension FixedWidthInteger {
/// Apply method to FixedWidthInteger
///
/// Methods available are `plusone`, `minusone`, `odd`, `even`
/// - Parameter name: method name
/// - Returns: Result
public func runMethod(_ name: String) -> Any? {
func runMethod(_ name: String) -> Any? {
switch name {
case "plusone":
return self + 1

View File

@@ -17,4 +17,3 @@ private func unwrapOptional(_ object: Any) -> Any? {
guard let first = mirror.children.first else { return nil }
return first.value
}

View File

@@ -10,4 +10,3 @@ protocol HBMustacheParent {
extension Dictionary: HBMustacheParent where Key == String {
func child(named: String) -> Any? { return self[named] }
}

View File

@@ -23,10 +23,10 @@ struct HBParser {
if let buffer = utf8Data as? [UInt8] {
self.buffer = buffer
} else {
self.buffer = Array(utf8Data)
buffer = Array(utf8Data)
}
self.index = 0
self.range = 0..<self.buffer.endIndex
index = 0
range = 0 ..< buffer.endIndex
// should check that the data is valid utf8
if validateUTF8 == true, self.validateUTF8() == false {
@@ -35,19 +35,19 @@ struct HBParser {
}
init(_ string: String) {
self.buffer = Array(string.utf8)
self.index = 0
self.range = 0..<self.buffer.endIndex
buffer = Array(string.utf8)
index = 0
range = 0 ..< buffer.endIndex
}
/// Return contents of parser as a string
var count: Int {
return self.range.count
return range.count
}
/// Return contents of parser as a string
var string: String {
return makeString(self.buffer[self.range])
return makeString(buffer[range])
}
private var buffer: [UInt8]
@@ -60,12 +60,12 @@ struct HBParser {
extension HBParser {
/// initialise a parser that parses a section of the buffer attached to another parser
init(_ parser: HBParser, range: Range<Int>) {
self.buffer = parser.buffer
self.index = range.startIndex
buffer = parser.buffer
index = range.startIndex
self.range = range
precondition(range.startIndex >= 0 && range.endIndex <= self.buffer.endIndex)
precondition(self.buffer[range.startIndex] & 0xC0 != 0x80) // check we arent in the middle of a UTF8 character
precondition(range.startIndex >= 0 && range.endIndex <= buffer.endIndex)
precondition(buffer[range.startIndex] & 0xC0 != 0x80) // check we arent in the middle of a UTF8 character
}
/// initialise a parser that parses a section of the buffer attached to this parser
@@ -79,7 +79,7 @@ extension HBParser {
/// - Throws: .overflow
/// - Returns: Current character
mutating func character() throws -> Unicode.Scalar {
guard !self.reachedEnd() else { throw Error.overflow }
guard !reachedEnd() else { throw Error.overflow }
return unsafeCurrentAndAdvance()
}
@@ -88,9 +88,9 @@ extension HBParser {
/// - Throws: .overflow
/// - Returns: If current character was the one we expected
mutating func read(_ char: Unicode.Scalar) throws -> Bool {
let initialIndex = self.index
let initialIndex = index
let c = try character()
guard c == char else { self.index = initialIndex; return false }
guard c == char else { index = initialIndex; return false }
return true
}
@@ -99,9 +99,9 @@ extension HBParser {
/// - Throws: .overflow
/// - Returns: If current character is in character set
mutating func read(_ characterSet: Set<Unicode.Scalar>) throws -> Bool {
let initialIndex = self.index
let initialIndex = index
let c = try character()
guard characterSet.contains(c) else { self.index = initialIndex; return false }
guard characterSet.contains(c) else { index = initialIndex; return false }
return true
}
@@ -110,10 +110,10 @@ extension HBParser {
/// - Throws: .overflow, .emptyString
/// - Returns: If characters at current position equal string
mutating func read(_ string: String) throws -> Bool {
let initialIndex = self.index
let initialIndex = index
guard string.count > 0 else { throw Error.emptyString }
let subString = try read(count: string.count)
guard subString.string == string else { self.index = initialIndex; return false }
guard subString.string == string else { index = initialIndex; return false }
return true
}
@@ -123,14 +123,14 @@ extension HBParser {
/// - Returns: The string read from the buffer
mutating func read(count: Int) throws -> HBParser {
var count = count
var readEndIndex = self.index
var readEndIndex = index
while count > 0 {
guard readEndIndex != self.range.endIndex else { throw Error.overflow }
guard readEndIndex != range.endIndex else { throw Error.overflow }
readEndIndex = skipUTF8Character(at: readEndIndex)
count -= 1
}
let result = self.subParser(self.index..<readEndIndex)
self.index = readEndIndex
let result = subParser(index ..< readEndIndex)
index = readEndIndex
return result
}
@@ -139,10 +139,10 @@ extension HBParser {
/// - Throws: .overflow if we hit the end of the buffer before reading character
/// - Returns: String read from buffer
@discardableResult mutating func read(until: Unicode.Scalar, throwOnOverflow: Bool = true) throws -> HBParser {
let startIndex = self.index
while !self.reachedEnd() {
let startIndex = index
while !reachedEnd() {
if unsafeCurrent() == until {
return self.subParser(startIndex..<self.index)
return subParser(startIndex ..< index)
}
unsafeAdvance()
}
@@ -150,7 +150,7 @@ extension HBParser {
_setPosition(startIndex)
throw Error.overflow
}
return self.subParser(startIndex..<self.index)
return subParser(startIndex ..< index)
}
/// Read from buffer until we hit a character in supplied set. Position after this is of the character we were checking for
@@ -158,10 +158,10 @@ extension HBParser {
/// - Throws: .overflow
/// - Returns: String read from buffer
@discardableResult mutating func read(until characterSet: Set<Unicode.Scalar>, throwOnOverflow: Bool = true) throws -> HBParser {
let startIndex = self.index
while !self.reachedEnd() {
let startIndex = index
while !reachedEnd() {
if characterSet.contains(unsafeCurrent()) {
return self.subParser(startIndex..<self.index)
return subParser(startIndex ..< index)
}
unsafeAdvance()
}
@@ -169,7 +169,7 @@ extension HBParser {
_setPosition(startIndex)
throw Error.overflow
}
return self.subParser(startIndex..<self.index)
return subParser(startIndex ..< index)
}
/// Read from buffer until we hit a character that returns true for supplied closure. Position after this is of the character we were checking for
@@ -177,10 +177,10 @@ extension HBParser {
/// - Throws: .overflow
/// - Returns: String read from buffer
@discardableResult mutating func read(until: (Unicode.Scalar) -> Bool, throwOnOverflow: Bool = true) throws -> HBParser {
let startIndex = self.index
while !self.reachedEnd() {
let startIndex = index
while !reachedEnd() {
if until(unsafeCurrent()) {
return self.subParser(startIndex..<self.index)
return subParser(startIndex ..< index)
}
unsafeAdvance()
}
@@ -188,7 +188,7 @@ extension HBParser {
_setPosition(startIndex)
throw Error.overflow
}
return self.subParser(startIndex..<self.index)
return subParser(startIndex ..< index)
}
/// Read from buffer until we hit a character where supplied KeyPath is true. Position after this is of the character we were checking for
@@ -196,10 +196,10 @@ extension HBParser {
/// - Throws: .overflow
/// - Returns: String read from buffer
@discardableResult mutating func read(until keyPath: KeyPath<Unicode.Scalar, Bool>, throwOnOverflow: Bool = true) throws -> HBParser {
let startIndex = self.index
while !self.reachedEnd() {
let startIndex = index
while !reachedEnd() {
if unsafeCurrent()[keyPath: keyPath] {
return self.subParser(startIndex..<self.index)
return subParser(startIndex ..< index)
}
unsafeAdvance()
}
@@ -207,7 +207,7 @@ extension HBParser {
_setPosition(startIndex)
throw Error.overflow
}
return self.subParser(startIndex..<self.index)
return subParser(startIndex ..< index)
}
/// Read from buffer until we hit a string. By default the position after this is of the beginning of the string we were checking for
@@ -234,7 +234,7 @@ extension HBParser {
if skipToEnd == false {
index = foundIndex
}
let result = subParser(startIndex..<foundIndex)
let result = subParser(startIndex ..< foundIndex)
return result
}
} else {
@@ -246,16 +246,16 @@ extension HBParser {
_setPosition(startIndex)
throw Error.overflow
}
return subParser(startIndex..<index)
return subParser(startIndex ..< index)
}
}
/// Read from buffer from current position until the end of the buffer
/// - Returns: String read from buffer
@discardableResult mutating func readUntilTheEnd() -> HBParser {
let startIndex = self.index
self.index = self.range.endIndex
return self.subParser(startIndex..<self.index)
let startIndex = index
index = range.endIndex
return subParser(startIndex ..< index)
}
/// Read while character at current position is the one supplied
@@ -263,7 +263,7 @@ extension HBParser {
/// - Returns: String read from buffer
@discardableResult mutating func read(while: Unicode.Scalar) -> Int {
var count = 0
while !self.reachedEnd(),
while !reachedEnd(),
unsafeCurrent() == `while`
{
unsafeAdvance()
@@ -276,39 +276,39 @@ extension HBParser {
/// - Parameter while: character set to check
/// - Returns: String read from buffer
@discardableResult mutating func read(while characterSet: Set<Unicode.Scalar>) -> HBParser {
let startIndex = self.index
while !self.reachedEnd(),
let startIndex = index
while !reachedEnd(),
characterSet.contains(unsafeCurrent())
{
unsafeAdvance()
}
return self.subParser(startIndex..<self.index)
return subParser(startIndex ..< index)
}
/// Read while character returns true for supplied closure
/// - Parameter while: character set to check
/// - Returns: String read from buffer
@discardableResult mutating func read(while: (Unicode.Scalar) -> Bool) -> HBParser {
let startIndex = self.index
while !self.reachedEnd(),
let startIndex = index
while !reachedEnd(),
`while`(unsafeCurrent())
{
unsafeAdvance()
}
return self.subParser(startIndex..<self.index)
return subParser(startIndex ..< index)
}
/// Read while character returns true for supplied KeyPath
/// - Parameter while: character set to check
/// - Returns: String read from buffer
@discardableResult mutating func read(while keyPath: KeyPath<Unicode.Scalar, Bool>) -> HBParser {
let startIndex = self.index
while !self.reachedEnd(),
let startIndex = index
while !reachedEnd(),
unsafeCurrent()[keyPath: keyPath]
{
unsafeAdvance()
}
return self.subParser(startIndex..<self.index)
return subParser(startIndex ..< index)
}
/// Split parser into sections separated by character
@@ -316,14 +316,14 @@ extension HBParser {
/// - Returns: arrays of sub parsers
mutating func split(separator: Unicode.Scalar) -> [HBParser] {
var subParsers: [HBParser] = []
while !self.reachedEnd() {
while !reachedEnd() {
do {
let section = try read(until: separator)
subParsers.append(section)
unsafeAdvance()
} catch {
if !self.reachedEnd() {
subParsers.append(self.readUntilTheEnd())
if !reachedEnd() {
subParsers.append(readUntilTheEnd())
}
}
}
@@ -333,7 +333,7 @@ extension HBParser {
/// Return whether we have reached the end of the buffer
/// - Returns: Have we reached the end
func reachedEnd() -> Bool {
return self.index == self.range.endIndex
return index == range.endIndex
}
}
@@ -343,15 +343,15 @@ extension HBParser {
/// - Throws: .overflow
/// - Returns: Unicode.Scalar
func current() -> Unicode.Scalar {
guard !self.reachedEnd() else { return Unicode.Scalar(0) }
guard !reachedEnd() else { return Unicode.Scalar(0) }
return unsafeCurrent()
}
/// Move forward one character
/// - Throws: .overflow
mutating func advance() throws {
guard !self.reachedEnd() else { throw Error.overflow }
return self.unsafeAdvance()
guard !reachedEnd() else { throw Error.overflow }
return unsafeAdvance()
}
/// Move forward so many character
@@ -360,8 +360,8 @@ extension HBParser {
mutating func advance(by amount: Int) throws {
var amount = amount
while amount > 0 {
guard !self.reachedEnd() else { throw Error.overflow }
self.index = skipUTF8Character(at: self.index)
guard !reachedEnd() else { throw Error.overflow }
index = skipUTF8Character(at: index)
amount -= 1
}
}
@@ -369,8 +369,8 @@ extension HBParser {
/// Move backwards one character
/// - Throws: .overflow
mutating func retreat() throws {
guard self.index > self.range.startIndex else { throw Error.overflow }
self.index = backOneUTF8Character(at: self.index)
guard index > range.startIndex else { throw Error.overflow }
index = backOneUTF8Character(at: index)
}
/// Move back so many characters
@@ -379,20 +379,20 @@ extension HBParser {
mutating func retreat(by amount: Int) throws {
var amount = amount
while amount > 0 {
guard self.index > self.range.startIndex else { throw Error.overflow }
self.index = backOneUTF8Character(at: self.index)
guard index > range.startIndex else { throw Error.overflow }
index = backOneUTF8Character(at: index)
amount -= 1
}
}
mutating func unsafeAdvance() {
self.index = skipUTF8Character(at: self.index)
index = skipUTF8Character(at: index)
}
mutating func unsafeAdvance(by amount: Int) {
var amount = amount
while amount > 0 {
self.index = skipUTF8Character(at: self.index)
index = skipUTF8Character(at: index)
amount -= 1
}
}
@@ -416,8 +416,8 @@ extension HBParser: Sequence {
}
mutating func next() -> Unicode.Scalar? {
guard !self.parser.reachedEnd() else { return nil }
return self.parser.unsafeCurrentAndAdvance()
guard !parser.reachedEnd() else { return nil }
return parser.unsafeCurrentAndAdvance()
}
}
}
@@ -425,7 +425,7 @@ extension HBParser: Sequence {
// internal versions without checks
private extension HBParser {
func unsafeCurrent() -> Unicode.Scalar {
return decodeUTF8Character(at: self.index).0
return decodeUTF8Character(at: index).0
}
mutating func unsafeCurrentAndAdvance() -> Unicode.Scalar {
@@ -477,16 +477,16 @@ extension HBParser {
}
func skipUTF8Character(at index: Int) -> Int {
if self.buffer[index] & 0x80 != 0x80 { return index + 1 }
if self.buffer[index + 1] & 0xC0 == 0x80 { return index + 2 }
if self.buffer[index + 2] & 0xC0 == 0x80 { return index + 3 }
if buffer[index] & 0x80 != 0x80 { return index + 1 }
if buffer[index + 1] & 0xC0 == 0x80 { return index + 2 }
if buffer[index + 2] & 0xC0 == 0x80 { return index + 3 }
return index + 4
}
func backOneUTF8Character(at index: Int) -> Int {
if self.buffer[index - 1] & 0xC0 != 0x80 { return index - 1 }
if self.buffer[index - 2] & 0xC0 != 0x80 { return index - 2 }
if self.buffer[index - 3] & 0xC0 != 0x80 { return index - 3 }
if buffer[index - 1] & 0xC0 != 0x80 { return index - 1 }
if buffer[index - 2] & 0xC0 != 0x80 { return index - 2 }
if buffer[index - 3] & 0xC0 != 0x80 { return index - 3 }
return index - 4
}
@@ -526,9 +526,9 @@ extension HBParser {
/// return if the buffer is valid UTF8
func validateUTF8() -> Bool {
var index = self.range.startIndex
while index < self.range.endIndex {
let (scalar, newIndex) = self.validateUTF8Character(at: index)
var index = range.startIndex
while index < range.endIndex {
let (scalar, newIndex) = validateUTF8Character(at: index)
guard scalar != nil else { return false }
index = newIndex
}
@@ -598,17 +598,17 @@ extension HBParser {
return newIndex
}
guard self.index != self.range.endIndex else { return "" }
guard index != range.endIndex else { return "" }
do {
if #available(macOS 11, *) {
return try String(unsafeUninitializedCapacity: range.endIndex - index) { bytes -> Int in
return try _percentDecode(self.buffer[self.index..<range.endIndex], bytes)
try _percentDecode(self.buffer[self.index ..< range.endIndex], bytes)
}
} else {
let newBuffer = try [UInt8].init(unsafeUninitializedCapacity: self.range.endIndex - self.index) { bytes, count in
try count = _percentDecode(self.buffer[self.index..<range.endIndex], bytes)
let newBuffer = try [UInt8].init(unsafeUninitializedCapacity: range.endIndex - index) { bytes, count in
try count = _percentDecode(self.buffer[self.index ..< range.endIndex], bytes)
}
return self.makeString(newBuffer)
return makeString(newBuffer)
}
} catch {
return nil
@@ -622,8 +622,8 @@ extension Unicode.Scalar {
}
var isNewline: Bool {
switch self.value {
case 0x000A...0x000D /* LF ... CR */: return true
switch value {
case 0x000A ... 0x000D /* LF ... CR */: return true
case 0x0085 /* NEXT LINE (NEL) */: return true
case 0x2028 /* LINE SEPARATOR */: return true
case 0x2029 /* PARAGRAPH SEPARATOR */: return true
@@ -640,7 +640,7 @@ extension Unicode.Scalar {
}
var isLetterOrNumber: Bool {
return self.isLetter || self.isNumber
return isLetter || isNumber
}
}

View File

@@ -7,12 +7,12 @@ public protocol HBMustacheSequence {
func renderInvertedSection(with template: HBMustacheTemplate) -> String
}
extension Sequence {
public extension Sequence {
/// Render section using template
public func renderSection(with template: HBMustacheTemplate) -> String {
func renderSection(with template: HBMustacheTemplate) -> String {
var string = ""
var context = HBMustacheContext(first: true)
var iterator = self.makeIterator()
var iterator = makeIterator()
guard var currentObject = iterator.next() else { return "" }
while let object = iterator.next() {
@@ -24,19 +24,18 @@ extension Sequence {
context.last = true
string += template.render(currentObject, context: context)
return string
}
/// Render inverted section using template
public func renderInvertedSection(with template: HBMustacheTemplate) -> String {
func renderInvertedSection(with template: HBMustacheTemplate) -> String {
var iterator = makeIterator()
if iterator.next() == nil {
return template.render(self)
}
return ""
}
}
extension Array: HBMustacheSequence {}

View File

@@ -10,7 +10,7 @@ extension String {
/// HTML escape string. Replace '<', '>' and '&' with HTML escaped versions
func htmlEscape() -> String {
var newString = ""
newString.reserveCapacity(self.count)
newString.reserveCapacity(count)
// currently doing this by going through each character could speed
// this us by treating as an array of UInt8's
for c in self {

View File

@@ -83,7 +83,7 @@ extension HBMustacheTemplate {
/// parse variable name
static func parseName(_ parser: inout HBParser) throws -> (String, String?) {
parser.read(while: \.isWhitespace)
var 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 }
// does the name include brackets. If so this is a method call
@@ -106,7 +106,7 @@ extension HBMustacheTemplate {
let text = try parser.read(untilString: "}}", throwOnOverflow: true, skipToEnd: true)
return text.string
}
private static let sectionNameCharsWithoutBrackets = Set<Unicode.Scalar>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._?")
private static let sectionNameChars = Set<Unicode.Scalar>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._?()")
}

View File

@@ -9,9 +9,9 @@ extension HBMustacheTemplate {
var string = ""
for token in tokens {
switch token {
case .text(let text):
case let .text(text):
string += text
case .variable(let variable, let method):
case let .variable(variable, method):
if let child = getChild(named: variable, from: object, method: method, context: context) {
if let template = child as? HBMustacheTemplate {
string += template.render(object)
@@ -19,19 +19,19 @@ extension HBMustacheTemplate {
string += String(describing: child).htmlEscape()
}
}
case .unescapedVariable(let variable, let method):
case let .unescapedVariable(variable, method):
if let child = getChild(named: variable, from: object, method: method, context: context) {
string += String(describing: child)
}
case .section(let variable, let method, let template):
case let .section(variable, method, template):
let child = getChild(named: variable, from: object, method: method, context: context)
string += renderSection(child, parent: object, with: template)
case .invertedSection(let variable, let method, let template):
case let .invertedSection(variable, method, template):
let child = getChild(named: variable, from: object, method: method, context: context)
string += renderInvertedSection(child, parent: object, with: template)
case .partial(let name):
case let .partial(name):
if let text = library?.render(object, withTemplate: name) {
string += text
}
@@ -54,13 +54,13 @@ extension HBMustacheTemplate {
return bool ? template.render(parent) : ""
case let lambda as HBMustacheLambda:
return lambda.run(parent, template)
case .some(let value):
case let .some(value):
return template.render(value)
case .none:
return ""
}
}
/// Render an inverted section
/// - Parameters:
/// - child: Object to render section for
@@ -111,7 +111,8 @@ extension HBMustacheTemplate {
// if we want to run a method and the current child can have methods applied to it then
// run method on the current child
if let method = method,
let runnable = child as? HBMustacheMethods {
let runnable = child as? HBMustacheMethods
{
if let result = runnable.runMethod(method) {
return result
}
@@ -119,4 +120,3 @@ extension HBMustacheTemplate {
return child
}
}

View File

@@ -4,16 +4,16 @@ public class HBMustacheTemplate {
/// - Parameter string: Template text
/// - Throws: HBMustacheTemplate.Error
public init(string: String) throws {
self.tokens = try Self.parse(string)
tokens = try Self.parse(string)
}
/// Render object using this template
/// - Parameter object: Object to render
/// - Returns: Rendered text
public func render(_ object: Any) -> String {
self.render(object, context: nil)
render(object, context: nil)
}
internal init(_ tokens: [Token]) {
self.tokens = tokens
}
@@ -22,14 +22,14 @@ public class HBMustacheTemplate {
self.library = library
for token in tokens {
switch token {
case .section(_, _, let template), .invertedSection(_, _, let template):
case let .section(_, _, template), let .invertedSection(_, _, template):
template.setLibrary(library)
default:
break
}
}
}
enum Token {
case text(String)
case variable(name: String, method: String? = nil)
@@ -42,4 +42,3 @@ public class HBMustacheTemplate {
let tokens: [Token]
var library: HBMustacheLibrary?
}