public string Convert(string templateName, ViewData data) { string template = templateName; if (template.StartsWith(templatePath)) template = template.Substring(templatePath.Length + 1); SparkViewDescriptor descriptor = new SparkViewDescriptor() .AddTemplate(template); var view = (SparkTemplateBase)engine.CreateInstance(descriptor); using (var writer = new StringWriter()) { try { view.ViewData = data; view.RenderView(writer); } finally { engine.ReleaseInstance(view); } return writer.ToString(); } }
private void AddMatch(string outputPath, string templatePath, ViewData data, Namespace ns, DeclaredType type) { var clone = data.Clone(); clone.Type = type; AddMatch(outputPath, templatePath, clone, ns); }
private void AddMatch(string outputPath, string templatePath, ViewData data, Namespace ns) { var clone = data.Clone(); clone.Namespace = ns; AddMatch(outputPath, templatePath, clone); }
private void ResolveDirectory(string head, string templatePath, IEnumerable<Namespace> namespaces, ViewData data, IList<string> tail) { if (!IsSpecial(head)) ResolveRecursive(tail[0], tail.ToTail(), templatePath, templatePath, namespaces, data.Clone()); else if (head == "!namespace") ResolveNamespaceDirectory(head, tail, templatePath, namespaces, data); else if (head == "!type") ResolveTypeDirectory(head, tail, templatePath, namespaces, data); }
private void AddMatch(string outputPath, string templatePath, ViewData data) { var path = outputPath.Replace(".spark", ".htm"); var clone = data.Clone(); if (clone.Types.Count == 0) clone.Types = (from n in data.Namespaces from t in n.Types select t).ToList(); matches.Add(new TemplateMatch(path, templatePath, clone)); }
public ViewData Clone() { var clone = new ViewData(); clone.Assemblies = new List<Assembly>(Assemblies); clone.Namespaces = new List<Namespace>(Namespaces); clone.Types = new List<DeclaredType>(Types); clone.Namespace = Namespace; clone.Type = Type; return clone; }
private IList<TemplateMatch> ResolveRecursive(string current, string outputPath, string templatePath, IEnumerable<Namespace> namespaces, ViewData data) { if (Path.GetExtension(current) == ".spark") { if (current == "!namespace.spark") { foreach (var ns in namespaces) { AddMatch(outputPath.Replace("!namespace", ns.Name), templatePath, data, ns); } } else if (current == "!type.spark") { var foundTypes = from n in namespaces from t in n.Types select new { Type = t, Namespace = n }; foreach (var found in foundTypes) { string name = HACK_inNamespace ? found.Type.Name : found.Namespace.Name + "." + found.Type.Name; AddMatch(outputPath.Replace("!type", name), templatePath, data, found.Namespace, found.Type); } } else AddMatch(outputPath, templatePath, data); } else { if (!IsSpecial(current)) { string[] parts = templatePath.Substring(templatePath.IndexOf(current) + current.Length).Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries); ResolveRecursive(parts[0], templatePath, templatePath, namespaces, data.Clone()); } else if (current == "!namespace") { string[] parts = templatePath.Substring(templatePath.IndexOf(current) + current.Length).Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries); HACK_inNamespace = false; foreach (var ns in namespaces) { string nsPath = templatePath.Replace(current, ns.Name); HACK_inNamespace = true; var clone = data.Clone(); clone.Namespace = ns; ResolveRecursive(parts[0], nsPath, templatePath, new List<Namespace> { ns }, clone); } HACK_inNamespace = false; } else if (current == "!type") { string[] parts = templatePath.Replace(current, "").Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries); foreach (var found in from n in namespaces from t in n.Types select new { Type = t, Namespace = n }) { string typePath = templatePath.Replace(current, found.Namespace.Name + "." + found.Type.Name); var clone = data.Clone(); clone.Namespace = found.Namespace; clone.Type = found.Type; ResolveRecursive(parts[0], typePath, templatePath, new List<Namespace> { found.Namespace }, clone); } } } return matches; }
private string Convert(ViewData data) { var callStack = new StackTrace(); var caller = callStack.GetFrame(1); return generator.Convert(caller.GetMethod().Name, data); }
public GeneratorTestChain WithData(ViewData data) { viewData = data; return this; }
private void ResolveNamespaceDirectory(string head, IList<string> tail, string templatePath, IEnumerable<Namespace> namespaces, ViewData data) { HACK_inNamespace = false; foreach (var ns in namespaces) { string nsPath = templatePath.Replace(head, ns.Name); HACK_inNamespace = true; var clone = data.Clone(); clone.Namespace = ns; ResolveRecursive(tail[0], tail.ToTail(), nsPath, templatePath, new List<Namespace> { ns }, clone); } HACK_inNamespace = false; }
private void ResolveTypeTemplate(string outputPath, string templatePath, IEnumerable<Namespace> namespaces, ViewData data) { var foundTypes = from n in namespaces from t in n.Types select new { Type = t, Namespace = n }; foreach (var found in foundTypes) { string name = HACK_inNamespace ? found.Type.Name : found.Namespace.Name + "." + found.Type.Name; AddMatch(outputPath.Replace("!type", name), templatePath, data, found.Namespace, found.Type); } }
private void ResolveTypeDirectory(string head, IList<string> tail, string templatePath, IEnumerable<Namespace> namespaces, ViewData data) { foreach (var found in from n in namespaces from t in n.Types select new { Type = t, Namespace = n }) { string typePath = templatePath.Replace(head, found.Namespace.Name + "." + found.Type.Name); var clone = data.Clone(); clone.Namespace = found.Namespace; clone.Type = found.Type; ResolveRecursive(tail[0], tail.ToTail(), typePath, templatePath, new List<Namespace> { found.Namespace }, clone); } }
private void ResolveTemplate(string head, string outputPath, string templatePath, IEnumerable<Namespace> namespaces, ViewData data) { if (head.StartsWith("!namespace")) ResolveNamespaceTemplate(outputPath, templatePath, namespaces, data); else if (head.StartsWith("!type")) ResolveTypeTemplate(outputPath, templatePath, namespaces, data); else AddMatch(outputPath, templatePath, data); }
private IList<TemplateMatch> ResolveRecursive(string head, IList<string> tail, string outputPath, string templatePath, IEnumerable<Namespace> namespaces, ViewData data) { if (Path.GetExtension(head) == ".spark") ResolveTemplate(head, outputPath, templatePath, namespaces, data); else ResolveDirectory(head, templatePath, namespaces, data, tail); return matches; }
private void ResolveNamespaceTemplate(string outputPath, string templatePath, IEnumerable<Namespace> namespaces, ViewData data) { foreach (var ns in namespaces) { AddMatch(outputPath.Replace("!namespace", ns.Name), templatePath, data, ns); } }