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

@@ -22,6 +22,10 @@ extension HBMustacheTemplate {
case illegalTokenInsideInheritSection
/// text found inside inherit section of partial
case textInsideInheritSection
/// config variable syntax is wrong
case invalidConfigVariableSyntax
/// unrecognised config variable
case unrecognisedConfigVariable
}
struct ParserState {
@@ -51,13 +55,6 @@ extension HBMustacheTemplate {
newValue.endDelimiter = end
return newValue
}
func withDefaultDelimiters(start _: String, end _: String) -> ParserState {
var newValue = self
newValue.startDelimiter = "{{"
newValue.endDelimiter = "}}"
return newValue
}
}
/// parse mustache text to generate a list of tokens
@@ -236,6 +233,14 @@ extension HBMustacheTemplate {
state = try self.parserSetDelimiter(&parser, state: state)
setNewLine = self.isStandalone(&parser, state: state)
case "%":
// read config variable
parser.unsafeAdvance()
if let token = try self.readConfigVariable(&parser, state: state) {
tokens.append(token)
}
setNewLine = self.isStandalone(&parser, state: state)
default:
// variable
if whiteSpaceBefore.count > 0 {
@@ -327,6 +332,34 @@ extension HBMustacheTemplate {
return state.withDelimiters(start: String(startDelimiter), end: String(endDelimiter))
}
static func readConfigVariable(_ parser: inout HBParser, state: ParserState) throws -> Token? {
let variable: Substring
let value: Substring
do {
parser.read(while: \.isWhitespace)
variable = parser.read(while: self.sectionNameCharsWithoutBrackets)
parser.read(while: \.isWhitespace)
guard try parser.read(":") else { throw Error.invalidConfigVariableSyntax }
parser.read(while: \.isWhitespace)
value = parser.read(while: self.sectionNameCharsWithoutBrackets)
guard try parser.read(string: state.endDelimiter) else { throw Error.invalidConfigVariableSyntax }
} catch {
throw Error.invalidConfigVariableSyntax
}
// do both variable and value have content
guard variable.count > 0, value.count > 0 else { throw Error.invalidConfigVariableSyntax }
switch variable {
case "CONTENT_TYPE":
guard let contentType = HBMustacheContentTypes.get(String(value)) else { throw Error.unrecognisedConfigVariable }
return .contentType(contentType)
default:
throw Error.unrecognisedConfigVariable
}
}
static func hasLineFinished(_ parser: inout HBParser) -> Bool {
var parser2 = parser
if parser.reachedEnd() { return true }