Provide context when placeholders found.
It could be useful to know the context when discovering a placeholder. I also renamed MissingKeyEventArgs to KeyNotFoundEventArgs. I created a new event KeyFoundEventArgs.
This commit is contained in:
parent
20dcfc059d
commit
42463a888f
|
@ -332,7 +332,7 @@ Content";
|
|||
/// registering with the PlaceholderFound event.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestCompile_FindsKeys_RecordsKeys()
|
||||
public void TestCompile_FindsPlaceholders_RecordsPlaceholders()
|
||||
{
|
||||
FormatCompiler compiler = new FormatCompiler();
|
||||
HashSet<string> keys = new HashSet<string>();
|
||||
|
@ -346,6 +346,28 @@ Content";
|
|||
CollectionAssert.AreEqual(expected, actual, "Not all placeholders were found.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// We can determine the context in which a placeholder is found by looking at the provided context array.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void TestCompile_FindsPlaceholders_ProvidesContext()
|
||||
{
|
||||
FormatCompiler compiler = new FormatCompiler();
|
||||
Context[] context = null;
|
||||
compiler.PlaceholderFound += (o, e) =>
|
||||
{
|
||||
context = e.Context;
|
||||
};
|
||||
compiler.Compile(@"{{#with Address}}{{ZipCode}}{{/with}}");
|
||||
|
||||
Assert.IsNotNull(context, "The context was not set.");
|
||||
Assert.AreEqual(2, context.Length, "The context did not contain the right number of items.");
|
||||
Assert.AreEqual(String.Empty, context[0].Tag.Name, "The top-most context had the wrong tag type.");
|
||||
Assert.AreEqual("this", context[0].Argument, "The top-level argument should always be 'this'.");
|
||||
Assert.AreEqual("with", context[1].Tag.Name, "The inner context should have been a 'with' tag.");
|
||||
Assert.AreEqual("Address", context[1].Argument, "The inner context argument was wrong.");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#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
|
||||
// by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("0.0.6.0")]
|
||||
[assembly: AssemblyFileVersion("0.0.6.0")]
|
||||
[assembly: AssemblyVersion("0.0.7.0")]
|
||||
[assembly: AssemblyFileVersion("0.0.7.0")]
|
||||
|
|
|
@ -1,56 +1,74 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace mustache
|
||||
{
|
||||
/// <summary>
|
||||
/// Associates parameters to their argument values.
|
||||
/// </summary>
|
||||
internal sealed class ArgumentCollection
|
||||
{
|
||||
private readonly Dictionary<TagParameter, string> _argumentLookup;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of an ArgumentCollection.
|
||||
/// </summary>
|
||||
public ArgumentCollection()
|
||||
{
|
||||
_argumentLookup = new Dictionary<TagParameter, string>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Associates the given parameter to the key placeholder.
|
||||
/// </summary>
|
||||
/// <param name="parameter">The parameter to associate the key with.</param>
|
||||
/// <param name="key">The key placeholder used as the argument.</param>
|
||||
/// <remarks>If the key is null, the default value of the parameter will be used.</remarks>
|
||||
public void AddArgument(TagParameter parameter, string key)
|
||||
{
|
||||
_argumentLookup.Add(parameter, key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Substitutes the key placeholders with their respective values.
|
||||
/// </summary>
|
||||
/// <param name="scope">The current lexical scope.</param>
|
||||
/// <returns>A dictionary associating the parameter name to the associated value.</returns>
|
||||
public Dictionary<string, object> GetArguments(KeyScope scope)
|
||||
{
|
||||
Dictionary<string, object> arguments = new Dictionary<string,object>();
|
||||
foreach (KeyValuePair<TagParameter, string> pair in _argumentLookup)
|
||||
{
|
||||
object value;
|
||||
if (pair.Value == null)
|
||||
{
|
||||
value = pair.Key.DefaultValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
value = scope.Find(pair.Value);
|
||||
}
|
||||
arguments.Add(pair.Key.Name, value);
|
||||
}
|
||||
return arguments;
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace mustache
|
||||
{
|
||||
/// <summary>
|
||||
/// Associates parameters to their argument values.
|
||||
/// </summary>
|
||||
internal sealed class ArgumentCollection
|
||||
{
|
||||
private readonly Dictionary<TagParameter, string> _argumentLookup;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of an ArgumentCollection.
|
||||
/// </summary>
|
||||
public ArgumentCollection()
|
||||
{
|
||||
_argumentLookup = new Dictionary<TagParameter, string>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Associates the given parameter to the key placeholder.
|
||||
/// </summary>
|
||||
/// <param name="parameter">The parameter to associate the key with.</param>
|
||||
/// <param name="key">The key placeholder used as the argument.</param>
|
||||
/// <remarks>If the key is null, the default value of the parameter will be used.</remarks>
|
||||
public void AddArgument(TagParameter parameter, string key)
|
||||
{
|
||||
_argumentLookup.Add(parameter, key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the key that will be used to find the substitute value.
|
||||
/// </summary>
|
||||
/// <param name="parameterName">The name of the parameter.</param>
|
||||
public string GetKey(TagParameter parameter)
|
||||
{
|
||||
string key;
|
||||
if (_argumentLookup.TryGetValue(parameter, out key))
|
||||
{
|
||||
return key;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Substitutes the key placeholders with their respective values.
|
||||
/// </summary>
|
||||
/// <param name="scope">The current lexical scope.</param>
|
||||
/// <returns>A dictionary associating the parameter name to the associated value.</returns>
|
||||
public Dictionary<string, object> GetArguments(KeyScope scope)
|
||||
{
|
||||
Dictionary<string, object> arguments = new Dictionary<string,object>();
|
||||
foreach (KeyValuePair<TagParameter, string> pair in _argumentLookup)
|
||||
{
|
||||
object value;
|
||||
if (pair.Value == null)
|
||||
{
|
||||
value = pair.Key.DefaultValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
value = scope.Find(pair.Value);
|
||||
}
|
||||
arguments.Add(pair.Key.Name, value);
|
||||
}
|
||||
return arguments;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,105 +1,119 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace mustache
|
||||
{
|
||||
/// <summary>
|
||||
/// Builds text by combining the output of other generators.
|
||||
/// </summary>
|
||||
internal sealed class CompoundGenerator : IGenerator
|
||||
{
|
||||
private readonly TagDefinition _definition;
|
||||
private readonly ArgumentCollection _arguments;
|
||||
private readonly LinkedList<IGenerator> _primaryGenerators;
|
||||
private IGenerator _subGenerator;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of a CompoundGenerator.
|
||||
/// </summary>
|
||||
/// <param name="definition">The tag that the text is being generated for.</param>
|
||||
/// <param name="arguments">The arguments that were passed to the tag.</param>
|
||||
public CompoundGenerator(TagDefinition definition, ArgumentCollection arguments)
|
||||
{
|
||||
_definition = definition;
|
||||
_arguments = arguments;
|
||||
_primaryGenerators = new LinkedList<IGenerator>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the given generator.
|
||||
/// </summary>
|
||||
/// <param name="generator">The generator to add.</param>
|
||||
public void AddGenerator(IGenerator generator)
|
||||
{
|
||||
addGenerator(generator, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the given generator, determining whether the generator should
|
||||
/// be part of the primary generators or added as an secondary generator.
|
||||
/// </summary>
|
||||
/// <param name="definition">The tag that the generator is generating text for.</param>
|
||||
/// <param name="generator">The generator to add.</param>
|
||||
public void AddGenerator(TagDefinition definition, IGenerator generator)
|
||||
{
|
||||
bool isSubGenerator = _definition.ShouldCreateSecondaryGroup(definition);
|
||||
addGenerator(generator, isSubGenerator);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a StaticGenerator from the given value and adds it.
|
||||
/// </summary>
|
||||
/// <param name="generators">The static generators to add.</param>
|
||||
public void AddStaticGenerators(IEnumerable<StaticGenerator> generators)
|
||||
{
|
||||
foreach (StaticGenerator generator in generators)
|
||||
{
|
||||
LinkedListNode<IGenerator> node = _primaryGenerators.AddLast(generator);
|
||||
generator.Node = node;
|
||||
}
|
||||
}
|
||||
|
||||
private void addGenerator(IGenerator generator, bool isSubGenerator)
|
||||
{
|
||||
if (isSubGenerator)
|
||||
{
|
||||
_subGenerator = generator;
|
||||
}
|
||||
else
|
||||
{
|
||||
_primaryGenerators.AddLast(generator);
|
||||
}
|
||||
}
|
||||
|
||||
void IGenerator.GetText(KeyScope scope, TextWriter writer)
|
||||
{
|
||||
Dictionary<string, object> arguments = _arguments.GetArguments(scope);
|
||||
IEnumerable<NestedContext> contexts = _definition.GetChildContext(writer, scope, arguments);
|
||||
LinkedList<IGenerator> generators;
|
||||
if (_definition.ShouldGeneratePrimaryGroup(arguments))
|
||||
{
|
||||
generators = _primaryGenerators;
|
||||
}
|
||||
else
|
||||
{
|
||||
generators = new LinkedList<IGenerator>();
|
||||
if (_subGenerator != null)
|
||||
{
|
||||
generators.AddLast(_subGenerator);
|
||||
}
|
||||
}
|
||||
foreach (NestedContext context in contexts)
|
||||
{
|
||||
foreach (IGenerator generator in generators)
|
||||
{
|
||||
generator.GetText(context.KeyScope ?? scope, context.Writer ?? writer);
|
||||
if (context.WriterNeedsConsidated)
|
||||
{
|
||||
writer.Write(_definition.ConsolidateWriter(context.Writer ?? writer, arguments));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace mustache
|
||||
{
|
||||
/// <summary>
|
||||
/// Builds text by combining the output of other generators.
|
||||
/// </summary>
|
||||
internal sealed class CompoundGenerator : IGenerator
|
||||
{
|
||||
private readonly TagDefinition _definition;
|
||||
private readonly ArgumentCollection _arguments;
|
||||
private readonly LinkedList<IGenerator> _primaryGenerators;
|
||||
private IGenerator _subGenerator;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of a CompoundGenerator.
|
||||
/// </summary>
|
||||
/// <param name="definition">The tag that the text is being generated for.</param>
|
||||
/// <param name="arguments">The arguments that were passed to the tag.</param>
|
||||
public CompoundGenerator(TagDefinition definition, ArgumentCollection arguments)
|
||||
{
|
||||
_definition = definition;
|
||||
_arguments = arguments;
|
||||
_primaryGenerators = new LinkedList<IGenerator>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the argument that will act as the context for the content.
|
||||
/// </summary>
|
||||
/// <returns>The argument that will act as the context for the content.</returns>
|
||||
public string GetContextArgument()
|
||||
{
|
||||
TagParameter parameter = _definition.GetChildContextParameter();
|
||||
if (parameter == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _arguments.GetKey(parameter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the given generator.
|
||||
/// </summary>
|
||||
/// <param name="generator">The generator to add.</param>
|
||||
public void AddGenerator(IGenerator generator)
|
||||
{
|
||||
addGenerator(generator, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the given generator, determining whether the generator should
|
||||
/// be part of the primary generators or added as an secondary generator.
|
||||
/// </summary>
|
||||
/// <param name="definition">The tag that the generator is generating text for.</param>
|
||||
/// <param name="generator">The generator to add.</param>
|
||||
public void AddGenerator(TagDefinition definition, IGenerator generator)
|
||||
{
|
||||
bool isSubGenerator = _definition.ShouldCreateSecondaryGroup(definition);
|
||||
addGenerator(generator, isSubGenerator);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a StaticGenerator from the given value and adds it.
|
||||
/// </summary>
|
||||
/// <param name="generators">The static generators to add.</param>
|
||||
public void AddStaticGenerators(IEnumerable<StaticGenerator> generators)
|
||||
{
|
||||
foreach (StaticGenerator generator in generators)
|
||||
{
|
||||
LinkedListNode<IGenerator> node = _primaryGenerators.AddLast(generator);
|
||||
generator.Node = node;
|
||||
}
|
||||
}
|
||||
|
||||
private void addGenerator(IGenerator generator, bool isSubGenerator)
|
||||
{
|
||||
if (isSubGenerator)
|
||||
{
|
||||
_subGenerator = generator;
|
||||
}
|
||||
else
|
||||
{
|
||||
_primaryGenerators.AddLast(generator);
|
||||
}
|
||||
}
|
||||
|
||||
void IGenerator.GetText(KeyScope scope, TextWriter writer)
|
||||
{
|
||||
Dictionary<string, object> arguments = _arguments.GetArguments(scope);
|
||||
IEnumerable<NestedContext> contexts = _definition.GetChildContext(writer, scope, arguments);
|
||||
LinkedList<IGenerator> generators;
|
||||
if (_definition.ShouldGeneratePrimaryGroup(arguments))
|
||||
{
|
||||
generators = _primaryGenerators;
|
||||
}
|
||||
else
|
||||
{
|
||||
generators = new LinkedList<IGenerator>();
|
||||
if (_subGenerator != null)
|
||||
{
|
||||
generators.AddLast(_subGenerator);
|
||||
}
|
||||
}
|
||||
foreach (NestedContext context in contexts)
|
||||
{
|
||||
foreach (IGenerator generator in generators)
|
||||
{
|
||||
generator.GetText(context.KeyScope ?? scope, context.Writer ?? writer);
|
||||
if (context.WriterNeedsConsidated)
|
||||
{
|
||||
writer.Write(_definition.ConsolidateWriter(context.Writer ?? writer, arguments));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,92 +1,101 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace mustache
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines a tag that conditionally prints its content.
|
||||
/// </summary>
|
||||
internal abstract class ConditionTagDefinition : ContentTagDefinition
|
||||
{
|
||||
private const string conditionParameter = "condition";
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of a ConditionTagDefinition.
|
||||
/// </summary>
|
||||
/// <param name="tagName">The name of the tag.</param>
|
||||
protected ConditionTagDefinition(string tagName)
|
||||
: base(tagName, true)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the parameters that can be passed to the tag.
|
||||
/// </summary>
|
||||
/// <returns>The parameters.</returns>
|
||||
protected override IEnumerable<TagParameter> GetParameters()
|
||||
{
|
||||
return new TagParameter[] { new TagParameter(conditionParameter) { IsRequired = true } };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the tags that come into scope within the context of the current tag.
|
||||
/// </summary>
|
||||
/// <returns>The child tag definitions.</returns>
|
||||
protected override IEnumerable<string> GetChildTags()
|
||||
{
|
||||
return new string[] { "elif", "else" };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the given tag's generator should be used for a secondary (or substitute) text block.
|
||||
/// </summary>
|
||||
/// <param name="definition">The tag to inspect.</param>
|
||||
/// <returns>True if the tag's generator should be used as a secondary generator.</returns>
|
||||
public override bool ShouldCreateSecondaryGroup(TagDefinition definition)
|
||||
{
|
||||
return new string[] { "elif", "else" }.Contains(definition.Name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the primary generator group should be used to render the tag.
|
||||
/// </summary>
|
||||
/// <param name="arguments">The arguments passed to the tag.</param>
|
||||
/// <returns>
|
||||
/// True if the primary generator group should be used to render the tag;
|
||||
/// otherwise, false to use the secondary group.
|
||||
/// </returns>
|
||||
public override bool ShouldGeneratePrimaryGroup(Dictionary<string, object> arguments)
|
||||
{
|
||||
object condition = arguments[conditionParameter];
|
||||
return isConditionSatisfied(condition);
|
||||
}
|
||||
|
||||
private bool isConditionSatisfied(object condition)
|
||||
{
|
||||
if (condition == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
IEnumerable enumerable = condition as IEnumerable;
|
||||
if (enumerable != null)
|
||||
{
|
||||
return enumerable.Cast<object>().Any();
|
||||
}
|
||||
if (condition is Char)
|
||||
{
|
||||
return (Char)condition != '\0';
|
||||
}
|
||||
try
|
||||
{
|
||||
decimal number = (decimal)Convert.ChangeType(condition, typeof(decimal));
|
||||
return number != 0.0m;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace mustache
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines a tag that conditionally prints its content.
|
||||
/// </summary>
|
||||
internal abstract class ConditionTagDefinition : ContentTagDefinition
|
||||
{
|
||||
private const string conditionParameter = "condition";
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of a ConditionTagDefinition.
|
||||
/// </summary>
|
||||
/// <param name="tagName">The name of the tag.</param>
|
||||
protected ConditionTagDefinition(string tagName)
|
||||
: base(tagName, true)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the parameters that can be passed to the tag.
|
||||
/// </summary>
|
||||
/// <returns>The parameters.</returns>
|
||||
protected override IEnumerable<TagParameter> GetParameters()
|
||||
{
|
||||
return new TagParameter[] { new TagParameter(conditionParameter) { IsRequired = true } };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the tags that come into scope within the context of the current tag.
|
||||
/// </summary>
|
||||
/// <returns>The child tag definitions.</returns>
|
||||
protected override IEnumerable<string> GetChildTags()
|
||||
{
|
||||
return new string[] { "elif", "else" };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the given tag's generator should be used for a secondary (or substitute) text block.
|
||||
/// </summary>
|
||||
/// <param name="definition">The tag to inspect.</param>
|
||||
/// <returns>True if the tag's generator should be used as a secondary generator.</returns>
|
||||
public override bool ShouldCreateSecondaryGroup(TagDefinition definition)
|
||||
{
|
||||
return new string[] { "elif", "else" }.Contains(definition.Name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the primary generator group should be used to render the tag.
|
||||
/// </summary>
|
||||
/// <param name="arguments">The arguments passed to the tag.</param>
|
||||
/// <returns>
|
||||
/// True if the primary generator group should be used to render the tag;
|
||||
/// otherwise, false to use the secondary group.
|
||||
/// </returns>
|
||||
public override bool ShouldGeneratePrimaryGroup(Dictionary<string, object> arguments)
|
||||
{
|
||||
object condition = arguments[conditionParameter];
|
||||
return isConditionSatisfied(condition);
|
||||
}
|
||||
|
||||
private bool isConditionSatisfied(object condition)
|
||||
{
|
||||
if (condition == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
IEnumerable enumerable = condition as IEnumerable;
|
||||
if (enumerable != null)
|
||||
{
|
||||
return enumerable.Cast<object>().Any();
|
||||
}
|
||||
if (condition is Char)
|
||||
{
|
||||
return (Char)condition != '\0';
|
||||
}
|
||||
try
|
||||
{
|
||||
decimal number = (decimal)Convert.ChangeType(condition, typeof(decimal));
|
||||
return number != 0.0m;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the parameter that is used to create a new child context.
|
||||
/// </summary>
|
||||
/// <returns>The parameter that is used to create a new child context.</returns>
|
||||
public override TagParameter GetChildContextParameter()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
using System;
|
||||
|
||||
namespace mustache
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a context within a template.
|
||||
/// </summary>
|
||||
public sealed class Context
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of a Context.
|
||||
/// </summary>
|
||||
/// <param name="definition">The definition of tag that created the context.</param>
|
||||
/// <param name="argument">The argument used to create the context.</param>
|
||||
internal Context(TagDefinition definition, string argument)
|
||||
{
|
||||
Tag = definition;
|
||||
Argument = argument;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the tag that created the context.
|
||||
/// </summary>
|
||||
public TagDefinition Tag { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the argument used to create the context.
|
||||
/// </summary>
|
||||
public string Argument { get; private set; }
|
||||
}
|
||||
}
|
|
@ -1,71 +1,81 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace mustache
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines a tag that can iterate over a collection of items and render
|
||||
/// the content using each item as the context.
|
||||
/// </summary>
|
||||
internal sealed class EachTagDefinition : ContentTagDefinition
|
||||
{
|
||||
private const string collectionParameter = "collection";
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of an EachTagDefinition.
|
||||
/// </summary>
|
||||
public EachTagDefinition()
|
||||
: base("each", true)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the tag only exists within the scope of its parent.
|
||||
/// </summary>
|
||||
protected override bool GetIsContextSensitive()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the parameters that can be passed to the tag.
|
||||
/// </summary>
|
||||
/// <returns>The parameters.</returns>
|
||||
protected override IEnumerable<TagParameter> GetParameters()
|
||||
{
|
||||
return new TagParameter[] { new TagParameter(collectionParameter) { IsRequired = true } };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the context to use when building the inner text of the tag.
|
||||
/// </summary>
|
||||
/// <param name="writer">The text writer passed</param>
|
||||
/// <param name="scope">The current scope.</param>
|
||||
/// <param name="arguments">The arguments passed to the tag.</param>
|
||||
/// <returns>The scope to use when building the inner text of the tag.</returns>
|
||||
public override IEnumerable<NestedContext> GetChildContext(TextWriter writer, KeyScope scope, Dictionary<string, object> arguments)
|
||||
{
|
||||
object value = arguments[collectionParameter];
|
||||
IEnumerable enumerable = value as IEnumerable;
|
||||
if (enumerable == null)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
foreach (object item in enumerable)
|
||||
{
|
||||
yield return new NestedContext() { KeyScope = scope.CreateChildScope(item), Writer = writer };
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the tags that are in scope under this tag.
|
||||
/// </summary>
|
||||
/// <returns>The name of the tags that are in scope.</returns>
|
||||
protected override IEnumerable<string> GetChildTags()
|
||||
{
|
||||
return new string[] { };
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace mustache
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines a tag that can iterate over a collection of items and render
|
||||
/// the content using each item as the context.
|
||||
/// </summary>
|
||||
internal sealed class EachTagDefinition : ContentTagDefinition
|
||||
{
|
||||
private const string collectionParameter = "collection";
|
||||
private static readonly TagParameter collection = new TagParameter(collectionParameter) { IsRequired = true };
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of an EachTagDefinition.
|
||||
/// </summary>
|
||||
public EachTagDefinition()
|
||||
: base("each", true)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the tag only exists within the scope of its parent.
|
||||
/// </summary>
|
||||
protected override bool GetIsContextSensitive()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the parameters that can be passed to the tag.
|
||||
/// </summary>
|
||||
/// <returns>The parameters.</returns>
|
||||
protected override IEnumerable<TagParameter> GetParameters()
|
||||
{
|
||||
return new TagParameter[] { collection };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the context to use when building the inner text of the tag.
|
||||
/// </summary>
|
||||
/// <param name="writer">The text writer passed</param>
|
||||
/// <param name="scope">The current scope.</param>
|
||||
/// <param name="arguments">The arguments passed to the tag.</param>
|
||||
/// <returns>The scope to use when building the inner text of the tag.</returns>
|
||||
public override IEnumerable<NestedContext> GetChildContext(TextWriter writer, KeyScope scope, Dictionary<string, object> arguments)
|
||||
{
|
||||
object value = arguments[collectionParameter];
|
||||
IEnumerable enumerable = value as IEnumerable;
|
||||
if (enumerable == null)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
foreach (object item in enumerable)
|
||||
{
|
||||
yield return new NestedContext() { KeyScope = scope.CreateChildScope(item), Writer = writer };
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the tags that are in scope under this tag.
|
||||
/// </summary>
|
||||
/// <returns>The name of the tags that are in scope.</returns>
|
||||
protected override IEnumerable<string> GetChildTags()
|
||||
{
|
||||
return new string[] { };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the parameter that is used to create a new child context.
|
||||
/// </summary>
|
||||
/// <returns>The parameter that is used to create a new child context.</returns>
|
||||
public override TagParameter GetChildContextParameter()
|
||||
{
|
||||
return collection;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,35 +1,44 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace mustache
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines a tag that renders its content if all preceding if and elif tags.
|
||||
/// </summary>
|
||||
internal sealed class ElseTagDefinition : ContentTagDefinition
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of a ElseTagDefinition.
|
||||
/// </summary>
|
||||
public ElseTagDefinition()
|
||||
: base("else", true)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the tag only exists within the scope of its parent.
|
||||
/// </summary>
|
||||
protected override bool GetIsContextSensitive()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the tags that indicate the end of the current tag's content.
|
||||
/// </summary>
|
||||
protected override IEnumerable<string> GetClosingTags()
|
||||
{
|
||||
return new string[] { "if" };
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace mustache
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines a tag that renders its content if all preceding if and elif tags.
|
||||
/// </summary>
|
||||
internal sealed class ElseTagDefinition : ContentTagDefinition
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of a ElseTagDefinition.
|
||||
/// </summary>
|
||||
public ElseTagDefinition()
|
||||
: base("else", true)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the tag only exists within the scope of its parent.
|
||||
/// </summary>
|
||||
protected override bool GetIsContextSensitive()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the tags that indicate the end of the current tag's content.
|
||||
/// </summary>
|
||||
protected override IEnumerable<string> GetClosingTags()
|
||||
{
|
||||
return new string[] { "if" };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the parameter that is used to create a new child context.
|
||||
/// </summary>
|
||||
/// <returns>The parameter that is used to create a new child context.</returns>
|
||||
public override TagParameter GetChildContextParameter()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -74,7 +74,8 @@ namespace mustache
|
|||
}
|
||||
CompoundGenerator generator = new CompoundGenerator(_masterDefinition, new ArgumentCollection());
|
||||
Trimmer trimmer = new Trimmer();
|
||||
int formatIndex = buildCompoundGenerator(_masterDefinition, generator, trimmer, format, 0);
|
||||
List<Context> context = new List<Context>() { new Context(_masterDefinition, "this") };
|
||||
int formatIndex = buildCompoundGenerator(_masterDefinition, context, generator, trimmer, format, 0);
|
||||
string trailing = format.Substring(formatIndex);
|
||||
generator.AddStaticGenerators(trimmer.RecordText(trailing, false, false));
|
||||
trimmer.Trim();
|
||||
|
@ -165,7 +166,8 @@ namespace mustache
|
|||
}
|
||||
|
||||
private int buildCompoundGenerator(
|
||||
TagDefinition tagDefinition,
|
||||
TagDefinition tagDefinition,
|
||||
List<Context> context,
|
||||
CompoundGenerator generator,
|
||||
Trimmer trimmer,
|
||||
string format, int formatIndex)
|
||||
|
@ -193,7 +195,7 @@ namespace mustache
|
|||
string key = match.Groups["key"].Value;
|
||||
string alignment = match.Groups["alignment"].Value;
|
||||
string formatting = match.Groups["format"].Value;
|
||||
PlaceholderFoundEventArgs args = new PlaceholderFoundEventArgs(key, alignment, formatting);
|
||||
PlaceholderFoundEventArgs args = new PlaceholderFoundEventArgs(key, alignment, formatting, context.ToArray());
|
||||
if (PlaceholderFound != null)
|
||||
{
|
||||
PlaceholderFound(this, args);
|
||||
|
@ -216,7 +218,12 @@ namespace mustache
|
|||
generator.AddStaticGenerators(trimmer.RecordText(leading, true, false));
|
||||
ArgumentCollection arguments = getArguments(nextDefinition, match);
|
||||
CompoundGenerator compoundGenerator = new CompoundGenerator(nextDefinition, arguments);
|
||||
formatIndex = buildCompoundGenerator(nextDefinition, compoundGenerator, trimmer, format, formatIndex);
|
||||
string newContext = compoundGenerator.GetContextArgument();
|
||||
if (newContext != null)
|
||||
{
|
||||
context.Add(new Context(nextDefinition, newContext));
|
||||
}
|
||||
formatIndex = buildCompoundGenerator(nextDefinition, context, compoundGenerator, trimmer, format, formatIndex);
|
||||
generator.AddGenerator(nextDefinition, compoundGenerator);
|
||||
}
|
||||
else
|
||||
|
|
|
@ -11,7 +11,8 @@ namespace mustache
|
|||
public sealed class Generator
|
||||
{
|
||||
private readonly IGenerator _generator;
|
||||
private readonly List<EventHandler<MissingKeyEventArgs>> _handlers;
|
||||
private readonly List<EventHandler<KeyFoundEventArgs>> _foundHandlers;
|
||||
private readonly List<EventHandler<KeyNotFoundEventArgs>> _notFoundHandlers;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of a Generator.
|
||||
|
@ -20,16 +21,26 @@ namespace mustache
|
|||
internal Generator(IGenerator generator)
|
||||
{
|
||||
_generator = generator;
|
||||
_handlers = new List<EventHandler<MissingKeyEventArgs>>();
|
||||
_foundHandlers = new List<EventHandler<KeyFoundEventArgs>>();
|
||||
_notFoundHandlers = new List<EventHandler<KeyNotFoundEventArgs>>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when a key/property is found.
|
||||
/// </summary>
|
||||
public event EventHandler<KeyFoundEventArgs> KeyFound
|
||||
{
|
||||
add { _foundHandlers.Add(value); }
|
||||
remove { _foundHandlers.Remove(value); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when a key/property is not found in the object graph.
|
||||
/// </summary>
|
||||
public event EventHandler<MissingKeyEventArgs> KeyNotFound
|
||||
public event EventHandler<KeyNotFoundEventArgs> KeyNotFound
|
||||
{
|
||||
add { _handlers.Add(value); }
|
||||
remove { _handlers.Remove(value); }
|
||||
add { _notFoundHandlers.Add(value); }
|
||||
remove { _notFoundHandlers.Remove(value); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -60,7 +71,11 @@ namespace mustache
|
|||
private string render(IFormatProvider provider, object source)
|
||||
{
|
||||
KeyScope scope = new KeyScope(source);
|
||||
foreach (EventHandler<MissingKeyEventArgs> handler in _handlers)
|
||||
foreach (EventHandler<KeyFoundEventArgs> handler in _foundHandlers)
|
||||
{
|
||||
scope.KeyFound += handler;
|
||||
}
|
||||
foreach (EventHandler<KeyNotFoundEventArgs> handler in _notFoundHandlers)
|
||||
{
|
||||
scope.KeyNotFound += handler;
|
||||
}
|
||||
|
|
|
@ -1,40 +1,49 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace mustache
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines a tag that cannot contain inner text.
|
||||
/// </summary>
|
||||
public abstract class InlineTagDefinition : TagDefinition
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of an InlineTagDefinition.
|
||||
/// </summary>
|
||||
/// <param name="tagName">The name of the tag being defined.</param>
|
||||
protected InlineTagDefinition(string tagName)
|
||||
: base(tagName)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of an InlineTagDefinition.
|
||||
/// </summary>
|
||||
/// <param name="tagName">The name of the tag being defined.</param>
|
||||
/// <param name="isBuiltin">Specifies whether the tag is a built-in tag.</param>
|
||||
internal InlineTagDefinition(string tagName, bool isBuiltin)
|
||||
: base(tagName, isBuiltin)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether the tag can have content.
|
||||
/// </summary>
|
||||
/// <returns>True if the tag can have a body; otherwise, false.</returns>
|
||||
protected override bool GetHasContent()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace mustache
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines a tag that cannot contain inner text.
|
||||
/// </summary>
|
||||
public abstract class InlineTagDefinition : TagDefinition
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of an InlineTagDefinition.
|
||||
/// </summary>
|
||||
/// <param name="tagName">The name of the tag being defined.</param>
|
||||
protected InlineTagDefinition(string tagName)
|
||||
: base(tagName)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of an InlineTagDefinition.
|
||||
/// </summary>
|
||||
/// <param name="tagName">The name of the tag being defined.</param>
|
||||
/// <param name="isBuiltin">Specifies whether the tag is a built-in tag.</param>
|
||||
internal InlineTagDefinition(string tagName, bool isBuiltin)
|
||||
: base(tagName, isBuiltin)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether the tag can have content.
|
||||
/// </summary>
|
||||
/// <returns>True if the tag can have a body; otherwise, false.</returns>
|
||||
protected override bool GetHasContent()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the parameter that is used to create a child context.
|
||||
/// </summary>
|
||||
/// <returns>The parameter that is used to create a child context.</returns>
|
||||
public override TagParameter GetChildContextParameter()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
using System;
|
||||
|
||||
namespace mustache
|
||||
{
|
||||
/// <summary>
|
||||
/// Holds the information about a key that was found.
|
||||
/// </summary>
|
||||
public class KeyFoundEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of a KeyFoundEventArgs.
|
||||
/// </summary>
|
||||
/// <param name="key">The fully-qualified key.</param>
|
||||
internal KeyFoundEventArgs(string key, object value)
|
||||
{
|
||||
Key = key;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the fully-qualified key.
|
||||
/// </summary>
|
||||
public string Key { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the object to use as the substitute.
|
||||
/// </summary>
|
||||
public object Substitute { get; set; }
|
||||
}
|
||||
}
|
|
@ -5,14 +5,14 @@ namespace mustache
|
|||
/// <summary>
|
||||
/// Holds the information needed to handle a missing key.
|
||||
/// </summary>
|
||||
public class MissingKeyEventArgs : EventArgs
|
||||
public class KeyNotFoundEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of a MissingKeyEventArgs.
|
||||
/// Initializes a new instance of a KeyNotFoundEventArgs.
|
||||
/// </summary>
|
||||
/// <param name="key">The fully-qualified key.</param>
|
||||
/// <param name="missingMember">The part of the key that could not be found.</param>
|
||||
internal MissingKeyEventArgs(string key, string missingMember)
|
||||
internal KeyNotFoundEventArgs(string key, string missingMember)
|
||||
{
|
||||
Key = key;
|
||||
MissingMember = missingMember;
|
|
@ -34,10 +34,15 @@ namespace mustache
|
|||
_source = source;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when a key/property is found in the object graph.
|
||||
/// </summary>
|
||||
public event EventHandler<KeyFoundEventArgs> KeyFound;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when a key/property is not found in the object graph.
|
||||
/// </summary>
|
||||
public event EventHandler<MissingKeyEventArgs> KeyNotFound;
|
||||
public event EventHandler<KeyNotFoundEventArgs> KeyNotFound;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a child scope that searches for keys in the given object.
|
||||
|
@ -47,6 +52,7 @@ namespace mustache
|
|||
public KeyScope CreateChildScope(object source)
|
||||
{
|
||||
KeyScope scope = new KeyScope(source, this);
|
||||
scope.KeyFound = KeyFound;
|
||||
scope.KeyNotFound = KeyNotFound;
|
||||
return scope;
|
||||
}
|
||||
|
@ -75,6 +81,12 @@ namespace mustache
|
|||
nextLevel = handleKeyNotFound(name, member);
|
||||
}
|
||||
}
|
||||
if (KeyFound != null)
|
||||
{
|
||||
KeyFoundEventArgs args = new KeyFoundEventArgs(name, nextLevel);
|
||||
KeyFound(this, args);
|
||||
nextLevel = args.Substitute;
|
||||
}
|
||||
return nextLevel;
|
||||
}
|
||||
|
||||
|
@ -94,7 +106,7 @@ namespace mustache
|
|||
|
||||
private object handleKeyNotFound(string fullName, string memberName)
|
||||
{
|
||||
MissingKeyEventArgs args = new MissingKeyEventArgs(fullName, memberName);
|
||||
KeyNotFoundEventArgs args = new KeyNotFoundEventArgs(fullName, memberName);
|
||||
if (KeyNotFound != null)
|
||||
{
|
||||
KeyNotFound(this, args);
|
||||
|
|
|
@ -1,36 +1,45 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace mustache
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines a pseudo tag that wraps the entire content of a format string.
|
||||
/// </summary>
|
||||
internal sealed class MasterTagDefinition : ContentTagDefinition
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of a MasterTagDefinition.
|
||||
/// </summary>
|
||||
public MasterTagDefinition()
|
||||
: base(String.Empty, true)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the tag only exists within the scope of its parent.
|
||||
/// </summary>
|
||||
protected override bool GetIsContextSensitive()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the tags that indicate that the tag's context is closed.
|
||||
/// </summary>
|
||||
/// <returns>The tag names.</returns>
|
||||
protected override IEnumerable<string> GetClosingTags()
|
||||
{
|
||||
return new string[] { };
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace mustache
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines a pseudo tag that wraps the entire content of a format string.
|
||||
/// </summary>
|
||||
internal sealed class MasterTagDefinition : ContentTagDefinition
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of a MasterTagDefinition.
|
||||
/// </summary>
|
||||
public MasterTagDefinition()
|
||||
: base(String.Empty, true)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the tag only exists within the scope of its parent.
|
||||
/// </summary>
|
||||
protected override bool GetIsContextSensitive()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the tags that indicate that the tag's context is closed.
|
||||
/// </summary>
|
||||
/// <returns>The tag names.</returns>
|
||||
protected override IEnumerable<string> GetClosingTags()
|
||||
{
|
||||
return new string[] { };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the parameter that is used to create a new child context.
|
||||
/// </summary>
|
||||
/// <returns>The parameter that is used to create a new child context.</returns>
|
||||
public override TagParameter GetChildContextParameter()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,11 +14,13 @@ namespace mustache
|
|||
/// <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)
|
||||
/// <param name="context">The context where the placeholder was found.</param>
|
||||
internal PlaceholderFoundEventArgs(string key, string alignment, string formatting, Context[] context)
|
||||
{
|
||||
Key = key;
|
||||
Alignment = alignment;
|
||||
Formatting = formatting;
|
||||
Context = context;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -35,5 +37,10 @@ namespace mustache
|
|||
/// Gets or sets the formatting that will be applied to the substitute value.
|
||||
/// </summary>
|
||||
public string Formatting { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the context where the placeholder was found.
|
||||
/// </summary>
|
||||
public Context[] Context { get; private set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,6 +34,6 @@ using System.Runtime.CompilerServices;
|
|||
// 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.6.0")]
|
||||
[assembly: AssemblyFileVersion("0.0.6.0")]
|
||||
[assembly: AssemblyVersion("0.0.7.0")]
|
||||
[assembly: AssemblyFileVersion("0.0.7.0")]
|
||||
[assembly: InternalsVisibleTo("mustache-sharp.test")]
|
|
@ -1,184 +1,190 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using mustache.Properties;
|
||||
using System.IO;
|
||||
|
||||
namespace mustache
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the attributes of a custom tag.
|
||||
/// </summary>
|
||||
public abstract class TagDefinition
|
||||
{
|
||||
private readonly string _tagName;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of a TagDefinition.
|
||||
/// </summary>
|
||||
/// <param name="tagName">The name of the tag.</param>
|
||||
/// <exception cref="System.ArgumentException">The name of the tag is null or blank.</exception>
|
||||
protected TagDefinition(string tagName)
|
||||
: this(tagName, false)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of a TagDefinition.
|
||||
/// </summary>
|
||||
/// <param name="tagName">The name of the tag.</param>
|
||||
/// <param name="isBuiltIn">Specifies whether the tag is built-in or not. Checks are not performed on the names of built-in tags.</param>
|
||||
internal TagDefinition(string tagName, bool isBuiltIn)
|
||||
{
|
||||
if (!isBuiltIn && !RegexHelper.IsValidIdentifier(tagName))
|
||||
{
|
||||
throw new ArgumentException(Resources.BlankTagName, "tagName");
|
||||
}
|
||||
_tagName = tagName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the tag.
|
||||
/// </summary>
|
||||
public string Name
|
||||
{
|
||||
get { return _tagName; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the tag is limited to the parent tag's context.
|
||||
/// </summary>
|
||||
internal bool IsContextSensitive
|
||||
{
|
||||
get { return GetIsContextSensitive(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether a tag is limited to the parent tag's context.
|
||||
/// </summary>
|
||||
protected virtual bool GetIsContextSensitive()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the parameters that are defined for the tag.
|
||||
/// </summary>
|
||||
internal IEnumerable<TagParameter> Parameters
|
||||
{
|
||||
get { return GetParameters(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies which parameters are passed to the tag.
|
||||
/// </summary>
|
||||
/// <returns>The tag parameters.</returns>
|
||||
protected virtual IEnumerable<TagParameter> GetParameters()
|
||||
{
|
||||
return new TagParameter[] { };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the tag contains content.
|
||||
/// </summary>
|
||||
internal bool HasContent
|
||||
{
|
||||
get { return GetHasContent(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether tag has content.
|
||||
/// </summary>
|
||||
/// <returns>True if the tag has content; otherwise, false.</returns>
|
||||
protected abstract bool GetHasContent();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the tags that can indicate that the tag has closed.
|
||||
/// This field is only used if no closing tag is expected.
|
||||
/// </summary>
|
||||
internal IEnumerable<string> ClosingTags
|
||||
{
|
||||
get { return GetClosingTags(); }
|
||||
}
|
||||
|
||||
protected virtual IEnumerable<string> GetClosingTags()
|
||||
{
|
||||
if (HasContent)
|
||||
{
|
||||
return new string[] { Name };
|
||||
}
|
||||
else
|
||||
{
|
||||
return new string[] { };
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the tags that are in scope within the current tag.
|
||||
/// </summary>
|
||||
internal IEnumerable<string> ChildTags
|
||||
{
|
||||
get { return GetChildTags(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies which tags are scoped under the current tag.
|
||||
/// </summary>
|
||||
/// <returns>The child tag definitions.</returns>
|
||||
protected virtual IEnumerable<string> GetChildTags()
|
||||
{
|
||||
return new string[] { };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the context to use when building the inner text of the tag.
|
||||
/// </summary>
|
||||
/// <param name="writer">The text writer passed</param>
|
||||
/// <param name="scope">The current scope.</param>
|
||||
/// <param name="arguments">The arguments passed to the tag.</param>
|
||||
/// <returns>The scope to use when building the inner text of the tag.</returns>
|
||||
public virtual IEnumerable<NestedContext> GetChildContext(TextWriter writer, KeyScope scope, Dictionary<string, object> arguments)
|
||||
{
|
||||
yield return new NestedContext() { KeyScope = scope, Writer = writer };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies additional formatting to the inner text of the tag.
|
||||
/// </summary>
|
||||
/// <param name="writer">The text writer to write to.</param>
|
||||
/// <param name="arguments">The arguments passed to the tag.</param>
|
||||
public virtual void GetText(TextWriter writer, Dictionary<string, object> arguments)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Consolidates the text in the given writer to a string, using the given arguments as necessary.
|
||||
/// </summary>
|
||||
/// <param name="writer">The writer containing the text to consolidate.</param>
|
||||
/// <param name="arguments">The arguments passed to the tag.</param>
|
||||
/// <returns>The consolidated string.</returns>
|
||||
public virtual string ConsolidateWriter(TextWriter writer, Dictionary<string, object> arguments)
|
||||
{
|
||||
return writer.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Requests which generator group to associate the given tag type.
|
||||
/// </summary>
|
||||
/// <param name="definition">The child tag definition being grouped.</param>
|
||||
/// <returns>The name of the group to associate the given tag with.</returns>
|
||||
public virtual bool ShouldCreateSecondaryGroup(TagDefinition definition)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the group with the given name should have text generated for them.
|
||||
/// </summary>
|
||||
/// <param name="arguments">The arguments passed to the tag.</param>
|
||||
/// <returns>True if text should be generated for the group; otherwise, false.</returns>
|
||||
public virtual bool ShouldGeneratePrimaryGroup(Dictionary<string, object> arguments)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using mustache.Properties;
|
||||
using System.IO;
|
||||
|
||||
namespace mustache
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the attributes of a custom tag.
|
||||
/// </summary>
|
||||
public abstract class TagDefinition
|
||||
{
|
||||
private readonly string _tagName;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of a TagDefinition.
|
||||
/// </summary>
|
||||
/// <param name="tagName">The name of the tag.</param>
|
||||
/// <exception cref="System.ArgumentException">The name of the tag is null or blank.</exception>
|
||||
protected TagDefinition(string tagName)
|
||||
: this(tagName, false)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of a TagDefinition.
|
||||
/// </summary>
|
||||
/// <param name="tagName">The name of the tag.</param>
|
||||
/// <param name="isBuiltIn">Specifies whether the tag is built-in or not. Checks are not performed on the names of built-in tags.</param>
|
||||
internal TagDefinition(string tagName, bool isBuiltIn)
|
||||
{
|
||||
if (!isBuiltIn && !RegexHelper.IsValidIdentifier(tagName))
|
||||
{
|
||||
throw new ArgumentException(Resources.BlankTagName, "tagName");
|
||||
}
|
||||
_tagName = tagName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the tag.
|
||||
/// </summary>
|
||||
public string Name
|
||||
{
|
||||
get { return _tagName; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the tag is limited to the parent tag's context.
|
||||
/// </summary>
|
||||
internal bool IsContextSensitive
|
||||
{
|
||||
get { return GetIsContextSensitive(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether a tag is limited to the parent tag's context.
|
||||
/// </summary>
|
||||
protected virtual bool GetIsContextSensitive()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the parameters that are defined for the tag.
|
||||
/// </summary>
|
||||
internal IEnumerable<TagParameter> Parameters
|
||||
{
|
||||
get { return GetParameters(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies which parameters are passed to the tag.
|
||||
/// </summary>
|
||||
/// <returns>The tag parameters.</returns>
|
||||
protected virtual IEnumerable<TagParameter> GetParameters()
|
||||
{
|
||||
return new TagParameter[] { };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the tag contains content.
|
||||
/// </summary>
|
||||
internal bool HasContent
|
||||
{
|
||||
get { return GetHasContent(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether tag has content.
|
||||
/// </summary>
|
||||
/// <returns>True if the tag has content; otherwise, false.</returns>
|
||||
protected abstract bool GetHasContent();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the tags that can indicate that the tag has closed.
|
||||
/// This field is only used if no closing tag is expected.
|
||||
/// </summary>
|
||||
internal IEnumerable<string> ClosingTags
|
||||
{
|
||||
get { return GetClosingTags(); }
|
||||
}
|
||||
|
||||
protected virtual IEnumerable<string> GetClosingTags()
|
||||
{
|
||||
if (HasContent)
|
||||
{
|
||||
return new string[] { Name };
|
||||
}
|
||||
else
|
||||
{
|
||||
return new string[] { };
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the tags that are in scope within the current tag.
|
||||
/// </summary>
|
||||
internal IEnumerable<string> ChildTags
|
||||
{
|
||||
get { return GetChildTags(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Specifies which tags are scoped under the current tag.
|
||||
/// </summary>
|
||||
/// <returns>The child tag definitions.</returns>
|
||||
protected virtual IEnumerable<string> GetChildTags()
|
||||
{
|
||||
return new string[] { };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the parameter that will be used to create a new child scope.
|
||||
/// </summary>
|
||||
/// <returns>The parameter that will be used to create a new child scope -or- null if no new scope is created.</returns>
|
||||
public abstract TagParameter GetChildContextParameter();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the context to use when building the inner text of the tag.
|
||||
/// </summary>
|
||||
/// <param name="writer">The text writer passed</param>
|
||||
/// <param name="scope">The current scope.</param>
|
||||
/// <param name="arguments">The arguments passed to the tag.</param>
|
||||
/// <returns>The scope to use when building the inner text of the tag.</returns>
|
||||
public virtual IEnumerable<NestedContext> GetChildContext(TextWriter writer, KeyScope scope, Dictionary<string, object> arguments)
|
||||
{
|
||||
yield return new NestedContext() { KeyScope = scope, Writer = writer };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies additional formatting to the inner text of the tag.
|
||||
/// </summary>
|
||||
/// <param name="writer">The text writer to write to.</param>
|
||||
/// <param name="arguments">The arguments passed to the tag.</param>
|
||||
public virtual void GetText(TextWriter writer, Dictionary<string, object> arguments)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Consolidates the text in the given writer to a string, using the given arguments as necessary.
|
||||
/// </summary>
|
||||
/// <param name="writer">The writer containing the text to consolidate.</param>
|
||||
/// <param name="arguments">The arguments passed to the tag.</param>
|
||||
/// <returns>The consolidated string.</returns>
|
||||
public virtual string ConsolidateWriter(TextWriter writer, Dictionary<string, object> arguments)
|
||||
{
|
||||
return writer.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Requests which generator group to associate the given tag type.
|
||||
/// </summary>
|
||||
/// <param name="definition">The child tag definition being grouped.</param>
|
||||
/// <returns>The name of the group to associate the given tag with.</returns>
|
||||
public virtual bool ShouldCreateSecondaryGroup(TagDefinition definition)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the group with the given name should have text generated for them.
|
||||
/// </summary>
|
||||
/// <param name="arguments">The arguments passed to the tag.</param>
|
||||
/// <returns>True if text should be generated for the group; otherwise, false.</returns>
|
||||
public virtual bool ShouldGeneratePrimaryGroup(Dictionary<string, object> arguments)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,52 +1,62 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace mustache
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines a tag that changes the scope to the object passed as an argument.
|
||||
/// </summary>
|
||||
internal sealed class WithTagDefinition : ContentTagDefinition
|
||||
{
|
||||
private const string contextParameter = "context";
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of a WithTagDefinition.
|
||||
/// </summary>
|
||||
public WithTagDefinition()
|
||||
: base("with", true)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the tag only exists within the scope of its parent.
|
||||
/// </summary>
|
||||
protected override bool GetIsContextSensitive()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the parameters that can be passed to the tag.
|
||||
/// </summary>
|
||||
/// <returns>The parameters.</returns>
|
||||
protected override IEnumerable<TagParameter> GetParameters()
|
||||
{
|
||||
return new TagParameter[] { new TagParameter(contextParameter) { IsRequired = true } };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the context to use when building the inner text of the tag.
|
||||
/// </summary>
|
||||
/// <param name="writer">The text writer passed</param>
|
||||
/// <param name="scope">The current scope.</param>
|
||||
/// <param name="arguments">The arguments passed to the tag.</param>
|
||||
/// <returns>The scope to use when building the inner text of the tag.</returns>
|
||||
public override IEnumerable<NestedContext> GetChildContext(TextWriter writer, KeyScope scope, Dictionary<string, object> arguments)
|
||||
{
|
||||
object context = arguments[contextParameter];
|
||||
yield return new NestedContext() { KeyScope = scope.CreateChildScope(context), Writer = writer };
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace mustache
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines a tag that changes the scope to the object passed as an argument.
|
||||
/// </summary>
|
||||
internal sealed class WithTagDefinition : ContentTagDefinition
|
||||
{
|
||||
private const string contextParameter = "context";
|
||||
private static readonly TagParameter context = new TagParameter(contextParameter) { IsRequired = true };
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of a WithTagDefinition.
|
||||
/// </summary>
|
||||
public WithTagDefinition()
|
||||
: base("with", true)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the tag only exists within the scope of its parent.
|
||||
/// </summary>
|
||||
protected override bool GetIsContextSensitive()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the parameters that can be passed to the tag.
|
||||
/// </summary>
|
||||
/// <returns>The parameters.</returns>
|
||||
protected override IEnumerable<TagParameter> GetParameters()
|
||||
{
|
||||
return new TagParameter[] { context };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the parameter that is used to create a new child context.
|
||||
/// </summary>
|
||||
/// <returns>The parameter that is used to create a new child context.</returns>
|
||||
public override TagParameter GetChildContextParameter()
|
||||
{
|
||||
return context;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the context to use when building the inner text of the tag.
|
||||
/// </summary>
|
||||
/// <param name="writer">The text writer passed</param>
|
||||
/// <param name="scope">The current scope.</param>
|
||||
/// <param name="arguments">The arguments passed to the tag.</param>
|
||||
/// <returns>The scope to use when building the inner text of the tag.</returns>
|
||||
public override IEnumerable<NestedContext> GetChildContext(TextWriter writer, KeyScope scope, Dictionary<string, object> arguments)
|
||||
{
|
||||
object context = arguments[contextParameter];
|
||||
yield return new NestedContext() { KeyScope = scope.CreateChildScope(context), Writer = writer };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,6 +38,8 @@
|
|||
<Compile Include="CompoundGenerator.cs" />
|
||||
<Compile Include="ConditionTagDefinition.cs" />
|
||||
<Compile Include="ContentTagDefinition.cs" />
|
||||
<Compile Include="Context.cs" />
|
||||
<Compile Include="KeyFoundEventArgs.cs" />
|
||||
<Compile Include="InlineTagDefinition.cs" />
|
||||
<Compile Include="EachTagDefinition.cs" />
|
||||
<Compile Include="ElifTagDefinition.cs" />
|
||||
|
@ -50,7 +52,7 @@
|
|||
<Compile Include="PlaceholderFoundEventArgs.cs" />
|
||||
<Compile Include="KeyGenerator.cs" />
|
||||
<Compile Include="MasterTagDefinition.cs" />
|
||||
<Compile Include="MissingKeyEventArgs.cs" />
|
||||
<Compile Include="KeyNotFoundEventArgs.cs" />
|
||||
<Compile Include="NestedContext.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Properties\Resources.Designer.cs">
|
||||
|
|
Loading…
Reference in New Issue