Allow for the preservation of newlines

This commit is contained in:
Travis Parks 2014-05-21 13:38:39 -04:00
parent 10304d811c
commit 60f8294eb4
3 changed files with 44 additions and 8 deletions

View File

@ -1364,5 +1364,28 @@ Odd
} }
#endregion #endregion
#region New Line Management
/// <summary>
/// If the compiler is configured to ignore new lines,
/// they should not be removed from the output.
/// </summary>
[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
} }
} }

View File

@ -41,6 +41,8 @@ namespace Mustache
_tagLookup.Add(newlineDefinition.Name, newlineDefinition); _tagLookup.Add(newlineDefinition.Name, newlineDefinition);
SetTagDefinition setDefinition = new SetTagDefinition(); SetTagDefinition setDefinition = new SetTagDefinition();
_tagLookup.Add(setDefinition.Name, setDefinition); _tagLookup.Add(setDefinition.Name, setDefinition);
RemoveNewLines = true;
} }
/// <summary> /// <summary>
@ -53,6 +55,11 @@ namespace Mustache
/// </summary> /// </summary>
public event EventHandler<VariableFoundEventArgs> VariableFound; public event EventHandler<VariableFoundEventArgs> VariableFound;
/// <summary>
/// Gets or sets whether newlines are removed from the template (default: true).
/// </summary>
public bool RemoveNewLines { get; set; }
/// <summary> /// <summary>
/// Registers the given tag definition with the parser. /// Registers the given tag definition with the parser.
/// </summary> /// </summary>
@ -87,7 +94,7 @@ namespace Mustache
List<Context> context = new List<Context>() { new Context(_masterDefinition.Name, new ContextParameter[0]) }; List<Context> context = new List<Context>() { new Context(_masterDefinition.Name, new ContextParameter[0]) };
int formatIndex = buildCompoundGenerator(_masterDefinition, context, generator, format, 0); int formatIndex = buildCompoundGenerator(_masterDefinition, context, generator, format, 0);
string trailing = format.Substring(formatIndex); string trailing = format.Substring(formatIndex);
generator.AddGenerator(new StaticGenerator(trailing)); generator.AddGenerator(new StaticGenerator(trailing, RemoveNewLines));
return new Generator(generator); return new Generator(generator);
} }
@ -198,7 +205,7 @@ namespace Mustache
if (match.Groups["key"].Success) if (match.Groups["key"].Success)
{ {
generator.AddGenerator(new StaticGenerator(leading)); generator.AddGenerator(new StaticGenerator(leading, RemoveNewLines));
formatIndex = match.Index + match.Length; formatIndex = match.Index + match.Length;
string key = match.Groups["key"].Value; string key = match.Groups["key"].Value;
string alignment = match.Groups["alignment"].Value; string alignment = match.Groups["alignment"].Value;
@ -239,7 +246,7 @@ namespace Mustache
throw new FormatException(message); throw new FormatException(message);
} }
generator.AddGenerator(new StaticGenerator(leading)); generator.AddGenerator(new StaticGenerator(leading, RemoveNewLines));
ArgumentCollection arguments = getArguments(nextDefinition, match, context); ArgumentCollection arguments = getArguments(nextDefinition, match, context);
if (nextDefinition.HasContent) if (nextDefinition.HasContent)
@ -267,7 +274,7 @@ namespace Mustache
} }
else if (match.Groups["close"].Success) else if (match.Groups["close"].Success)
{ {
generator.AddGenerator(new StaticGenerator(leading)); generator.AddGenerator(new StaticGenerator(leading, RemoveNewLines));
string tagName = match.Groups["name"].Value; string tagName = match.Groups["name"].Value;
TagDefinition nextDefinition = _tagLookup[tagName]; TagDefinition nextDefinition = _tagLookup[tagName];
formatIndex = match.Index; formatIndex = match.Index;
@ -279,7 +286,7 @@ namespace Mustache
} }
else if (match.Groups["comment"].Success) else if (match.Groups["comment"].Success)
{ {
generator.AddGenerator(new StaticGenerator(leading)); generator.AddGenerator(new StaticGenerator(leading, RemoveNewLines));
formatIndex = match.Index + match.Length; formatIndex = match.Index + match.Length;
} }
else if (match.Groups["unknown"].Success) else if (match.Groups["unknown"].Success)

View File

@ -1,5 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
namespace Mustache namespace Mustache
@ -14,10 +13,17 @@ namespace Mustache
/// <summary> /// <summary>
/// Initializes a new instance of a StaticGenerator. /// Initializes a new instance of a StaticGenerator.
/// </summary> /// </summary>
public StaticGenerator(string value) public StaticGenerator(string value, bool removeNewLines)
{
if (removeNewLines)
{ {
this.value = value.Replace(Environment.NewLine, String.Empty); this.value = value.Replace(Environment.NewLine, String.Empty);
} }
else
{
this.value = value;
}
}
/// <summary> /// <summary>
/// Gets or sets the static text. /// Gets or sets the static text.