From fa0df6df1cc5d3d5ade7109def3000a8e74f3471 Mon Sep 17 00:00:00 2001 From: Olivier Halligon Date: Sun, 18 Oct 2015 06:42:00 +0200 Subject: [PATCH] [ExtraEmptyLines] Fix #22, making the tests pass --- Stencil/Node.swift | 9 +++++++++ Stencil/Parser.swift | 7 +++++++ StencilTests/StencilTests.swift | 3 +-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Stencil/Node.swift b/Stencil/Node.swift index 68cd694a..8d00bbb6 100644 --- a/Stencil/Node.swift +++ b/Stencil/Node.swift @@ -124,13 +124,16 @@ public class ForNode : NodeType { var emptyNodes = [NodeType]() + parser.eatNextCRLF() // If CRLF after {% for %}, eat it let forNodes = try parser.parse(until(["endfor", "empty"])) if let token = parser.nextToken() { if token.contents == "empty" { + parser.eatNextCRLF() // if CRLF after {% empty %}, eat it emptyNodes = try parser.parse(until(["endfor"])) parser.nextToken() } + parser.eatNextCRLF() // if CRLF after {% endfor %}, eat it } else { throw TemplateSyntaxError("`endfor` was not found.") } @@ -172,13 +175,16 @@ public class IfNode : NodeType { var trueNodes = [NodeType]() var falseNodes = [NodeType]() + parser.eatNextCRLF() // If CRLF after {% if %}, eat it trueNodes = try parser.parse(until(["endif", "else"])) if let token = parser.nextToken() { if token.contents == "else" { + parser.eatNextCRLF() // If CRLF after {% else %}, eat it falseNodes = try parser.parse(until(["endif"])) parser.nextToken() } + parser.eatNextCRLF() // If CRLF after {% endif %}, eat it } else { throw TemplateSyntaxError("`endif` was not found.") } @@ -191,13 +197,16 @@ public class IfNode : NodeType { var trueNodes = [NodeType]() var falseNodes = [NodeType]() + parser.eatNextCRLF() // If CRLF after {% ifnot %}, eat it falseNodes = try parser.parse(until(["endif", "else"])) if let token = parser.nextToken() { if token.contents == "else" { + parser.eatNextCRLF() // If CRLF after {% else %}, eat it trueNodes = try parser.parse(until(["endif"])) parser.nextToken() } + parser.eatNextCRLF() // If CRLF after {% endif %}, eat it } else { throw TemplateSyntaxError("`endif` was not found.") } diff --git a/Stencil/Parser.swift b/Stencil/Parser.swift index b5e1e11f..7d682e23 100644 --- a/Stencil/Parser.swift +++ b/Stencil/Parser.swift @@ -77,6 +77,13 @@ public class TokenParser { return nodes } + public func eatNextCRLF() { + if let token = tokens.first, case Token.Text(let text) = token where text.characters.first == "\n" { + let strippedText: String = String(text.characters.dropFirst()) + tokens[0] = Token.Text(value: strippedText) + } + } + public func nextToken() -> Token? { if tokens.count > 0 { return tokens.removeAtIndex(0) diff --git a/StencilTests/StencilTests.swift b/StencilTests/StencilTests.swift index 3d778268..b7264fe0 100644 --- a/StencilTests/StencilTests.swift +++ b/StencilTests/StencilTests.swift @@ -47,8 +47,7 @@ class StencilTests: XCTestCase { let fixture = "There are 2 articles.\n" + "\n" + " - Migrating from OCUnit to XCTest by Kyle Fuller.\n" + - " - Memory Management with ARC by Kyle Fuller.\n" + - "\n" + " - Memory Management with ARC by Kyle Fuller.\n" XCTAssertEqual(result, fixture) }