static bool CollapseNonEmpty(object value, string metadata, string property, ITemplater templater) { if (metadata == "collapseNonEmpty" || metadata == "collapseEmpty") { var dt = value as DataTable; if (dt == null) { return(false); } var isEmpty = dt.Rows.Count == 0; //loop until all tags with the same name are processed do { var md = templater.GetMetadata(property, false); var collapseOnEmpty = md.Contains("collapseEmpty"); var collapseNonEmpty = md.Contains("collapseNonEmpty"); if (isEmpty) { if (collapseOnEmpty) { templater.Resize(property, 0); } else { templater.Replace(property, ""); } } else { if (collapseNonEmpty) { templater.Resize(property, 0); } else { templater.Replace(property, ""); } } } while (templater.Tags.Contains(property)); return(true); } return(false); }
static bool Limit10Table(string prefix, ITemplater templater, DataTable table) { if (table.Rows.Count > 10) { //simplified way to match columns against tags var tags = table.Columns.Cast<DataColumn>().Select(it => prefix + it.ColumnName).ToList(); //if any of the found tags matches limit10 condition if (tags.Any(t => templater.GetMetadata(t, true).Contains("limit10"))) { templater.Resize(tags, 10); for (int i = 0; i < 10; i++) { DataRow r = table.Rows[i]; foreach (DataColumn c in table.Columns) templater.Replace(prefix + c.ColumnName, r[c]); } return true; } } return false; }
static bool TopNElementsProcessing(string prefix, ITemplater templater, IList list) { foreach (var t in templater.Tags) { //if any of the tag metadata contain limit(X), apply limit on the provided list var limit = templater.GetMetadata(t, true).FirstOrDefault(it => it.StartsWith("limit(")); if (limit != null) { int x = int.Parse(limit.Substring(6, limit.Length - 7)); //mutate the object in place... while this is not ideal, it's a convenient way to implement such requirement //alternative is to replicate Iterable processor features which is not trivial var size = list.Count - x; while (size-- > 0) { list.RemoveAt(x); } } } //say that we did not process object, so it's passed down to next processor (built-in) //which will process it with more complex logic return(false); }
static bool Limit10Table(string prefix, ITemplater templater, DataTable table) { if (table.Rows.Count > 10) { //simplified way to match columns against tags var tags = table.Columns.Cast <DataColumn>().Select(it => prefix + it.ColumnName).ToList(); //if any of the found tags matches limit10 condition if (tags.Any(t => templater.GetMetadata(t, true).Contains("limit10"))) { templater.Resize(tags, 10); for (int i = 0; i < 10; i++) { DataRow r = table.Rows[i]; foreach (DataColumn c in table.Columns) { templater.Replace(prefix + c.ColumnName, r[c]); } } return(true); } } return(false); }
static void RemoveTagsWithMissing(ITemplater templater) { foreach (var tag in templater.Tags.ToList()) { int i = 0; string[] md; //metadata will return null when a tag does not exist at that index while ((md = templater.GetMetadata(tag, i)) != null) { var missing = md.FirstOrDefault(it => it.StartsWith("missing(")); if (missing != null) { var description = missing.Substring(8, missing.Length - 9); //Replace tag at specific index, not just the first tag templater.Replace(tag, i, description); } else { i++; } } } }
//for this example position is ignored as it is always -1 static bool CollapseNonEmpty(object value, string metadata, string tag, int position, ITemplater templater) { var dt = value as DataTable; if (dt != null && (metadata == "collapseNonEmpty" || metadata == "collapseEmpty")) { var isEmpty = dt.Rows.Count == 0; //loop until all tags with the same name are processed do { var md = templater.GetMetadata(tag, false); var collapseOnEmpty = md.Contains("collapseEmpty"); var collapseNonEmpty = md.Contains("collapseNonEmpty"); if (isEmpty) { if (collapseOnEmpty) { //when position is -1 it means non sharing tag is being used, in which case we can resize that region via "standard" API //otherwise we need to use "advanced" resize API to specify which exact tag to replace if (position == -1) { templater.Resize(new[] { tag }, 0); } else { templater.Resize(new[] { new TagPosition(tag, position) }, 0); } } else { //when position is -1 it means non sharing tag is being used, in which case we can just replace the first tag //otherwise we can replace that exact tag via position API //replacing the first tag is the same as calling replace(tag, 0, value) if (position == -1) { templater.Replace(tag, ""); } else { templater.Replace(tag, position, ""); } } } else { if (collapseNonEmpty) { if (position == -1) { templater.Resize(new[] { tag }, 0); } else { templater.Resize(new[] { new TagPosition(tag, position) }, 0); } } else { if (position == -1) { templater.Replace(tag, ""); } else { templater.Replace(tag, position, ""); } } } } while (templater.Tags.Contains(tag)); return(true); } return(false); }