feat: allow using new lines inside tags (#202)

This commit is contained in:
Ilya Puchka
2018-03-13 09:07:56 +00:00
committed by Kyle Fuller
parent fa68ba9df8
commit 0bc6bd974e
4 changed files with 31 additions and 3 deletions

View File

@@ -11,6 +11,7 @@
- Allow default string filters to be applied to arrays - Allow default string filters to be applied to arrays
- Similar filters are suggested when unknown filter is used - Similar filters are suggested when unknown filter is used
- Added `indent` filter - Added `indent` filter
- Allow using new lines inside tags
### Bug Fixes ### Bug Fixes

View File

@@ -10,7 +10,12 @@ struct Lexer {
guard string.characters.count > 4 else { return "" } guard string.characters.count > 4 else { return "" }
let start = string.index(string.startIndex, offsetBy: 2) let start = string.index(string.startIndex, offsetBy: 2)
let end = string.index(string.endIndex, offsetBy: -2) let end = string.index(string.endIndex, offsetBy: -2)
return String(string[start..<end]).trim(character: " ") let trimmed = String(string[start..<end])
.components(separatedBy: "\n")
.filter({ !$0.isEmpty })
.map({ $0.trim(character: " ") })
.joined(separator: " ")
return trimmed
} }
if string.hasPrefix("{{") { if string.hasPrefix("{{") {

View File

@@ -64,5 +64,27 @@ func testLexer() {
let lexer = Lexer(templateString: "{{}}") let lexer = Lexer(templateString: "{{}}")
let _ = lexer.tokenize() let _ = lexer.tokenize()
} }
$0.it("can tokenize with new lines") {
let lexer = Lexer(templateString:
"My name is {%\n" +
" if name\n" +
" and\n" +
" name\n" +
"%}{{\n" +
"name\n" +
"}}{%\n" +
"endif %}.")
let tokens = lexer.tokenize()
try expect(tokens.count) == 5
try expect(tokens[0]) == Token.text(value: "My name is ")
try expect(tokens[1]) == Token.block(value: "if name and name")
try expect(tokens[2]) == Token.variable(value: "name")
try expect(tokens[3]) == Token.block(value: "endif")
try expect(tokens[4]) == Token.text(value: ".")
}
} }
} }

View File

@@ -5,10 +5,10 @@ import Spectre
#if os(OSX) #if os(OSX)
@objc class Superclass: NSObject { @objc class Superclass: NSObject {
let name = "Foo" @objc let name = "Foo"
} }
@objc class Object : Superclass { @objc class Object : Superclass {
let title = "Hello World" @objc let title = "Hello World"
} }
#endif #endif