Ejemplo n.º 1
0
        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();
            }
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        private void AddMatch(string outputPath, string templatePath, ViewData data, Namespace ns)
        {
            var clone = data.Clone();

            clone.Namespace = ns;

            AddMatch(outputPath, templatePath, clone);
        }
Ejemplo n.º 4
0
 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);
 }
Ejemplo n.º 5
0
        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));
        }
Ejemplo n.º 6
0
        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;
        }
Ejemplo n.º 7
0
        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;
        }
Ejemplo n.º 8
0
        private string Convert(ViewData data)
        {
            var callStack = new StackTrace();
            var caller = callStack.GetFrame(1);

            return generator.Convert(caller.GetMethod().Name, data);
        }
Ejemplo n.º 9
0
 public GeneratorTestChain WithData(ViewData data)
 {
     viewData = data;
     return this;
 }
Ejemplo n.º 10
0
        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;
        }
Ejemplo n.º 11
0
        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);
            }
        }
Ejemplo n.º 12
0
        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);
            }
        }
Ejemplo n.º 13
0
 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);
 }
Ejemplo n.º 14
0
        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;
        }
Ejemplo n.º 15
0
 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);
     }
 }