using System;
using System.Globalization;
using System.IO;
namespace mustache
{
///
/// Generates text by substituting an object's values for placeholders.
///
public sealed class Generator
{
private readonly IGenerator _generator;
///
/// Initializes a new instance of a Generator.
///
/// The text generator to wrap.
internal Generator(IGenerator generator)
{
_generator = generator;
}
///
/// Gets the text that is generated for the given object.
///
/// The object to generate the text with.
/// The text generated for the given object.
public string Render(object source)
{
return render(CultureInfo.CurrentCulture, source);
}
///
/// Gets the text that is generated for the given object.
///
/// The format provider to use.
/// The object to generate the text with.
/// The text generated for the given object.
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();
}
}
}