Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using new lines inside tags #202

Merged
merged 1 commit into from
Mar 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Allow default string filters to be applied to arrays
- Similar filters are suggested when unknown filter is used
- Added `indent` filter
- Allow using new lines inside tags

### Bug Fixes

Expand Down
7 changes: 6 additions & 1 deletion Sources/Lexer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ struct Lexer {
guard string.characters.count > 4 else { return "" }
let start = string.index(string.startIndex, 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("{{") {
Expand Down
22 changes: 22 additions & 0 deletions Tests/StencilTests/LexerSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,27 @@ func testLexer() {
let lexer = Lexer(templateString: "{{}}")
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: ".")

}
}
}
4 changes: 2 additions & 2 deletions Tests/StencilTests/VariableSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import Spectre

#if os(OSX)
@objc class Superclass: NSObject {
let name = "Foo"
@objc let name = "Foo"
}
@objc class Object : Superclass {
let title = "Hello World"
@objc let title = "Hello World"
}
#endif

Expand Down