Use special set of chars for partial filename (#18)
This commit is contained in:
@@ -199,7 +199,7 @@ extension HBMustacheTemplate {
|
|||||||
case ">":
|
case ">":
|
||||||
// partial
|
// partial
|
||||||
parser.unsafeAdvance()
|
parser.unsafeAdvance()
|
||||||
let (name, _) = try parseName(&parser, state: state)
|
let name = try parsePartialName(&parser, state: state)
|
||||||
if whiteSpaceBefore.count > 0 {
|
if whiteSpaceBefore.count > 0 {
|
||||||
tokens.append(.text(String(whiteSpaceBefore)))
|
tokens.append(.text(String(whiteSpaceBefore)))
|
||||||
}
|
}
|
||||||
@@ -214,9 +214,7 @@ extension HBMustacheTemplate {
|
|||||||
case "<":
|
case "<":
|
||||||
// partial with inheritance
|
// partial with inheritance
|
||||||
parser.unsafeAdvance()
|
parser.unsafeAdvance()
|
||||||
let (name, transform) = try parseName(&parser, state: state)
|
let name = try parsePartialName(&parser, state: state)
|
||||||
// ERROR: can't have transform applied to inherited sections
|
|
||||||
guard transform == nil else { throw Error.transformAppliedToInheritanceSection }
|
|
||||||
var indent: String?
|
var indent: String?
|
||||||
if self.isStandalone(&parser, state: state) {
|
if self.isStandalone(&parser, state: state) {
|
||||||
setNewLine = true
|
setNewLine = true
|
||||||
@@ -225,7 +223,7 @@ extension HBMustacheTemplate {
|
|||||||
tokens.append(.text(indent!))
|
tokens.append(.text(indent!))
|
||||||
whiteSpaceBefore = ""
|
whiteSpaceBefore = ""
|
||||||
}
|
}
|
||||||
let sectionTokens = try parse(&parser, state: state.withSectionName(name, transform: transform))
|
let sectionTokens = try parse(&parser, state: state.withSectionName(name))
|
||||||
var inherit: [String: HBMustacheTemplate] = [:]
|
var inherit: [String: HBMustacheTemplate] = [:]
|
||||||
// parse tokens in section to extract inherited sections
|
// parse tokens in section to extract inherited sections
|
||||||
for token in sectionTokens {
|
for token in sectionTokens {
|
||||||
@@ -321,6 +319,15 @@ extension HBMustacheTemplate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// parse partial name
|
||||||
|
static func parsePartialName(_ parser: inout HBParser, state: ParserState) throws -> String {
|
||||||
|
parser.read(while: \.isWhitespace)
|
||||||
|
let text = String(parser.read(while: self.sectionNameChars))
|
||||||
|
parser.read(while: \.isWhitespace)
|
||||||
|
guard try parser.read(string: state.endDelimiter) else { throw Error.unfinishedName }
|
||||||
|
return text
|
||||||
|
}
|
||||||
|
|
||||||
static func parseComment(_ parser: inout HBParser, state: ParserState) throws -> String {
|
static func parseComment(_ parser: inout HBParser, state: ParserState) throws -> String {
|
||||||
let text = try parser.read(untilString: state.endDelimiter, throwOnOverflow: true, skipToEnd: true)
|
let text = try parser.read(untilString: state.endDelimiter, throwOnOverflow: true, skipToEnd: true)
|
||||||
return String(text)
|
return String(text)
|
||||||
@@ -390,6 +397,7 @@ extension HBMustacheTemplate {
|
|||||||
return state.newLine && self.hasLineFinished(&parser)
|
return state.newLine && self.hasLineFinished(&parser)
|
||||||
}
|
}
|
||||||
|
|
||||||
private static let sectionNameCharsWithoutBrackets = Set<Character>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._?")
|
private static let sectionNameCharsWithoutBrackets = Set<Character>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_?")
|
||||||
private static let sectionNameChars = Set<Character>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._?()")
|
private static let sectionNameChars = Set<Character>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_?()")
|
||||||
|
private static let partialNameChars = Set<Character>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_()")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,24 @@ final class LibraryTests: XCTestCase {
|
|||||||
XCTAssertEqual(library.render(object, withTemplate: "test"), "<test><value>value1</value><value>value2</value></test>")
|
XCTAssertEqual(library.render(object, withTemplate: "test"), "<test><value>value1</value><value>value2</value></test>")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testPartial() throws {
|
||||||
|
let fs = FileManager()
|
||||||
|
try? fs.createDirectory(atPath: "templates", withIntermediateDirectories: false)
|
||||||
|
let mustache = Data("<test>{{#value}}<value>{{.}}</value>{{/value}}</test>".utf8)
|
||||||
|
try mustache.write(to: URL(fileURLWithPath: "templates/test-partial.mustache"))
|
||||||
|
let mustache2 = Data("{{>test-partial}}".utf8)
|
||||||
|
try mustache2.write(to: URL(fileURLWithPath: "templates/test.mustache"))
|
||||||
|
defer {
|
||||||
|
XCTAssertNoThrow(try fs.removeItem(atPath: "templates/test-partial.mustache"))
|
||||||
|
XCTAssertNoThrow(try fs.removeItem(atPath: "templates/test.mustache"))
|
||||||
|
XCTAssertNoThrow(try fs.removeItem(atPath: "templates"))
|
||||||
|
}
|
||||||
|
|
||||||
|
let library = try HBMustacheLibrary(directory: "./templates")
|
||||||
|
let object = ["value": ["value1", "value2"]]
|
||||||
|
XCTAssertEqual(library.render(object, withTemplate: "test"), "<test><value>value1</value><value>value2</value></test>")
|
||||||
|
}
|
||||||
|
|
||||||
func testLibraryParserError() throws {
|
func testLibraryParserError() throws {
|
||||||
let fs = FileManager()
|
let fs = FileManager()
|
||||||
try? fs.createDirectory(atPath: "templates", withIntermediateDirectories: false)
|
try? fs.createDirectory(atPath: "templates", withIntermediateDirectories: false)
|
||||||
|
|||||||
Reference in New Issue
Block a user