diff --git a/mustache-sharp.test/FormatCompilerTester.cs b/mustache-sharp.test/FormatCompilerTester.cs index c789e85..ad563e5 100644 --- a/mustache-sharp.test/FormatCompilerTester.cs +++ b/mustache-sharp.test/FormatCompilerTester.cs @@ -1364,5 +1364,28 @@ Odd } #endregion + + #region New Line Management + + /// + /// If the compiler is configured to ignore new lines, + /// they should not be removed from the output. + /// + [TestMethod] + public void TestCompile_PreserveNewLines() + { + FormatCompiler compiler = new FormatCompiler(); + compiler.RemoveNewLines = false; + const string format = @"Hello + "; + + const string expected = @"Hello + "; + Generator generator = compiler.Compile(format); + string result = generator.Render(null); + Assert.AreEqual(expected, result, "The wrong text was generated."); + } + + #endregion } } diff --git a/mustache-sharp/FormatCompiler.cs b/mustache-sharp/FormatCompiler.cs index d667668..f9dfea1 100644 --- a/mustache-sharp/FormatCompiler.cs +++ b/mustache-sharp/FormatCompiler.cs @@ -41,6 +41,8 @@ namespace Mustache _tagLookup.Add(newlineDefinition.Name, newlineDefinition); SetTagDefinition setDefinition = new SetTagDefinition(); _tagLookup.Add(setDefinition.Name, setDefinition); + + RemoveNewLines = true; } /// @@ -53,6 +55,11 @@ namespace Mustache /// public event EventHandler VariableFound; + /// + /// Gets or sets whether newlines are removed from the template (default: true). + /// + public bool RemoveNewLines { get; set; } + /// /// Registers the given tag definition with the parser. /// @@ -87,7 +94,7 @@ namespace Mustache List context = new List() { new Context(_masterDefinition.Name, new ContextParameter[0]) }; int formatIndex = buildCompoundGenerator(_masterDefinition, context, generator, format, 0); string trailing = format.Substring(formatIndex); - generator.AddGenerator(new StaticGenerator(trailing)); + generator.AddGenerator(new StaticGenerator(trailing, RemoveNewLines)); return new Generator(generator); } @@ -198,7 +205,7 @@ namespace Mustache if (match.Groups["key"].Success) { - generator.AddGenerator(new StaticGenerator(leading)); + generator.AddGenerator(new StaticGenerator(leading, RemoveNewLines)); formatIndex = match.Index + match.Length; string key = match.Groups["key"].Value; string alignment = match.Groups["alignment"].Value; @@ -239,7 +246,7 @@ namespace Mustache throw new FormatException(message); } - generator.AddGenerator(new StaticGenerator(leading)); + generator.AddGenerator(new StaticGenerator(leading, RemoveNewLines)); ArgumentCollection arguments = getArguments(nextDefinition, match, context); if (nextDefinition.HasContent) @@ -267,7 +274,7 @@ namespace Mustache } else if (match.Groups["close"].Success) { - generator.AddGenerator(new StaticGenerator(leading)); + generator.AddGenerator(new StaticGenerator(leading, RemoveNewLines)); string tagName = match.Groups["name"].Value; TagDefinition nextDefinition = _tagLookup[tagName]; formatIndex = match.Index; @@ -279,7 +286,7 @@ namespace Mustache } else if (match.Groups["comment"].Success) { - generator.AddGenerator(new StaticGenerator(leading)); + generator.AddGenerator(new StaticGenerator(leading, RemoveNewLines)); formatIndex = match.Index + match.Length; } else if (match.Groups["unknown"].Success) diff --git a/mustache-sharp/StaticGenerator.cs b/mustache-sharp/StaticGenerator.cs index 4068761..af27a90 100644 --- a/mustache-sharp/StaticGenerator.cs +++ b/mustache-sharp/StaticGenerator.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.IO; namespace Mustache @@ -14,9 +13,16 @@ namespace Mustache /// /// Initializes a new instance of a StaticGenerator. /// - public StaticGenerator(string value) + public StaticGenerator(string value, bool removeNewLines) { - this.value = value.Replace(Environment.NewLine, String.Empty); + if (removeNewLines) + { + this.value = value.Replace(Environment.NewLine, String.Empty); + } + else + { + this.value = value; + } } ///