method -> transform
This commit is contained in:
@@ -20,7 +20,7 @@ struct HBMustacheSequenceContext: HBMustacheTransformable {
|
||||
/// ```
|
||||
///
|
||||
/// Transforms available are `first`, `last`, `index`, `even` and `odd`
|
||||
/// - Parameter name: Method name
|
||||
/// - Parameter name: transform name
|
||||
/// - Returns: Result
|
||||
func transform(_ name: String) -> Any? {
|
||||
switch name {
|
||||
|
||||
@@ -26,7 +26,7 @@ extension HBMustacheTemplate {
|
||||
|
||||
struct ParserState {
|
||||
var sectionName: String?
|
||||
var sectionMethod: String?
|
||||
var sectionTransform: String?
|
||||
var newLine: Bool
|
||||
var startDelimiter: String
|
||||
var endDelimiter: String
|
||||
@@ -38,10 +38,10 @@ extension HBMustacheTemplate {
|
||||
self.endDelimiter = "}}"
|
||||
}
|
||||
|
||||
func withSectionName(_ name: String, method: String? = nil) -> ParserState {
|
||||
func withSectionName(_ name: String, transform: String? = nil) -> ParserState {
|
||||
var newValue = self
|
||||
newValue.sectionName = name
|
||||
newValue.sectionMethod = method
|
||||
newValue.sectionTransform = transform
|
||||
return newValue
|
||||
}
|
||||
|
||||
@@ -104,50 +104,50 @@ extension HBMustacheTemplate {
|
||||
case "#":
|
||||
// section
|
||||
parser.unsafeAdvance()
|
||||
let (name, method) = try parseName(&parser, state: state)
|
||||
let (name, transform) = try parseName(&parser, state: state)
|
||||
if self.isStandalone(&parser, state: state) {
|
||||
setNewLine = true
|
||||
} else if whiteSpaceBefore.count > 0 {
|
||||
tokens.append(.text(String(whiteSpaceBefore)))
|
||||
whiteSpaceBefore = ""
|
||||
}
|
||||
let sectionTokens = try parse(&parser, state: state.withSectionName(name, method: method))
|
||||
tokens.append(.section(name: name, method: method, template: HBMustacheTemplate(sectionTokens)))
|
||||
let sectionTokens = try parse(&parser, state: state.withSectionName(name, transform: transform))
|
||||
tokens.append(.section(name: name, transform: transform, template: HBMustacheTemplate(sectionTokens)))
|
||||
|
||||
case "^":
|
||||
// inverted section
|
||||
parser.unsafeAdvance()
|
||||
let (name, method) = try parseName(&parser, state: state)
|
||||
let (name, transform) = try parseName(&parser, state: state)
|
||||
if self.isStandalone(&parser, state: state) {
|
||||
setNewLine = true
|
||||
} else if whiteSpaceBefore.count > 0 {
|
||||
tokens.append(.text(String(whiteSpaceBefore)))
|
||||
whiteSpaceBefore = ""
|
||||
}
|
||||
let sectionTokens = try parse(&parser, state: state.withSectionName(name, method: method))
|
||||
tokens.append(.invertedSection(name: name, method: method, template: HBMustacheTemplate(sectionTokens)))
|
||||
let sectionTokens = try parse(&parser, state: state.withSectionName(name, transform: transform))
|
||||
tokens.append(.invertedSection(name: name, transform: transform, template: HBMustacheTemplate(sectionTokens)))
|
||||
|
||||
case "$":
|
||||
// inherited section
|
||||
parser.unsafeAdvance()
|
||||
let (name, method) = try parseName(&parser, state: state)
|
||||
// ERROR: can't have methods applied to inherited sections
|
||||
guard method == nil else { throw Error.transformAppliedToInheritanceSection }
|
||||
let (name, transform) = try parseName(&parser, state: state)
|
||||
// ERROR: can't have transform applied to inherited sections
|
||||
guard transform == nil else { throw Error.transformAppliedToInheritanceSection }
|
||||
if self.isStandalone(&parser, state: state) {
|
||||
setNewLine = true
|
||||
} else if whiteSpaceBefore.count > 0 {
|
||||
tokens.append(.text(String(whiteSpaceBefore)))
|
||||
whiteSpaceBefore = ""
|
||||
}
|
||||
let sectionTokens = try parse(&parser, state: state.withSectionName(name, method: method))
|
||||
let sectionTokens = try parse(&parser, state: state.withSectionName(name, transform: transform))
|
||||
tokens.append(.inheritedSection(name: name, template: HBMustacheTemplate(sectionTokens)))
|
||||
|
||||
case "/":
|
||||
// end of section
|
||||
parser.unsafeAdvance()
|
||||
let position = parser.position
|
||||
let (name, method) = try parseName(&parser, state: state)
|
||||
guard name == state.sectionName, method == state.sectionMethod else {
|
||||
let (name, transform) = try parseName(&parser, state: state)
|
||||
guard name == state.sectionName, transform == state.sectionTransform else {
|
||||
parser.unsafeSetPosition(position)
|
||||
throw Error.sectionCloseNameIncorrect
|
||||
}
|
||||
@@ -172,9 +172,9 @@ extension HBMustacheTemplate {
|
||||
whiteSpaceBefore = ""
|
||||
}
|
||||
parser.unsafeAdvance()
|
||||
let (name, method) = try parseName(&parser, state: state)
|
||||
let (name, transform) = try parseName(&parser, state: state)
|
||||
guard try parser.read("}") else { throw Error.unfinishedName }
|
||||
tokens.append(.unescapedVariable(name: name, method: method))
|
||||
tokens.append(.unescapedVariable(name: name, transform: transform))
|
||||
|
||||
case "&":
|
||||
// unescaped variable
|
||||
@@ -183,8 +183,8 @@ extension HBMustacheTemplate {
|
||||
whiteSpaceBefore = ""
|
||||
}
|
||||
parser.unsafeAdvance()
|
||||
let (name, method) = try parseName(&parser, state: state)
|
||||
tokens.append(.unescapedVariable(name: name, method: method))
|
||||
let (name, transform) = try parseName(&parser, state: state)
|
||||
tokens.append(.unescapedVariable(name: name, transform: transform))
|
||||
|
||||
case ">":
|
||||
// partial
|
||||
@@ -204,9 +204,9 @@ extension HBMustacheTemplate {
|
||||
case "<":
|
||||
// partial with inheritance
|
||||
parser.unsafeAdvance()
|
||||
let (name, method) = try parseName(&parser, state: state)
|
||||
// ERROR: can't have methods applied to inherited sections
|
||||
guard method == nil else { throw Error.transformAppliedToInheritanceSection }
|
||||
let (name, transform) = try parseName(&parser, state: state)
|
||||
// ERROR: can't have transform applied to inherited sections
|
||||
guard transform == nil else { throw Error.transformAppliedToInheritanceSection }
|
||||
var indent: String?
|
||||
if self.isStandalone(&parser, state: state) {
|
||||
setNewLine = true
|
||||
@@ -215,8 +215,9 @@ extension HBMustacheTemplate {
|
||||
tokens.append(.text(indent!))
|
||||
whiteSpaceBefore = ""
|
||||
}
|
||||
let sectionTokens = try parse(&parser, state: state.withSectionName(name, method: method))
|
||||
let sectionTokens = try parse(&parser, state: state.withSectionName(name, transform: transform))
|
||||
var inherit: [String: HBMustacheTemplate] = [:]
|
||||
// parse tokens in section to extract inherited sections
|
||||
for token in sectionTokens {
|
||||
switch token {
|
||||
case .inheritedSection(let name, let template):
|
||||
@@ -241,8 +242,8 @@ extension HBMustacheTemplate {
|
||||
tokens.append(.text(String(whiteSpaceBefore)))
|
||||
whiteSpaceBefore = ""
|
||||
}
|
||||
let (name, method) = try parseName(&parser, state: state)
|
||||
tokens.append(.variable(name: name, method: method))
|
||||
let (name, transform) = try parseName(&parser, state: state)
|
||||
tokens.append(.variable(name: name, transform: transform))
|
||||
}
|
||||
state.newLine = setNewLine
|
||||
}
|
||||
@@ -285,7 +286,7 @@ extension HBMustacheTemplate {
|
||||
parser.read(while: \.isWhitespace)
|
||||
guard try parser.read(string: state.endDelimiter) else { throw Error.unfinishedName }
|
||||
|
||||
// does the name include brackets. If so this is a method call
|
||||
// does the name include brackets. If so this is a transform call
|
||||
var nameParser = HBParser(String(text))
|
||||
let string = nameParser.read(while: self.sectionNameCharsWithoutBrackets)
|
||||
if nameParser.reachedEnd() {
|
||||
|
||||
@@ -27,24 +27,24 @@ extension HBMustacheTemplate {
|
||||
switch token {
|
||||
case .text(let text):
|
||||
return text
|
||||
case .variable(let variable, let method):
|
||||
if let child = getChild(named: variable, method: method, context: context) {
|
||||
case .variable(let variable, let transform):
|
||||
if let child = getChild(named: variable, transform: transform, context: context) {
|
||||
if let template = child as? HBMustacheTemplate {
|
||||
return template.render(context: context)
|
||||
} else {
|
||||
return String(describing: child).htmlEscape()
|
||||
}
|
||||
}
|
||||
case .unescapedVariable(let variable, let method):
|
||||
if let child = getChild(named: variable, method: method, context: context) {
|
||||
case .unescapedVariable(let variable, let transform):
|
||||
if let child = getChild(named: variable, transform: transform, context: context) {
|
||||
return String(describing: child)
|
||||
}
|
||||
case .section(let variable, let method, let template):
|
||||
let child = self.getChild(named: variable, method: method, context: context)
|
||||
case .section(let variable, let transform, let template):
|
||||
let child = self.getChild(named: variable, transform: transform, context: context)
|
||||
return self.renderSection(child, with: template, context: context)
|
||||
|
||||
case .invertedSection(let variable, let method, let template):
|
||||
let child = self.getChild(named: variable, method: method, context: context)
|
||||
case .invertedSection(let variable, let transform, let template):
|
||||
let child = self.getChild(named: variable, transform: transform, context: context)
|
||||
return self.renderInvertedSection(child, with: template, context: context)
|
||||
|
||||
case .inheritedSection(let name, let template):
|
||||
@@ -103,7 +103,7 @@ extension HBMustacheTemplate {
|
||||
}
|
||||
|
||||
/// Get child object from variable name
|
||||
func getChild(named name: String, method: String?, context: HBMustacheContext) -> Any? {
|
||||
func getChild(named name: String, transform: String?, context: HBMustacheContext) -> Any? {
|
||||
func _getImmediateChild(named name: String, from object: Any) -> Any? {
|
||||
if let customBox = object as? HBMustacheParent {
|
||||
return customBox.child(named: name)
|
||||
@@ -132,23 +132,23 @@ extension HBMustacheTemplate {
|
||||
}
|
||||
|
||||
// work out which object to access. "." means the current object, if the variable name is ""
|
||||
// and we have a method to run on the variable then we need the context object, otherwise
|
||||
// and we have a transform to run on the variable then we need the context object, otherwise
|
||||
// the name is split by "." and we use mirror to get the correct child object
|
||||
let child: Any?
|
||||
if name == "." {
|
||||
child = context.stack.last!
|
||||
} else if name == "", method != nil {
|
||||
} else if name == "", transform != nil {
|
||||
child = context.sequenceContext
|
||||
} else {
|
||||
let nameSplit = name.split(separator: ".").map { String($0) }
|
||||
child = _getChildInStack(named: nameSplit[...], from: context.stack)
|
||||
}
|
||||
// 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,
|
||||
// if we want to run a transform and the current child can have transforms applied to it then
|
||||
// run transform on the current child
|
||||
if let transform = transform,
|
||||
let runnable = child as? HBMustacheTransformable
|
||||
{
|
||||
if let result = runnable.transform(method) {
|
||||
if let result = runnable.transform(transform) {
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,10 +34,10 @@ public final class HBMustacheTemplate {
|
||||
|
||||
enum Token {
|
||||
case text(String)
|
||||
case variable(name: String, method: String? = nil)
|
||||
case unescapedVariable(name: String, method: String? = nil)
|
||||
case section(name: String, method: String? = nil, template: HBMustacheTemplate)
|
||||
case invertedSection(name: String, method: String? = nil, template: HBMustacheTemplate)
|
||||
case variable(name: String, transform: String? = nil)
|
||||
case unescapedVariable(name: String, transform: String? = nil)
|
||||
case section(name: String, transform: String? = nil, template: HBMustacheTemplate)
|
||||
case invertedSection(name: String, transform: String? = nil, template: HBMustacheTemplate)
|
||||
case inheritedSection(name: String, template: HBMustacheTemplate)
|
||||
case partial(String, indentation: String?, inherits: [String: HBMustacheTemplate]?)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/// Objects that can have a transforms run on them. Mustache transforms are specific to this implementation
|
||||
/// of Mustache. They allow you to process objects before they are rendered.
|
||||
///
|
||||
/// The syntax for applying transforms is `{{method(variable)}}`. Transforms can be applied to both
|
||||
/// The syntax for applying transforms is `{{transform(variable)}}`. Transforms can be applied to both
|
||||
/// variables, sections and inverted sections.
|
||||
///
|
||||
/// A simple example would be ensuring a string is lowercase.
|
||||
@@ -21,7 +21,7 @@ public extension StringProtocol {
|
||||
/// Transform String/Substring
|
||||
///
|
||||
/// Transforms available are `capitalized`, `lowercased`, `uppercased` and `reversed`
|
||||
/// - Parameter name: Method name
|
||||
/// - Parameter name: transform name
|
||||
/// - Returns: Result
|
||||
func transform(_ name: String) -> Any? {
|
||||
switch name {
|
||||
@@ -52,7 +52,7 @@ extension Array: HBMustacheTransformable {
|
||||
///
|
||||
/// Transforms available are `first`, `last`, `reversed`, `count` and for arrays
|
||||
/// with comparable elements `sorted`.
|
||||
/// - Parameter name: method name
|
||||
/// - Parameter name: transform name
|
||||
/// - Returns: Result
|
||||
public func transform(_ name: String) -> Any? {
|
||||
switch name {
|
||||
@@ -89,7 +89,7 @@ extension Dictionary: HBMustacheTransformable {
|
||||
///
|
||||
/// Transforms available are `count`, `enumerated` and for dictionaries
|
||||
/// with comparable keys `sorted`.
|
||||
/// - Parameter name: method name
|
||||
/// - Parameter name: transform name
|
||||
/// - Returns: Result
|
||||
public func transform(_ name: String) -> Any? {
|
||||
switch name {
|
||||
@@ -121,7 +121,7 @@ public extension FixedWidthInteger {
|
||||
/// Transform FixedWidthInteger
|
||||
///
|
||||
/// Transforms available are `plusone`, `minusone`, `odd`, `even`
|
||||
/// - Parameter name: method name
|
||||
/// - Parameter name: transform name
|
||||
/// - Returns: Result
|
||||
func transform(_ name: String) -> Any? {
|
||||
switch name {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import HummingbirdMustache
|
||||
import XCTest
|
||||
|
||||
final class MethodTests: XCTestCase {
|
||||
final class TransformTests: XCTestCase {
|
||||
func testLowercased() throws {
|
||||
let template = try HBMustacheTemplate(string: """
|
||||
{{ lowercased(name) }}
|
||||
Reference in New Issue
Block a user