2013-01-10 02:17:45 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
2013-01-16 20:10:25 +00:00
|
|
|
|
using System.IO;
|
2013-01-10 02:17:45 +00:00
|
|
|
|
|
|
|
|
|
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);
|
2013-01-16 20:10:25 +00:00
|
|
|
|
StringWriter writer = new StringWriter(provider);
|
|
|
|
|
_generator.GetText(scope, writer);
|
|
|
|
|
return writer.ToString();
|
2013-01-10 02:17:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|