private TemplateOccurence CompileOccurence(TemplateOccurence parent, Match m) { var tplContent = m.Groups[5].Value; var tplAttributes = m.Groups[4].Value; var tplName = m.Groups[3].Value; var tpl = new TemplateOccurence { Name = tplName, TplText = tplContent, Index = m.Index, Path = (parent != null ? parent.Path + "/" : "") + tplName, Length = m.Length }; tpl.Occurences = Compile(tpl, tplContent); var regAttr = new Regex("([\\w_]*)=\"([^\"]*)\""); foreach (var a in from Match match in regAttr.Matches(tplAttributes) select new TemplateAttribute { Name = match.Groups[1].Value.ToLower(), Value = match.Groups[2].Value.ToLower() }) { tpl.Attributes.Add(a.Name, a); } return(tpl); }
public TemplateOccurence Select(string pattern) { if (string.IsNullOrEmpty(pattern)) { return(Occurences.FirstOrDefault()); } var occurences = Occurences; var path = pattern.Split(new[] { '/' }); TemplateOccurence occ = null; foreach (var t in path) { var t1 = t; occ = occurences.FirstOrDefault(o => o.Name == t1); if (occ == null) { return(null); } occurences = occ.Occurences.ToList(); } return(occ); }
private IEnumerable <TemplateOccurence> Compile(TemplateOccurence parent, string content) { var occurences = new List <TemplateOccurence>(); var tpls = Regex.Matches(content, RegularExpressionTemplate); foreach (Match m in tpls) { var tplType = m.Groups[2].Value; if (tplType != "for") { continue; } var o = CompileOccurence(parent, m); var i = 0; var fragment = ""; foreach (var to in o.Occurences) { fragment += o.TplText.Substring(i, (to.Index) - i); i = to.Index + to.Length; } if (i < o.TplText.Length) { fragment += o.TplText.Substring(i, o.TplText.Length - i); } o.Fragment = fragment; var matches = Regex.Matches(fragment, RegField); var exprs = (from Match mexpr in matches select new TemplateExpression { Name = mexpr.Groups[0].Value }).ToList(); o.Expressions = exprs; occurences.Add(o); } return(occurences); }