Tidy standalone processing
This commit is contained in:
@@ -406,6 +406,10 @@ extension HBParser {
|
||||
}
|
||||
|
||||
mutating func setPosition(_ index: Int) throws {
|
||||
if index == self.range.endIndex {
|
||||
_setPosition(index)
|
||||
return
|
||||
}
|
||||
guard range.contains(index) else { throw Error.invalidPosition }
|
||||
guard validateUTF8Character(at: index).0 != nil else { throw Error.invalidPosition }
|
||||
_setPosition(index)
|
||||
|
||||
@@ -82,11 +82,8 @@ extension HBMustacheTemplate {
|
||||
// section
|
||||
parser.unsafeAdvance()
|
||||
let (name, method) = try parseName(&parser, state: state)
|
||||
if state.newLine, hasLineFinished(&parser) {
|
||||
if isStandalone(&parser, state: state) {
|
||||
setNewLine = true
|
||||
if parser.current() == "\n" {
|
||||
parser.unsafeAdvance()
|
||||
}
|
||||
} else if whiteSpaceBefore.count > 0 {
|
||||
tokens.append(.text(whiteSpaceBefore))
|
||||
whiteSpaceBefore = ""
|
||||
@@ -98,11 +95,8 @@ extension HBMustacheTemplate {
|
||||
// inverted section
|
||||
parser.unsafeAdvance()
|
||||
let (name, method) = try parseName(&parser, state: state)
|
||||
if state.newLine, hasLineFinished(&parser) {
|
||||
if isStandalone(&parser, state: state) {
|
||||
setNewLine = true
|
||||
if parser.current() == "\n" {
|
||||
parser.unsafeAdvance()
|
||||
}
|
||||
} else if whiteSpaceBefore.count > 0 {
|
||||
tokens.append(.text(whiteSpaceBefore))
|
||||
whiteSpaceBefore = ""
|
||||
@@ -117,11 +111,8 @@ extension HBMustacheTemplate {
|
||||
guard name == state.sectionName else {
|
||||
throw Error.sectionCloseNameIncorrect
|
||||
}
|
||||
if state.newLine, hasLineFinished(&parser) {
|
||||
if isStandalone(&parser, state: state) {
|
||||
setNewLine = true
|
||||
if parser.current() == "\n" {
|
||||
parser.unsafeAdvance()
|
||||
}
|
||||
} else if whiteSpaceBefore.count > 0 {
|
||||
tokens.append(.text(whiteSpaceBefore))
|
||||
whiteSpaceBefore = ""
|
||||
@@ -132,12 +123,7 @@ extension HBMustacheTemplate {
|
||||
// comment
|
||||
parser.unsafeAdvance()
|
||||
_ = try parseComment(&parser, state: state)
|
||||
if state.newLine, hasLineFinished(&parser) {
|
||||
setNewLine = true
|
||||
if !parser.reachedEnd() {
|
||||
parser.unsafeAdvance()
|
||||
}
|
||||
}
|
||||
setNewLine = isStandalone(&parser, state: state)
|
||||
|
||||
case "{":
|
||||
// unescaped variable
|
||||
@@ -167,11 +153,8 @@ extension HBMustacheTemplate {
|
||||
if whiteSpaceBefore.count > 0 {
|
||||
tokens.append(.text(whiteSpaceBefore))
|
||||
}
|
||||
if state.newLine, hasLineFinished(&parser) {
|
||||
if isStandalone(&parser, state: state) {
|
||||
setNewLine = true
|
||||
if parser.current() == "\n" {
|
||||
parser.unsafeAdvance()
|
||||
}
|
||||
tokens.append(.partial(name, indentation: whiteSpaceBefore))
|
||||
} else {
|
||||
tokens.append(.partial(name, indentation: nil))
|
||||
@@ -182,12 +165,7 @@ extension HBMustacheTemplate {
|
||||
// set delimiter
|
||||
parser.unsafeAdvance()
|
||||
state = try parserSetDelimiter(&parser, state: state)
|
||||
if state.newLine, hasLineFinished(&parser) {
|
||||
setNewLine = true
|
||||
if !parser.reachedEnd() {
|
||||
parser.unsafeAdvance()
|
||||
}
|
||||
}
|
||||
setNewLine = isStandalone(&parser, state: state)
|
||||
|
||||
default:
|
||||
// variable
|
||||
@@ -278,12 +256,17 @@ extension HBMustacheTemplate {
|
||||
if parser.reachedEnd() { return true }
|
||||
parser2.read(while: Set(" \t\r"))
|
||||
if parser2.current() == "\n" {
|
||||
parser2.unsafeAdvance()
|
||||
try! parser.setPosition(parser2.getPosition())
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
static func isStandalone(_ parser: inout HBParser, state: ParserState) -> Bool {
|
||||
return state.newLine && hasLineFinished(&parser)
|
||||
}
|
||||
|
||||
private static let sectionNameCharsWithoutBrackets = Set<Unicode.Scalar>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._?")
|
||||
private static let sectionNameChars = Set<Unicode.Scalar>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._?()")
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ extension HBMustacheTemplate {
|
||||
/// - Returns: Rendered text
|
||||
func render(_ stack: [Any], context: HBMustacheContext? = nil, indentation: String? = nil) -> String {
|
||||
var string = ""
|
||||
if let indentation = indentation {
|
||||
if let indentation = indentation, indentation != "" {
|
||||
for token in tokens {
|
||||
if string.last == "\n" {
|
||||
string += indentation
|
||||
|
||||
Reference in New Issue
Block a user