Allow for the preservation of newlines
This commit is contained in:
parent
10304d811c
commit
60f8294eb4
|
@ -1364,5 +1364,28 @@ Odd
|
|||
}
|
||||
|
||||
#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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,6 +41,8 @@ namespace Mustache
|
|||
_tagLookup.Add(newlineDefinition.Name, newlineDefinition);
|
||||
SetTagDefinition setDefinition = new SetTagDefinition();
|
||||
_tagLookup.Add(setDefinition.Name, setDefinition);
|
||||
|
||||
RemoveNewLines = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -53,6 +55,11 @@ namespace Mustache
|
|||
/// </summary>
|
||||
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>
|
||||
/// Registers the given tag definition with the parser.
|
||||
/// </summary>
|
||||
|
@ -87,7 +94,7 @@ namespace Mustache
|
|||
List<Context> context = new List<Context>() { 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)
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace Mustache
|
||||
|
@ -14,9 +13,16 @@ namespace Mustache
|
|||
/// <summary>
|
||||
/// Initializes a new instance of a StaticGenerator.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
Loading…
Reference in New Issue