DocSpace-buildtools/common/ASC.Core.Common/Notify/Patterns/ReplacePatternFormatter.cs

35 lines
905 B
C#
Raw Normal View History

2022-02-15 11:52:43 +00:00
namespace ASC.Notify.Patterns;
public sealed class ReplacePatternFormatter : PatternFormatter
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
public const string DefaultPattern = @"[[]%(?<tagName>[a-zA-Z0-9_\-.]+)%[]]";
public ReplacePatternFormatter()
: base(DefaultPattern)
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
internal ReplacePatternFormatter(string tagPattern, bool formatMessage)
: base(tagPattern, formatMessage)
{
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
protected override string FormatText(string text, ITagValue[] tagsValues)
{
if (string.IsNullOrEmpty(text))
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
return text;
2019-05-15 14:56:09 +00:00
}
2022-02-15 11:52:43 +00:00
var formattedText = RegEx.Replace(text,
match =>
{
2022-02-15 11:52:43 +00:00
var value = Array.Find(tagsValues, v => v.Tag == match.Groups["tagName"].Value);
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
return value != null && value.Value != null ? Convert.ToString(value.Value) : match.Value;
});
2022-02-15 11:52:43 +00:00
return formattedText;
2019-05-15 14:56:09 +00:00
}
}