using System; using System.Collections.Generic; using InABox.Core; using Scriban; using Scriban.Runtime; namespace PRSDesktop { public static class DataModelUtils { public static string ParseTemplate(DataModel model, string data) { if (string.IsNullOrWhiteSpace(data)) return ""; var template = Template.Parse(data); if (template.HasErrors) throw new Exception(string.Join("\n", template.Messages)); var templatecontext = new TemplateContext { MemberRenamer = member => member.Name }; var so = new ScriptObject(); ScriptObjectExtensions.Import(so, "string.to_base64", new Func(b => Convert.ToBase64String(b))); var tables = model.AsDictionary; foreach (var key in tables.Keys) { var objects = new List(); foreach (var tablerow in tables[key].Rows) objects.Add(tablerow.ToObject(key)); so.Add(key.Name, objects); } templatecontext.PushGlobal(so); return template.Render(templatecontext); } } }