using System.Net; namespace JiDangJia.Core; public static class VoucherTemplates { public static readonly Dictionary Fields=new(){["ticketNo"]="凭证号",["customerName"]="客户",["itemName"]="物品",["amount"]="金额",["date"]="日期",["dueDate"]="到期日期",["remark"]="备注"}; public static string Basic(string title,string footer,IEnumerable fields,string pageSize="A5",bool landscape=false) { if(pageSize is not ("A4" or "A5"))throw new ArgumentException("纸张只支持 A4 / A5"); var rows=string.Join("",fields.Where(Fields.ContainsKey).Distinct().Select(k=>""+Fields[k]+"{{"+k+"}}")); return "

{{shopName}}

"+WebUtility.HtmlEncode(title)+"

"+rows+"
{{address}} · {{phone}}
"+WebUtility.HtmlEncode(footer)+"
"; } public static string Render(string html,IReadOnlyDictionary values) { // One pass: data that happens to contain another placeholder is still just data. return System.Text.RegularExpressions.Regex.Replace(html,@"\{\{([A-Za-z]+)\}\}",m=>values.TryGetValue(m.Groups[1].Value,out var value)?WebUtility.HtmlEncode(value):m.Value); } }