using System.Net;
namespace JiDangJia.Core;
public static class VoucherTemplates
{
public static readonly Dictionary<string,string> Fields=new(){["ticketNo"]="凭证号",["customerName"]="客户",["itemName"]="物品",["amount"]="金额",["date"]="日期",["dueDate"]="到期日期",["remark"]="备注"};
public static string Basic(string title,string footer,IEnumerable<string> 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=>"<tr><td>"+Fields[k]+"</td><td>{{"+k+"}}</td></tr>"));
return "<!doctype html><html lang='zh-CN'><head><meta charset='utf-8'><meta name='viewport' content='width=device-width'><style>@page{size:"+pageSize+(landscape?" landscape":" portrait")+";margin:15mm}body{font-family:sans-serif;color:#2b211a;margin:24px}h1,h2{text-align:center}h1{font-size:23px}h2{font-size:18px}table{width:100%;border-collapse:collapse;margin:24px 0}td{border:1px solid #bbb;padding:10px;overflow-wrap:anywhere}td:first-child{width:26%}footer{white-space:pre-wrap;line-height:2;margin-top:30px}small{color:#666}</style></head><body><h1>{{shopName}}</h1><h2>"+WebUtility.HtmlEncode(title)+"</h2><table>"+rows+"</table><small>{{address}} · {{phone}}</small><footer>"+WebUtility.HtmlEncode(footer)+"</footer></body></html>";
}
public static string Render(string html,IReadOnlyDictionary<string,string> 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);
}
}
using System.Net;
namespace JiDangJia.Core;
public static class VoucherTemplates
{
public static readonly Dictionary<string,string> Fields=new(){["ticketNo"]="凭证号",["customerName"]="客户",["itemName"]="物品",["amount"]="金额",["date"]="日期",["dueDate"]="到期日期",["remark"]="备注"};
public static string Basic(string title,string footer,IEnumerable<string> 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=>"<tr><td>"+Fields[k]+"</td><td>{{"+k+"}}</td></tr>"));
return "<!doctype html><html lang='zh-CN'><head><meta charset='utf-8'><meta name='viewport' content='width=device-width'><style>@page{size:"+pageSize+(landscape?" landscape":" portrait")+";margin:15mm}body{font-family:sans-serif;color:#2b211a;margin:24px}h1,h2{text-align:center}h1{font-size:23px}h2{font-size:18px}table{width:100%;border-collapse:collapse;margin:24px 0}td{border:1px solid #bbb;padding:10px;overflow-wrap:anywhere}td:first-child{width:26%}footer{white-space:pre-wrap;line-height:2;margin-top:30px}small{color:#666}</style></head><body><h1>{{shopName}}</h1><h2>"+WebUtility.HtmlEncode(title)+"</h2><table>"+rows+"</table><small>{{address}} · {{phone}}</small><footer>"+WebUtility.HtmlEncode(footer)+"</footer></body></html>";
}
public static string Render(string html,IReadOnlyDictionary<string,string> 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);
}
}