コード例 #1
0
 public void generate_hxl_runtime_template_source()
 {
     // Reads in an HTML file, generates a source code
     // file that will generate the output (T4 style template)
     HxlTemplate template = HxlTemplate.FromStream(Stream.Null);
     string      source   = template.GenerateSource();
 }
コード例 #2
0
        public void load_hxl_template_from_assembly()
        {
            // uses factory, HxlTemplateUsage
            HxlTemplate template = HxlTemplate.FromName(typeof(object).GetTypeInfo().Assembly, "MyTemplate");
            IEnumerable <KeyValuePair <string, object> > variables =
                Properties.FromValue(new { greeting = "Hello, World" });

            template.Transform("C:/output.html", variables);
        }
コード例 #3
0
        public void save_hxl_template_results_to_stream()
        {
            HxlTemplate template = HxlTemplate.Parse("<html><body>$greeting</body></html>");
            IEnumerable <KeyValuePair <string, object> > variables =
                Properties.FromValue(new { greeting = "Hello, World" });

            FileStream fs = new FileStream("/output.html", FileMode.Create);

            template.Transform(fs, variables);
        }
コード例 #4
0
        string GenerateSourceTempFile(HxlCompilerSession session, HxlTemplate template)
        {
            string name, type;

            HxlTemplateAttribute.NameOrDefault(template, out name, out type);
            string tempFile = CodeUtility.Slug(name) + "-" + ((ParsedTemplate)template).Signature + ".cs";

            using (var sw = session.CreateText(tempFile)) {
                HxlCompiler.GenerateOneSourceFile(session, template, sw);
            }
            return(session.GetFileName(tempFile));
        }
コード例 #5
0
        protected void Load(string fixture)
        {
            var fixtureFileName = string.Format("Hxl/{0}.fixture", fixture);
            var myFixture       = TestContext.LoadFixture(fixtureFileName).Items[0];

            errors.Clear();

            this.FixtureName = GetType().Name + "-" + fixture;
            this.Source      = myFixture["input.hxl"];

            this.InputHtml = ParseHtml(Source);
            HxlCompilerSettings settings = new HxlCompilerSettings();

            settings.Namespaces.AddNew("test", new Uri("http://example.com/"));

            HxlCompiler c    = HxlCompiler.Create(settings);
            HxlTemplate temp = c.ParseTemplate(this.Source);

            // UNDONE Currently, f-spec is using the stream context API from shared instead of f-core,
            // so we have to copy the data over and hack the generated template names to support the
            // proper fixture data names.
            var templates = myFixture.Where(t => t.Key.EndsWith(".hxl", StringComparison.Ordinal))
                            .Select(t => {
                var tpl          = (ParsedTemplate)c.ParseTemplate(myFixture.GetStreamContext(t.Key).ReadAllText());
                string name      = WorkaroundTemplateName(fixtureFileName, t.Key);
                tpl.TemplateName = name;
                tpl.ClassName    = name;
                return(tpl);
            });
            var results = c.Compile(templates);

            this.Data = new Properties();
            if (myFixture["data.properties"] != null)
            {
                var props = Properties.FromStream(myFixture.GetStreamContext("data.properties").OpenRead());

                // So that we get some iterable content:
                // Use array syntax [a,b,c]
                foreach (var kvp in props)
                {
                    string parsedValue = Convert.ToString(kvp.Value);

                    if (parsedValue.Length > 0 && parsedValue[0] == '[')
                    {
                        var array = parsedValue.Substring(1, parsedValue.Length - 2).Split(',');
                        this.Data.SetProperty(kvp.Key, array);
                    }
                    else
                    {
                        this.Data.SetProperty(kvp.Key, parsedValue);
                    }
                }
            }

            var es = myFixture["output.cs"];

            if (es == null)
            {
                _expectedSource = null;
            }
            else
            {
                var    reader = new StringReader(es);
                var    lines  = new List <string>();
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    lines.Add(line);
                }
                _expectedSource = lines;
            }

            GeneratedSource       = c.GenerateSource(temp);
            OtherGeneratedSources = templates.Except(temp).ToDictionary(t => ((IHxlTemplateBuilder)t).TemplateName, t => c.GenerateSource(t));

            var allErrors = results.Errors.Where(t => !t.IsWarning);

            if (allErrors.Any())
            {
                WriteCompilerOutput(string.Empty, string.Empty);
                Assert.Fail("One or more compiler errors: {0}", allErrors.First());
            }

            Assembly = System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyPath(
                results.PathToAssembly
                );

            ExpectedHtml   = myFixture["generated.html"];
            CompilerErrors = results.Errors;
        }
コード例 #6
0
 public void load_hxl_template_from_uri()
 {
     HxlTemplate template = HxlTemplate.FromSource(new Uri("http://example.com/index.xl.html"));
     string      output   = template.TransformText();
 }
コード例 #7
0
 public void render_hxl_template_to_file()
 {
     // Reads in an HTML file, executes the HXL commands
     // within to generate HTML file
     HxlTemplate.Render("C:/input.html", "C:/output.html");
 }
コード例 #8
0
 public void render_hxl_template_to_text()
 {
     // Reads in an HTML file, executes the HXL commands
     // within to generate HTML text
     string text = HxlTemplate.RenderText("C:/myfile.html");
 }