public override TemplMatchTable Handler(DocX doc, object model, TemplMatchTable m) { m.Validate(); var e = TemplModelEntry.Get(model, m.Fields.First()); var maxCells = TemplConst.MaxMatchesPerScope; if (m.Fields.Length == 2) { if (!uint.TryParse(m.Fields[1], out maxCells)) { throw new ArgumentException($"Templ: Dynamic Cell Module '{m.Placeholder}' has invalid maximum cells: '{m.Fields[1]}'"); } } var keys = e.ToStringKeys().Take((int)maxCells).ToList(); var columnDiff = keys.Count() - m.Table.ColumnCount + m.CellIndex; for (int i = 0; i < columnDiff; i++) { m.Table.InsertColumn(); } for (int i = 0; i < keys.Count(); i++) { var s = $"{TemplConst.MatchOpen}{TemplConst.Prefix.Text}{TemplConst.FieldSep}{m.Fields[0]}[{keys[i]}]{TemplConst.MatchClose}"; var p = m.Row.Cells[i + m.CellIndex].Paragraphs.Last(); p.InsertParagraphAfterSelf(m.Paragraph).ReplaceText(m.Paragraph.Text, s); p.Remove(trackChanges: false); TemplDoc.CellCopyProperties(m.Cell, m.Row.Cells[i + m.CellIndex]); } m.RemovePlaceholder(); m.Removed = true; return(m); }
public override TemplMatchText Handler(DocX doc, object model, TemplMatchText m) { int w = -1; if (m.Fields.Length == 2) { // 2 parts: second part is an int for the width if (!int.TryParse(m.Fields[1], out w)) { throw new FormatException($"Templ: Picture {m.Body} has a non-integer value for width ({m.Fields[1]})"); } } var e = TemplModelEntry.Get(model, m.Fields[0]); //Try as array, as collection, as single if (e.Value is TemplGraphic) { return(m.ToPicture((e.Value as TemplGraphic).Load(doc), w)); } if (e.Value is TemplGraphic[]) { return(m.ToPictures((e.Value as TemplGraphic[]) .Select(g => g.Load(doc)).ToArray(), w)); } if (e.Value is IEnumerable <TemplGraphic> ) { return(m.ToPictures((e.Value as IEnumerable <TemplGraphic>) .Select(g => g.Load(doc)).ToList(), w)); } throw new InvalidCastException($"Templ: Failed to retrieve picture(s) from the model at path \"{e.Path}\"; its actual type is \"{e.Type}\""); }
public override TemplMatchText Handler(DocX doc, object model, TemplMatchText m) { if (TemplModelEntry.Get(model, m.Body).AsType <bool>()) { m.Paragraph.Remove(false); m.Removed = true; m.Expired = true; } return(m); }
public override TemplMatchTable Handler(DocX doc, object model, TemplMatchTable m) { m.RemovePlaceholder(); if (m.Fields[0].Length > 0) { // 2 parts: first part is a bool expression in the model for 'delete table' m.Expired = TemplModelEntry.Get(model, m.Fields[0]).AsType <bool>(); } return(m); }
public override TemplMatchSection Handler(DocX doc, object model, TemplMatchSection m) { m.RemovePlaceholder(); if (m.Fields[0].Length > 0) { // first field is a bool expression in the model for 'delete section' m.Expired = TemplModelEntry.Get(model, m.Fields[0]).AsType <bool>(); } return(m); }
public override TemplMatchText Handler(DocX doc, object model, TemplMatchText m) { var e = TemplModelEntry.Get(model, m.Body); m.RemovePlaceholder(); foreach (var key in e.ToStringKeys().Reverse()) { var p = m.Paragraph.InsertParagraphAfterSelf(m.Paragraph); new TemplCollectionModule().BuildFromScope(doc, model, new Paragraph[] { p }, $"{m.Body}[{key}]"); } m.Paragraph.Remove(false); m.Removed = true; return(m); }
public override TemplMatchPicture Handler(DocX doc, object model, TemplMatchPicture m) { var e = TemplModelEntry.Get(model, m.Body); var w = m.Picture.Width; // Single picture: add text placeholder, expire the placeholder picture if (e.Value is TemplGraphic) { return(m.ToText($"{TemplConst.MatchOpen}{TemplConst.Prefix.Picture}{TemplConst.FieldSep}{m.Body}{TemplConst.FieldSep}{w}{TemplConst.MatchClose}")); } // Multiple pictures: add repeating list placeholder, expire the placeholder picture if (e.Value is TemplGraphic[] || e.Value is IEnumerable <TemplGraphic> ) { return(m.ToText($"{TemplConst.MatchOpen}{TemplConst.Prefix.List}{TemplConst.FieldSep}{m.Body}{TemplConst.MatchClose}{TemplConst.MatchOpen}${TemplConst.FieldSep}{TemplConst.Prefix.Picture}{TemplConst.FieldSep}{TemplConst.FieldSep}{w}{TemplConst.MatchClose}")); } throw new InvalidCastException($"Templ: Failed to retrieve picture(s) from the model at path \"{e.Path}\"; its actual type is \"{e.Type}\""); }
public override TemplMatchTable Handler(DocX doc, object model, TemplMatchTable m) { var e = TemplModelEntry.Get(model, m.Body); m.Validate(); var idx = m.RowIndex; m.RemovePlaceholder(); foreach (var key in e.ToStringKeys()) { var r = m.Table.InsertRow(m.Row, ++idx); new TemplCollectionModule().BuildFromScope(doc, model, r.Paragraphs, $"{m.Body}[{key}]"); } m.Row.Remove(); m.Removed = true; return(m); }
public override TemplMatchHyperlink Handler(DocX doc, object model, TemplMatchHyperlink m) { var e = TemplModelEntry.Get(model, m.Body); if (e.Value is TemplUrl) { var url = e.Value as TemplUrl; if (url.ToDelete) { m.Remove(); } if (url.Text?.Length > 0) { m.SetText(url.Text); } if (url.Url?.Length > 0) { m.SetUrl(url.Url); } return(m); } throw new InvalidCastException($"Templ: Failed to retrieve url from the model at path \"{e.Path}\"; its actual type is \"{e.Type}\""); }
public override TemplMatchTable Handler(DocX doc, object model, TemplMatchTable m) { var width = m.Table.Rows.First().Cells.Count; var keys = TemplModelEntry.Get(model, m.Body).ToStringKeys(); var nrows = (int)Math.Ceiling(keys.Count() / (float)width); m.Validate(); m.RemovePlaceholder(); for (int n = 0; n < width; n++) { if (n != m.CellIndex) { TemplDoc.CellCopyContents(m.Cell, m.Row.Cells[n]); } } Row row = m.Row; for (int keyIdx = 0; keyIdx < nrows * width; keyIdx++) { if (keyIdx % width == 0) { row = m.Table.InsertRow(m.Row, m.RowIndex + keyIdx / width + 1); } Cell cell = row.Cells[keyIdx % width]; if (keyIdx < keys.Count()) { new TemplCollectionModule().BuildFromScope(doc, model, cell.Paragraphs, $"{m.Body}[{keys[keyIdx]}]"); } else { TemplDoc.CellClear(cell); } } m.Row.Remove(); m.Removed = true; return(m); }
public override TemplMatchText Handler(DocX doc, object model, TemplMatchText m) { m.ToText(TemplModelEntry.Get(model, m.Body).ToString()); return(m); }