2013-01-09 02:33:53 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace mustache
|
|
|
|
|
{
|
2013-01-10 02:17:45 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Builds text by combining the output of other generators.
|
|
|
|
|
/// </summary>
|
2013-01-09 02:33:53 +00:00
|
|
|
|
internal sealed class CompoundGenerator : IGenerator
|
|
|
|
|
{
|
2013-01-10 02:17:45 +00:00
|
|
|
|
private readonly TagDefinition _definition;
|
|
|
|
|
private readonly ArgumentCollection _arguments;
|
|
|
|
|
private readonly List<IGenerator> _primaryGenerators;
|
|
|
|
|
private IGenerator _subGenerator;
|
2013-01-09 02:33:53 +00:00
|
|
|
|
|
2013-01-10 02:17:45 +00:00
|
|
|
|
/// <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)
|
2013-01-09 02:33:53 +00:00
|
|
|
|
{
|
2013-01-10 02:17:45 +00:00
|
|
|
|
_definition = definition;
|
|
|
|
|
_arguments = arguments;
|
|
|
|
|
_primaryGenerators = new List<IGenerator>();
|
2013-01-09 02:33:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-10 02:17:45 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds the given generator.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="generator">The generator to add.</param>
|
|
|
|
|
public void AddGenerator(IGenerator generator)
|
2013-01-09 02:33:53 +00:00
|
|
|
|
{
|
2013-01-10 02:17:45 +00:00
|
|
|
|
addGenerator(generator, false);
|
2013-01-09 02:33:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-10 02:17:45 +00:00
|
|
|
|
/// <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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addGenerator(IGenerator generator, bool isSubGenerator)
|
|
|
|
|
{
|
|
|
|
|
if (isSubGenerator)
|
|
|
|
|
{
|
|
|
|
|
_subGenerator = generator;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_primaryGenerators.Add(generator);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string IGenerator.GetText(IFormatProvider provider, KeyScope scope)
|
2013-01-09 02:33:53 +00:00
|
|
|
|
{
|
|
|
|
|
StringBuilder builder = new StringBuilder();
|
2013-01-10 02:17:45 +00:00
|
|
|
|
Dictionary<string, object> arguments = _arguments.GetArguments(scope);
|
|
|
|
|
IEnumerable<KeyScope> scopes = _definition.GetChildScopes(scope, arguments);
|
|
|
|
|
List<IGenerator> generators;
|
|
|
|
|
if (_definition.ShouldGeneratePrimaryGroup(arguments))
|
|
|
|
|
{
|
|
|
|
|
generators = _primaryGenerators;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
generators = new List<IGenerator>() { _subGenerator };
|
|
|
|
|
}
|
|
|
|
|
foreach (KeyScope childScope in scopes)
|
2013-01-09 02:33:53 +00:00
|
|
|
|
{
|
2013-01-10 02:17:45 +00:00
|
|
|
|
foreach (IGenerator generator in generators)
|
|
|
|
|
{
|
|
|
|
|
builder.Append(generator.GetText(provider, childScope));
|
|
|
|
|
}
|
2013-01-09 02:33:53 +00:00
|
|
|
|
}
|
|
|
|
|
string innerText = builder.ToString();
|
2013-01-10 02:17:45 +00:00
|
|
|
|
string outerText = _definition.Decorate(provider, innerText, arguments);
|
|
|
|
|
return outerText;
|
2013-01-09 02:33:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|