Files
mixtape/zero.Core/Renderer/TokenReplacement.cs
T
2020-12-18 20:50:13 +01:00

57 lines
1.2 KiB
C#

using System.Collections.Generic;
using System.Text.RegularExpressions;
using zero.Core.Extensions;
namespace zero.Core.Renderer
{
public class TokenReplacement
{
static Regex TokenRegex;
static TokenReplacement()
{
TokenRegex = new Regex("{([\\w-_.]+)}", RegexOptions.IgnoreCase);
}
public static string Apply(string text, IDictionary<string, string> tokens)
{
if (text.IsNullOrWhiteSpace())
{
return text;
}
MatchCollection matches = TokenRegex.Matches(text);
foreach (Match match in matches)
{
if (!match.Success)
{
continue;
}
string original = match.Value;
string token = match.Groups[1].Value;
if (tokens.ContainsKey(token))
{
text = text.Replace(original, tokens[token]);
}
}
return text;
//foreach ((string key, string value) in tokens)
//{
// string tokenKey = key.EnsureStartsWith(BEGIN).EnsureEndsWith(END);
// string tokenValue = value; // TODO escape for HTML?
// text = text.Replace(tokenKey, value);
//}
//return text;
}
}
}