Files
ssh-access-tui/outlook-bulk-sender/BulkArchiveMailer.Core/TemplateEngine.cs
T
redlineoandClaude Sonnet 5 804b9610ca Generalize mail templates from a fixed pair to an operator-managed list
ArchiveKind was a hardcoded two-value enum (ssh_access/config_update),
wired through FilenameParser's regex, MailTemplateSet's two fixed
slots, and SettingsForm's two fixed tabs. Operators wanted arbitrary
custom templates (own file-name suffix, subject, body) without a code
change per new archive kind ssh-access-tui might start producing.

Replaced with MailTemplateDefinition (DisplayName/FileSuffix/Subject/
BodyHtml) in an open-ended list: FilenameParser now accepts any
"{login}_{suffix}.7z" and ArchiveScanner matches the suffix against
the configured list; SendJob.Kind/SendLogEntry.Kind became plain
strings; SettingsForm gained a template list (+/- buttons, editor
panel) replacing the two static tabs. ssh_access/config_update are now
just the two default entries in that list, not special-cased.

Handles the two backward-compat edges that matter for a tool already
in real use: old config.json's object-shaped "templates" migrates to
the new list shape transparently on first load (DataContractJsonSerializer
turned out to silently produce an empty list rather than throw on the
old shape, so detection is structural, not exception-based), and old
send-log.csv rows with PascalCase Kind values (SshAccess/ConfigUpdate)
still dedupe correctly against the new snake_case suffixes.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-24 11:26:31 +03:00

57 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace BulkArchiveMailer.Core
{
/// <summary>
/// Minimal placeholder substitution for Subject/BodyHtml templates.
/// Deliberately simple (no conditionals/loops) - templates are short,
/// hand-edited strings, not a general templating language.
/// </summary>
public static class TemplateEngine
{
private static readonly Regex PlaceholderPattern = new Regex(@"\{(?<name>[A-Za-z0-9_]+)\}", RegexOptions.Compiled);
/// <summary>
/// Replaces every "{Name}" placeholder with the matching value from
/// <paramref name="values"/> (case-insensitive lookup). Unknown
/// placeholders are left untouched rather than throwing, so a typo
/// in a hand-edited template shows up visibly in the preview
/// instead of crashing the scan.
/// </summary>
public static string Render(string template, IDictionary<string, string> values)
{
if (string.IsNullOrEmpty(template))
{
return template ?? string.Empty;
}
return PlaceholderPattern.Replace(template, match =>
{
var name = match.Groups["name"].Value;
foreach (var kvp in values)
{
if (string.Equals(kvp.Key, name, StringComparison.OrdinalIgnoreCase))
{
return kvp.Value ?? string.Empty;
}
}
return match.Value; // leave "{Unknown}" as-is
});
}
/// <summary>Builds the {Login}/{Email}/{Date} placeholder values for one recipient, ready to pass to Render(string, IDictionary&lt;string,string&gt;) for a template's Subject and BodyHtml separately.</summary>
public static Dictionary<string, string> BuildValues(string login, string email)
{
return new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
["Login"] = login ?? "",
["Email"] = email ?? "",
["Date"] = DateTime.Now.ToString("dd.MM.yyyy"),
};
}
}
}