Add support for custom text escaping (#11)

* Add support for custom text escaping

* swift format

* Remove withDefaultDelimiters

* Update README.md

* Don't pass content type into partial
This commit is contained in:
Adam Fowler
2021-03-23 17:36:28 +00:00
committed by GitHub
parent ef4eb40eb7
commit d3edef1b8e
9 changed files with 164 additions and 16 deletions

View File

@@ -4,6 +4,7 @@ struct HBMustacheContext {
let sequenceContext: HBMustacheSequenceContext?
let indentation: String?
let inherited: [String: HBMustacheTemplate]?
let contentType: HBMustacheContentType
/// initialize context with a single objectt
init(_ object: Any) {
@@ -11,25 +12,34 @@ struct HBMustacheContext {
self.sequenceContext = nil
self.indentation = nil
self.inherited = nil
self.contentType = HBHTMLContentType()
}
private init(
stack: [Any],
sequenceContext: HBMustacheSequenceContext?,
indentation: String?,
inherited: [String: HBMustacheTemplate]?
inherited: [String: HBMustacheTemplate]?,
contentType: HBMustacheContentType
) {
self.stack = stack
self.sequenceContext = sequenceContext
self.indentation = indentation
self.inherited = inherited
self.contentType = contentType
}
/// return context with object add to stack
func withObject(_ object: Any) -> HBMustacheContext {
var stack = self.stack
stack.append(object)
return .init(stack: stack, sequenceContext: nil, indentation: self.indentation, inherited: self.inherited)
return .init(
stack: stack,
sequenceContext: nil,
indentation: self.indentation,
inherited: self.inherited,
contentType: self.contentType
)
}
/// return context with indent and parameter information for invoking a partial
@@ -50,13 +60,36 @@ struct HBMustacheContext {
} else {
inherits = self.inherited
}
return .init(stack: self.stack, sequenceContext: nil, indentation: indentation, inherited: inherits)
return .init(
stack: self.stack,
sequenceContext: nil,
indentation: indentation,
inherited: inherits,
contentType: HBHTMLContentType()
)
}
/// return context with sequence info and sequence element added to stack
func withSequence(_ object: Any, sequenceContext: HBMustacheSequenceContext) -> HBMustacheContext {
var stack = self.stack
stack.append(object)
return .init(stack: stack, sequenceContext: sequenceContext, indentation: self.indentation, inherited: self.inherited)
return .init(
stack: stack,
sequenceContext: sequenceContext,
indentation: self.indentation,
inherited: self.inherited,
contentType: self.contentType
)
}
/// return context with sequence info and sequence element added to stack
func withContentType(_ contentType: HBMustacheContentType) -> HBMustacheContext {
return .init(
stack: self.stack,
sequenceContext: self.sequenceContext,
indentation: self.indentation,
inherited: self.inherited,
contentType: contentType
)
}
}