Skip to content

Commit

Permalink
Use special set of chars for partial filename (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler authored Aug 8, 2022
1 parent 02d019b commit f95e192
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
22 changes: 15 additions & 7 deletions Sources/HummingbirdMustache/Template+Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ extension HBMustacheTemplate {
case ">":
// partial
parser.unsafeAdvance()
let (name, _) = try parseName(&parser, state: state)
let name = try parsePartialName(&parser, state: state)
if whiteSpaceBefore.count > 0 {
tokens.append(.text(String(whiteSpaceBefore)))
}
Expand All @@ -214,9 +214,7 @@ extension HBMustacheTemplate {
case "<":
// partial with inheritance
parser.unsafeAdvance()
let (name, transform) = try parseName(&parser, state: state)
// ERROR: can't have transform applied to inherited sections
guard transform == nil else { throw Error.transformAppliedToInheritanceSection }
let name = try parsePartialName(&parser, state: state)
var indent: String?
if self.isStandalone(&parser, state: state) {
setNewLine = true
Expand All @@ -225,7 +223,7 @@ extension HBMustacheTemplate {
tokens.append(.text(indent!))
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] = [:]
// parse tokens in section to extract inherited sections
for token in sectionTokens {
Expand Down Expand Up @@ -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 {
let text = try parser.read(untilString: state.endDelimiter, throwOnOverflow: true, skipToEnd: true)
return String(text)
Expand Down Expand Up @@ -390,6 +397,7 @@ extension HBMustacheTemplate {
return state.newLine && self.hasLineFinished(&parser)
}

private static let sectionNameCharsWithoutBrackets = Set<Character>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._?")
private static let sectionNameChars = Set<Character>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._?()")
private static let sectionNameCharsWithoutBrackets = Set<Character>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_?")
private static let sectionNameChars = Set<Character>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_?()")
private static let partialNameChars = Set<Character>("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_()")
}
18 changes: 18 additions & 0 deletions Tests/HummingbirdMustacheTests/LibraryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ final class LibraryTests: XCTestCase {
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 {
let fs = FileManager()
try? fs.createDirectory(atPath: "templates", withIntermediateDirectories: false)
Expand Down

0 comments on commit f95e192

Please sign in to comment.