using System;
using System.Security;
namespace Mustache
{
public sealed class HtmlFormatCompiler
{
private readonly FormatCompiler compiler;
public HtmlFormatCompiler()
{
compiler = new FormatCompiler();
compiler.AreExtensionTagsAllowed = true;
compiler.RemoveNewLines = true;
}
///
/// Occurs when a placeholder is found in the template.
///
public event EventHandler PlaceholderFound
{
add { compiler.PlaceholderFound += value; }
remove { compiler.PlaceholderFound -= value; }
}
///
/// Occurs when a variable is found in the template.
///
public event EventHandler VariableFound
{
add { compiler.VariableFound += value; }
remove { compiler.VariableFound -= value; }
}
///
/// Registers the given tag definition with the parser.
///
/// The tag definition to register.
/// Specifies whether the tag is immediately in scope.
public void RegisterTag(TagDefinition definition, bool isTopLevel)
{
compiler.RegisterTag(definition, isTopLevel);
}
///
/// Builds a text generator based on the given format.
///
/// The format to parse.
/// The text generator.
public Generator Compile(string format)
{
Generator generator = compiler.Compile(format);
generator.TagFormatted += escapeInvalidHtml;
return generator;
}
private static void escapeInvalidHtml(object sender, TagFormattedEventArgs e)
{
if (e.IsExtension)
{
// Do not escape text within triple curly braces
return;
}
e.Substitute = SecurityElement.Escape(e.Substitute);
}
}
}