Support Missing Key Handlers

A request was made to allow missing keys to be handled in a custom
manner. I created an event that can be registered with the Generator
instances.

I also fixed a deployment issue causing debug versions of the library to
be packaged and released to NuGet. This should boost performance
considerably.
This commit is contained in:
Travis Parks 2013-04-18 19:26:58 -04:00
parent bfc579be22
commit ac09c8fc38
8 changed files with 1385 additions and 1277 deletions

View File

@ -1,3 +1,4 @@
nuget pack ../mustache-sharp/mustache-sharp.csproj -Prop Configuration=Release -Build
nuget push *.nupkg
msbuild ../mustache-sharp.sln /p:Configuration=Release
nuget pack ../mustache-sharp/mustache-sharp.csproj -Properties Configuration=Release
nuget push *.nupkg
del *.nupkg

File diff suppressed because it is too large Load Diff

View File

@ -1,35 +1,35 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("mustache-sharp.test")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("mustache-sharp.test")]
[assembly: AssemblyCopyright("Copyright © 2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("9975f293-f972-4751-9c8c-e25b17c0c8bc")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("0.0.3.0")]
[assembly: AssemblyFileVersion("0.0.3.0")]
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("mustache-sharp.test")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("mustache-sharp.test")]
[assembly: AssemblyCopyright("Copyright © 2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("9975f293-f972-4751-9c8c-e25b17c0c8bc")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("0.0.4.0")]
[assembly: AssemblyFileVersion("0.0.4.0")]

View File

@ -1,56 +1,72 @@
using System;
using System.Globalization;
using System.IO;
namespace mustache
{
/// <summary>
/// Generates text by substituting an object's values for placeholders.
/// </summary>
public sealed class Generator
{
private readonly IGenerator _generator;
/// <summary>
/// Initializes a new instance of a Generator.
/// </summary>
/// <param name="generator">The text generator to wrap.</param>
internal Generator(IGenerator generator)
{
_generator = generator;
}
/// <summary>
/// Gets the text that is generated for the given object.
/// </summary>
/// <param name="source">The object to generate the text with.</param>
/// <returns>The text generated for the given object.</returns>
public string Render(object source)
{
return render(CultureInfo.CurrentCulture, source);
}
/// <summary>
/// Gets the text that is generated for the given object.
/// </summary>
/// <param name="provider">The format provider to use.</param>
/// <param name="source">The object to generate the text with.</param>
/// <returns>The text generated for the given object.</returns>
public string Render(IFormatProvider provider, object source)
{
if (provider == null)
{
provider = CultureInfo.CurrentCulture;
}
return render(provider, source);
}
private string render(IFormatProvider provider, object source)
{
KeyScope scope = new KeyScope(source);
StringWriter writer = new StringWriter(provider);
_generator.GetText(scope, writer);
return writer.ToString();
}
}
}
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
namespace mustache
{
/// <summary>
/// Generates text by substituting an object's values for placeholders.
/// </summary>
public sealed class Generator
{
private readonly IGenerator _generator;
private readonly List<EventHandler<MissingKeyEventArgs>> _handlers;
/// <summary>
/// Initializes a new instance of a Generator.
/// </summary>
/// <param name="generator">The text generator to wrap.</param>
internal Generator(IGenerator generator)
{
_generator = generator;
_handlers = new List<EventHandler<MissingKeyEventArgs>>();
}
/// <summary>
/// Occurs when a key/property is not found in the object graph.
/// </summary>
public event EventHandler<MissingKeyEventArgs> KeyNotFound
{
add { _handlers.Add(value); }
remove { _handlers.Remove(value); }
}
/// <summary>
/// Gets the text that is generated for the given object.
/// </summary>
/// <param name="source">The object to generate the text with.</param>
/// <returns>The text generated for the given object.</returns>
public string Render(object source)
{
return render(CultureInfo.CurrentCulture, source);
}
/// <summary>
/// Gets the text that is generated for the given object.
/// </summary>
/// <param name="provider">The format provider to use.</param>
/// <param name="source">The object to generate the text with.</param>
/// <returns>The text generated for the given object.</returns>
public string Render(IFormatProvider provider, object source)
{
if (provider == null)
{
provider = CultureInfo.CurrentCulture;
}
return render(provider, source);
}
private string render(IFormatProvider provider, object source)
{
KeyScope scope = new KeyScope(source);
foreach (EventHandler<MissingKeyEventArgs> handler in _handlers)
{
scope.KeyNotFound += handler;
}
StringWriter writer = new StringWriter(provider);
_generator.GetText(scope, writer);
return writer.ToString();
}
}
}

View File

@ -1,96 +1,111 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using mustache.Properties;
namespace mustache
{
/// <summary>
/// Represents a scope of keys.
/// </summary>
public sealed class KeyScope
{
private readonly object _source;
private readonly KeyScope _parent;
/// <summary>
/// Initializes a new instance of a KeyScope.
/// </summary>
/// <param name="source">The object to search for keys in.</param>
internal KeyScope(object source)
: this(source, null)
{
}
/// <summary>
/// Initializes a new instance of a KeyScope.
/// </summary>
/// <param name="source">The object to search for keys in.</param>
/// <param name="parent">The parent scope to search in if the value is not found.</param>
internal KeyScope(object source, KeyScope parent)
{
_parent = parent;
_source = source;
}
/// <summary>
/// Creates a child scope that searches for keys in the given object.
/// </summary>
/// <param name="source">The object to search for keys in.</param>
/// <returns>The new child scope.</returns>
public KeyScope CreateChildScope(object source)
{
KeyScope scope = new KeyScope(source, this);
return scope;
}
/// <summary>
/// Attempts to find the value associated with the key with given name.
/// </summary>
/// <param name="name">The name of the key.</param>
/// <returns>The value associated with the key with the given name.</returns>
/// <exception cref="System.Collections.Generic.KeyNotFoundException">A key with the given name could not be found.</exception>
internal object Find(string name)
{
string[] names = name.Split('.');
string member = names[0];
object nextLevel = _source;
if (member != "this")
{
nextLevel = find(member);
}
for (int index = 1; index < names.Length; ++index)
{
IDictionary<string, object> context = toLookup(nextLevel);
member = names[index];
nextLevel = context[member];
}
return nextLevel;
}
private object find(string name)
{
IDictionary<string, object> lookup = toLookup(_source);
if (lookup.ContainsKey(name))
{
return lookup[name];
}
if (_parent == null)
{
string message = String.Format(CultureInfo.CurrentCulture, Resources.KeyNotFound, name);
throw new KeyNotFoundException(message);
}
return _parent.find(name);
}
private static IDictionary<string, object> toLookup(object value)
{
IDictionary<string, object> lookup = value as IDictionary<string, object>;
if (lookup == null)
{
lookup = new PropertyDictionary(value);
}
return lookup;
}
}
}
using System;
using System.Collections.Generic;
using System.Globalization;
using mustache.Properties;
namespace mustache
{
/// <summary>
/// Represents a scope of keys.
/// </summary>
public sealed class KeyScope
{
private readonly object _source;
private readonly KeyScope _parent;
/// <summary>
/// Initializes a new instance of a KeyScope.
/// </summary>
/// <param name="source">The object to search for keys in.</param>
internal KeyScope(object source)
: this(source, null)
{
}
/// <summary>
/// Initializes a new instance of a KeyScope.
/// </summary>
/// <param name="source">The object to search for keys in.</param>
/// <param name="parent">The parent scope to search in if the value is not found.</param>
internal KeyScope(object source, KeyScope parent)
{
_parent = parent;
_source = source;
}
/// <summary>
/// Occurs when a key/property is not found in the object graph.
/// </summary>
public event EventHandler<MissingKeyEventArgs> KeyNotFound;
/// <summary>
/// Creates a child scope that searches for keys in the given object.
/// </summary>
/// <param name="source">The object to search for keys in.</param>
/// <returns>The new child scope.</returns>
public KeyScope CreateChildScope(object source)
{
KeyScope scope = new KeyScope(source, this);
scope.KeyNotFound = KeyNotFound;
return scope;
}
/// <summary>
/// Attempts to find the value associated with the key with given name.
/// </summary>
/// <param name="name">The name of the key.</param>
/// <returns>The value associated with the key with the given name.</returns>
/// <exception cref="System.Collections.Generic.KeyNotFoundException">A key with the given name could not be found.</exception>
internal object Find(string name)
{
string[] names = name.Split('.');
string member = names[0];
object nextLevel = _source;
if (member != "this")
{
nextLevel = find(member);
}
for (int index = 1; index < names.Length; ++index)
{
IDictionary<string, object> context = toLookup(nextLevel);
member = names[index];
nextLevel = context[member];
}
return nextLevel;
}
private object find(string name)
{
IDictionary<string, object> lookup = toLookup(_source);
if (lookup.ContainsKey(name))
{
return lookup[name];
}
if (_parent == null)
{
MissingKeyEventArgs args = new MissingKeyEventArgs(name);
if (KeyNotFound != null)
{
KeyNotFound(this, args);
}
if (args.Handled)
{
return args.Substitute;
}
string message = String.Format(CultureInfo.CurrentCulture, Resources.KeyNotFound, name);
throw new KeyNotFoundException(message);
}
return _parent.find(name);
}
private static IDictionary<string, object> toLookup(object value)
{
IDictionary<string, object> lookup = value as IDictionary<string, object>;
if (lookup == null)
{
lookup = new PropertyDictionary(value);
}
return lookup;
}
}
}

View File

@ -0,0 +1,34 @@
using System;
namespace mustache
{
/// <summary>
/// Holds the information needed to handle a missing key.
/// </summary>
public class MissingKeyEventArgs : EventArgs
{
/// <summary>
/// Initializes a new instance of a MissingKeyEventArgs.
/// </summary>
/// <param name="missingKey">The key that had no match.</param>
internal MissingKeyEventArgs(string missingKey)
{
MissingKey = missingKey;
}
/// <summary>
/// Gets the key that could not be found.
/// </summary>
public string MissingKey { get; private set; }
/// <summary>
/// Gets or sets whether to use the substitute.
/// </summary>
public bool Handled { get; set; }
/// <summary>
/// Gets or sets the object to use as the substitute.
/// </summary>
public object Substitute { get; set; }
}
}

View File

@ -1,39 +1,39 @@
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("mustache-sharp")]
[assembly: AssemblyDescription("A extension of the mustache text template engine for .NET.")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("truncon")]
[assembly: AssemblyProduct("mustache-sharp")]
[assembly: AssemblyCopyright("Copyright © 2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: CLSCompliant(true)]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("e5a4263d-d450-4d85-a4d5-44c0a2822668")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.0.3.0")]
[assembly: AssemblyFileVersion("0.0.3.0")]
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("mustache-sharp")]
[assembly: AssemblyDescription("A extension of the mustache text template engine for .NET.")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("truncon")]
[assembly: AssemblyProduct("mustache-sharp")]
[assembly: AssemblyCopyright("Copyright © 2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: CLSCompliant(true)]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("e5a4263d-d450-4d85-a4d5-44c0a2822668")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.0.4.0")]
[assembly: AssemblyFileVersion("0.0.4.0")]
[assembly: InternalsVisibleTo("mustache-sharp.test")]

View File

@ -1,82 +1,83 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{D71B378F-A4BA-4263-A4F0-07A49A0C528D}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>mustache</RootNamespace>
<AssemblyName>mustache-sharp</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="ArgumentCollection.cs" />
<Compile Include="CompoundGenerator.cs" />
<Compile Include="ConditionTagDefinition.cs" />
<Compile Include="ContentTagDefinition.cs" />
<Compile Include="InlineTagDefinition.cs" />
<Compile Include="EachTagDefinition.cs" />
<Compile Include="ElifTagDefinition.cs" />
<Compile Include="ElseTagDefinition.cs" />
<Compile Include="FormatCompiler.cs" />
<Compile Include="Generator.cs" />
<Compile Include="IfTagDefinition.cs" />
<Compile Include="IGenerator.cs" />
<Compile Include="InlineGenerator.cs" />
<Compile Include="KeyGenerator.cs" />
<Compile Include="MasterTagDefinition.cs" />
<Compile Include="NestedContext.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="PropertyDictionary.cs" />
<Compile Include="RegexHelper.cs" />
<Compile Include="StaticGenerator.cs" />
<Compile Include="TagDefinition.cs" />
<Compile Include="TagParameter.cs" />
<Compile Include="KeyScope.cs" />
<Compile Include="Trimmer.cs" />
<Compile Include="WithGenerator.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{D71B378F-A4BA-4263-A4F0-07A49A0C528D}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>mustache</RootNamespace>
<AssemblyName>mustache-sharp</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="ArgumentCollection.cs" />
<Compile Include="CompoundGenerator.cs" />
<Compile Include="ConditionTagDefinition.cs" />
<Compile Include="ContentTagDefinition.cs" />
<Compile Include="InlineTagDefinition.cs" />
<Compile Include="EachTagDefinition.cs" />
<Compile Include="ElifTagDefinition.cs" />
<Compile Include="ElseTagDefinition.cs" />
<Compile Include="FormatCompiler.cs" />
<Compile Include="Generator.cs" />
<Compile Include="IfTagDefinition.cs" />
<Compile Include="IGenerator.cs" />
<Compile Include="InlineGenerator.cs" />
<Compile Include="KeyGenerator.cs" />
<Compile Include="MasterTagDefinition.cs" />
<Compile Include="MissingKeyEventArgs.cs" />
<Compile Include="NestedContext.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="PropertyDictionary.cs" />
<Compile Include="RegexHelper.cs" />
<Compile Include="StaticGenerator.cs" />
<Compile Include="TagDefinition.cs" />
<Compile Include="TagParameter.cs" />
<Compile Include="KeyScope.cs" />
<Compile Include="Trimmer.cs" />
<Compile Include="WithGenerator.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>