Esempio n. 1
0
        public static string RenderTemplate(string template, ViewDataDictionary viewData)
        {
            // Set up your spark engine goodness.
            var settings = new SparkSettings().SetPageBaseType(typeof(SparkView));

            var engine = new SparkViewEngine(settings)
                         {
                             ViewFolder = new FileSystemViewFolder(AppDomain.CurrentDomain.BaseDirectory + @"\..\..\..\IntegrationTestingViews\Views")
                         };

            // "Describe" the view (the template, it is a template after all), and its details.
            var descriptor = new SparkViewDescriptor().AddTemplate(template);

            // Create a spark view engine instance
            var view = (SparkView)engine.CreateInstance(descriptor);
            try
            {
                // Merge the view data.
                viewData.Keys.ToList().ForEach(x => view.ViewData[x] = viewData[x]);

                // Render the view to a text writer.
                var writer = new StringWriter();
                view.RenderView(writer);
                return writer.ToString();
            }
            finally
            {
                engine.ReleaseInstance(view);
            }
        }
Esempio n. 2
0
        public IBuilder Load(Options options)
        {
            var currentDirectory = Environment.CurrentDirectory;
            var assemblyDirectory = Path.GetDirectoryName(typeof(SakeEngine).Assembly.Location);

            var settings = new SparkSettings()
                .SetPageBaseType(typeof(BuilderBase))
                .SetAutomaticEncoding(true)
                .SetAttributeBehaviour(AttributeBehaviour.TextOriented)
                .SetDebug(true);

            IViewFolder viewFolder = new FileSystemViewFolder(currentDirectory);
            foreach(var includeDir in options.IncludeDirectory)
            {
                viewFolder = new CombinedViewFolder(viewFolder, new FileSystemViewFolder(Path.Combine(currentDirectory, includeDir)));
            }
            viewFolder = new CombinedViewFolder(viewFolder, new FileSystemViewFolder(assemblyDirectory));

            var engine = new SparkViewEngine(settings)
                               {
                                   ViewFolder = viewFolder,
                                   ExtensionFactory = new ExtensionFactory(),
                               };

            var descriptor = new SparkViewDescriptor
            {
                Templates = new[] { options.Makefile }
            };

            var builder = (BuilderBase)engine.CreateInstance(descriptor);
            builder.Output = new StringWriter();
            builder.Log = _log;
            builder.Render();
            return builder;
        }
        public string RenderTemplate(string templateSource, object data)
        {
            var settings = new SparkSettings
                           	{
                           		PageBaseType = typeof(TestSparkView).Name
                           	};
            settings.AddViewFolder(typeof (TestingViewFolder), new Dictionary<string, string>
                                                               	{
                                                               		{"templateSource", templateSource}
                                                               	});

            settings.AddNamespace("OpenRasta.Codecs.Spark.Tests.TestObjects");
            settings.AddNamespace("OpenRasta.Codecs.Spark.IntegrationTests");
            settings.AddNamespace("OpenRasta.Codecs.Spark2.ViewHelpers");
            settings.AddNamespace("System.Collections.Generic");

            IWindsorContainer dependencies = CreateTestDependencies();
            ISparkViewEngine sparkViewEngine = new SparkViewEngine(settings)
                                               	{
                                               		ExtensionFactory =dependencies.Resolve<ISparkExtensionFactory>()
                                               	};

            var descriptor = new SparkViewDescriptor();
            descriptor.AddTemplate(TestingViewFolder.SingleTemplateName);
            var view = (TestSparkView)sparkViewEngine.CreateInstance(descriptor);
            view.Uris = new UriGenerator(DependencyManager.GetService<IUriResolver>());
            view.ViewData = new TestViewData(data);
            return Render(view);
        }
Esempio n. 4
0
        public void Render(dynamic model, RunnerOptions runnerOptions)
        {
            _logger.Debug(string.Format("Rendering {0}", model.FileName));

            var viewEngine = new SparkViewEngine
                                 {
                                     DefaultPageBaseType = typeof (SparkView).FullName,
                                     ViewFolder = new FileSystemViewFolder(Path.Combine(runnerOptions.WorkingDirectory, "_templates"))
                                 };

            var view = (SparkView) viewEngine.CreateInstance(new SparkViewDescriptor().AddTemplate("post.spark"));
            view.Model = model;

            using(var writer = new StreamWriter(Console.OpenStandardOutput(), Encoding.UTF8))
            {
                view.RenderView(writer);
            }
        }
Esempio n. 5
0
        private static void Stub(FileSystemInfo source, FileSystemInfo destination)
        {
            if (source.FullName.EndsWith(".spark"))
            {
                // At this time the destination is already copied and processed by
                // 'simple' text substitution, so destination should be processed.
                var settings = new SparkSettings();
                settings.AddNamespace("System");

                var engine = new SparkViewEngine(settings) {DefaultPageBaseType = typeof(Template).FullName};
                var folder = new InMemoryViewFolder {{source.Name, File.ReadAllText(destination.FullName)}};
                engine.ViewFolder = folder;

                var descriptor = new SparkViewDescriptor();
                descriptor.AddTemplate(source.Name);

                var view = (Template) engine.CreateInstance(descriptor);
                var builder = new StringBuilder();
                using (var output = new StringWriter(builder))
                {
                    view.RenderView(output);
                    File.WriteAllText(destination.FullName, output.ToString());
                }

                engine.ReleaseInstance(view);
            }
            else if (source.FullName.EndsWith(".tt"))
            {
                var generator = new TemplateGenerator();
                if (!generator.ProcessTemplate(source.FullName, destination.FullName))
                {
                    var exception = new TemplateException(source.FullName);
                    foreach (CompilerError error in generator.Errors)
                    {
                        exception.AddError(error.ErrorText);
                    }
                    throw exception;
                }
            }
        }
Esempio n. 6
0
        public Stream Process(string virtualPath, string rootPath, Stream source, Dictionary<string, string> settings)
        {
            IViewFolder currentFolder =
                new Spark.FileSystem.CombinedViewFolder(
                    new StringViewFolderFile(virtualPath, source),
                    new Spark.FileSystem.FileSystemViewFolder(rootPath)
                );

            /*
            foreach(var dir in Directory.GetDirectories(rootPath, "*", SearchOption.AllDirectories))
            {
                currentFolder = new Spark.FileSystem.CombinedViewFolder(
                    currentFolder,
                    new Spark.FileSystem.FileSystemViewFolder(dir)
                    );
            }
            */

            SparkViewEngine _engine = new SparkViewEngine(new Spark.SparkSettings())
            {
                DefaultPageBaseType = typeof(ViewTemplate).FullName,
                ViewFolder = currentFolder,
                TemplateLocator = new MasterFileLocator(virtualPath, rootPath),
            };

            var descriptor = new SparkViewDescriptor()
                .AddTemplate(virtualPath.TrimStart('\\'));

            var view = (ViewTemplate)_engine.CreateInstance(descriptor);

            view.RootPath = rootPath;
            view.VirtualPath = virtualPath;
            source.Dispose();
            var tw = new StringWriter();
            view.RenderView(tw);
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(tw.ToString().Replace("@/", view.AppRelativeFolder)));

            return ms;
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.Write(@"
            Transforms Xml using a Spark template

            XPARK templatefile [inputfile [outputfile]]

              templatefile  Path to a .spark file.
              inputfile     Source xml. Path to an file or url for http GET.
              outputfile    Target file to receive template output.

            If inputfile or outputfile are not provided stdin and stdout are used.

            The Model in the template is an XDocument loaded from the source.

            The templatefile location may also contain _partial.spark files, and
            a _global.spark file with common namespaces, macros, etc.
            ");
                return;
            }

            // Find the full path to the template file,
            // using current directory if argument isn't fully qualified
            var templatePath = Path.Combine(Environment.CurrentDirectory, args[0]);
            var templateName = Path.GetFileName(templatePath);
            var templateDirPath = Path.GetDirectoryName(templatePath);

            var viewFolder = new FileSystemViewFolder(templateDirPath);

            // Create an engine using the templates path as the root location
            // as well as the shared location
            var engine = new SparkViewEngine
                             {
                                 DefaultPageBaseType = typeof(SparkView).FullName,
                                 ViewFolder = viewFolder.Append(new SubViewFolder(viewFolder, "Shared"))
                             };

            SparkView view;
            // compile and instantiate the template
            view = (SparkView)engine.CreateInstance(
                                  new SparkViewDescriptor()
                                      .AddTemplate(templateName));

            // load the second argument, or default to reading stdin
            if (args.Length >= 2)
                view.Model = XDocument.Load(args[1]);
            else
                view.Model = XDocument.Load(XmlReader.Create(Console.OpenStandardInput()));

            // write out to the third argument, or default to writing stdout
            if (args.Length >= 3)
            {
                using (var writer = new StreamWriter(new FileStream(args[2], FileMode.Create), Encoding.UTF8))
                {
                    view.RenderView(writer);
                }
            }
            else
            {
                using (var writer = new StreamWriter(Console.OpenStandardOutput(), Encoding.UTF8))
                {
                    view.RenderView(writer);
                }
            }
        }
Esempio n. 8
0
        static string ProcessViewTemplate(SparkViewEngine engine, string templateName, List<Metadata> model, Dictionary<string, List<Metadata>> byAssembly)
        {
            var view = (IndexView)engine.CreateInstance(
                new SparkViewDescriptor()
                    .AddTemplate(templateName));

            view.Model = new ComplexView()
                             {
                                 ByAssembly = byAssembly,
                                 ByMessage = model
                             };

            var sb = new StringBuilder();
            using (var writer = new StringWriter(sb))
            {
                view.RenderView(writer);
            }

            return sb.ToString();
        }
Esempio n. 9
0
        static string ProcessViewTemplate(SparkViewEngine engine, string templateName, Metadata model)
        {
            var view = (MessageView)engine.CreateInstance(
                new SparkViewDescriptor()
                    .AddTemplate(templateName));

            view.Model = model;

            var sb = new StringBuilder();
            using (var writer = new StringWriter(sb))
            {
                view.RenderView(writer);
            }

            return sb.ToString();
        }