Fire event whenever a placeholder is found.
It could be useful to track which placeholders were found when parsing the template. This event will allow keys to be mapped to different keys, and support changing alignment and formatting.
This commit is contained in:
parent
a6c7933bab
commit
20dcfc059d
|
@ -1,8 +1,8 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Globalization;
|
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
|
||||||
namespace mustache.test
|
namespace mustache.test
|
||||||
{
|
{
|
||||||
|
@ -327,6 +327,25 @@ Content";
|
||||||
Assert.AreEqual(expected, result, "The wrong text was generated.");
|
Assert.AreEqual(expected, result, "The wrong text was generated.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// We can track all of the keys that appear in a template by
|
||||||
|
/// registering with the PlaceholderFound event.
|
||||||
|
/// </summary>
|
||||||
|
[TestMethod]
|
||||||
|
public void TestCompile_FindsKeys_RecordsKeys()
|
||||||
|
{
|
||||||
|
FormatCompiler compiler = new FormatCompiler();
|
||||||
|
HashSet<string> keys = new HashSet<string>();
|
||||||
|
compiler.PlaceholderFound += (o, e) =>
|
||||||
|
{
|
||||||
|
keys.Add(e.Key);
|
||||||
|
};
|
||||||
|
compiler.Compile(@"{{FirstName}} {{LastName}}");
|
||||||
|
string[] expected = new string[] { "FirstName", "LastName" };
|
||||||
|
string[] actual = keys.OrderBy(s => s).ToArray();
|
||||||
|
CollectionAssert.AreEqual(expected, actual, "Not all placeholders were found.");
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Comment
|
#region Comment
|
||||||
|
|
|
@ -31,5 +31,5 @@ using System.Runtime.InteropServices;
|
||||||
//
|
//
|
||||||
// You can specify all the values or you can default the Build and Revision Numbers
|
// You can specify all the values or you can default the Build and Revision Numbers
|
||||||
// by using the '*' as shown below:
|
// by using the '*' as shown below:
|
||||||
[assembly: AssemblyVersion("0.0.5.0")]
|
[assembly: AssemblyVersion("0.0.6.0")]
|
||||||
[assembly: AssemblyFileVersion("0.0.5.0")]
|
[assembly: AssemblyFileVersion("0.0.6.0")]
|
||||||
|
|
|
@ -37,6 +37,11 @@ namespace mustache
|
||||||
_tagLookup.Add(withDefinition.Name, withDefinition);
|
_tagLookup.Add(withDefinition.Name, withDefinition);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Occurs when a placeholder is found in the template.
|
||||||
|
/// </summary>
|
||||||
|
public event EventHandler<PlaceholderFoundEventArgs> PlaceholderFound;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Registers the given tag definition with the parser.
|
/// Registers the given tag definition with the parser.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -188,7 +193,12 @@ namespace mustache
|
||||||
string key = match.Groups["key"].Value;
|
string key = match.Groups["key"].Value;
|
||||||
string alignment = match.Groups["alignment"].Value;
|
string alignment = match.Groups["alignment"].Value;
|
||||||
string formatting = match.Groups["format"].Value;
|
string formatting = match.Groups["format"].Value;
|
||||||
KeyGenerator keyGenerator = new KeyGenerator(key, alignment, formatting);
|
PlaceholderFoundEventArgs args = new PlaceholderFoundEventArgs(key, alignment, formatting);
|
||||||
|
if (PlaceholderFound != null)
|
||||||
|
{
|
||||||
|
PlaceholderFound(this, args);
|
||||||
|
}
|
||||||
|
KeyGenerator keyGenerator = new KeyGenerator(args.Key, args.Alignment, args.Formatting);
|
||||||
generator.AddGenerator(keyGenerator);
|
generator.AddGenerator(keyGenerator);
|
||||||
}
|
}
|
||||||
else if (match.Groups["open"].Success)
|
else if (match.Groups["open"].Success)
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
using System;
|
||||||
|
using mustache.Properties;
|
||||||
|
|
||||||
|
namespace mustache
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Holds the information descibing a key that is found in a template.
|
||||||
|
/// </summary>
|
||||||
|
public class PlaceholderFoundEventArgs : EventArgs
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of a PlaceholderFoundEventArgs.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key">The key that was found.</param>
|
||||||
|
/// <param name="alignment">The alignment that will be applied to the substitute value.</param>
|
||||||
|
/// <param name="formatting">The formatting that will be applied to the substitute value.</param>
|
||||||
|
internal PlaceholderFoundEventArgs(string key, string alignment, string formatting)
|
||||||
|
{
|
||||||
|
Key = key;
|
||||||
|
Alignment = alignment;
|
||||||
|
Formatting = formatting;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the key that was found.
|
||||||
|
/// </summary>
|
||||||
|
public string Key { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the alignment that will be applied to the substitute value.
|
||||||
|
/// </summary>
|
||||||
|
public string Alignment { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the formatting that will be applied to the substitute value.
|
||||||
|
/// </summary>
|
||||||
|
public string Formatting { get; set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -34,6 +34,6 @@ using System.Runtime.CompilerServices;
|
||||||
// You can specify all the values or you can default the Build and Revision Numbers
|
// You can specify all the values or you can default the Build and Revision Numbers
|
||||||
// by using the '*' as shown below:
|
// by using the '*' as shown below:
|
||||||
// [assembly: AssemblyVersion("1.0.*")]
|
// [assembly: AssemblyVersion("1.0.*")]
|
||||||
[assembly: AssemblyVersion("0.0.5.0")]
|
[assembly: AssemblyVersion("0.0.6.0")]
|
||||||
[assembly: AssemblyFileVersion("0.0.4.0")]
|
[assembly: AssemblyFileVersion("0.0.6.0")]
|
||||||
[assembly: InternalsVisibleTo("mustache-sharp.test")]
|
[assembly: InternalsVisibleTo("mustache-sharp.test")]
|
|
@ -18,6 +18,10 @@ namespace mustache
|
||||||
/// <returns>True if the name is a legal identifier; otherwise, false.</returns>
|
/// <returns>True if the name is a legal identifier; otherwise, false.</returns>
|
||||||
public static bool IsValidIdentifier(string name)
|
public static bool IsValidIdentifier(string name)
|
||||||
{
|
{
|
||||||
|
if (name == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
Regex regex = new Regex("^" + Key + "$");
|
Regex regex = new Regex("^" + Key + "$");
|
||||||
return regex.IsMatch(name);
|
return regex.IsMatch(name);
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,6 +47,7 @@
|
||||||
<Compile Include="IfTagDefinition.cs" />
|
<Compile Include="IfTagDefinition.cs" />
|
||||||
<Compile Include="IGenerator.cs" />
|
<Compile Include="IGenerator.cs" />
|
||||||
<Compile Include="InlineGenerator.cs" />
|
<Compile Include="InlineGenerator.cs" />
|
||||||
|
<Compile Include="PlaceholderFoundEventArgs.cs" />
|
||||||
<Compile Include="KeyGenerator.cs" />
|
<Compile Include="KeyGenerator.cs" />
|
||||||
<Compile Include="MasterTagDefinition.cs" />
|
<Compile Include="MasterTagDefinition.cs" />
|
||||||
<Compile Include="MissingKeyEventArgs.cs" />
|
<Compile Include="MissingKeyEventArgs.cs" />
|
||||||
|
|
Loading…
Reference in New Issue