public string ResolveMacrosInContext(string value, object[] eachItems = null, bool itemsOnly = false)
{
bool keepGoing;
if(string.IsNullOrEmpty(value)) {
return value;
}
do {
keepGoing = false;
var matches = _macro.Matches(value);
foreach(var m in matches) {
var match = m as Match;
var innerMacro = match.Groups[2].Value;
var outerMacro = match.Groups[1].Value;
string replacement = null;
var ndx = GetIndex(innerMacro);
if(!itemsOnly && ndx < 0) {
// get the first responder.
var indexOfDot = innerMacro.IndexOf('.');
if(indexOfDot > -1) {
var membr = innerMacro.Substring(0, indexOfDot);
var val = LookupMacroValue(membr, this);
if(val != null) {
var obval = val.SimpleEval2(innerMacro.Substring(indexOfDot + 1).Trim());
if(obval != null) {
replacement = obval.ToString();
}
}
}
else {
var mv = LookupMacroValue(innerMacro, this);
if (!mv.IsNullOrEmpty()) {
replacement = mv.CollapseToString();
}
// replacement = LookupMacroValue(innerMacro, this).CollapseToString();
}
}
if(!eachItems.IsNullOrEmpty()) {
// try resolving it as an ${each.property} style.
// the element at the front is the 'this' value
// just trim off whatever is at the front up to and including the first dot.
try {
if(ndx >= 0) {
if(ndx < eachItems.Length) {
value = value.Replace(outerMacro, eachItems[ndx].ToString());
keepGoing = true;
}
}
else {
if(innerMacro.Contains(".")) {
var indexOfDot = innerMacro.IndexOf('.');
ndx = GetIndex(innerMacro.Substring(0, indexOfDot));
if(ndx >= 0) {
if(ndx < eachItems.Length) {
innerMacro = innerMacro.Substring(indexOfDot + 1).Trim();
var v = eachItems[ndx].SimpleEval2(innerMacro);
if(v != null) {
var r = v.ToString();
value = value.Replace(outerMacro, r);
keepGoing = true;
}
}
}
}
}
}
catch {
// meh. screw em'
}
}
if(replacement != null) {
value = value.Replace(outerMacro, replacement);
keepGoing = true;
break;
}
}
} while(keepGoing);
return value.Replace("${ElementId}", "").Replace("${conditionFolder}", "");
}