Beispiel #1
0
        public HtmlFile(string content, StringBuilder globalScripts)
        {
            var s = new StringScanner(content, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

            this.Directives = this.ReadDirectives(s).ToDictionary(x => x.Key, x => x.Value, StringComparer.OrdinalIgnoreCase);

            var scoped   = false;
            var scopedId = "s-" + Guid.NewGuid().ToString("d").Substring(0, 6);

            while (!s.HasTerminated)
            {
                if (s.Match(@"<style\s+scoped>([\s\S]*?)</style>"))
                {
                    if (scoped)
                    {
                        this.Template.Append("<h1>Only 1 style scoped supported<h1>");
                    }
                    else
                    {
                        scoped = true;

                        var style = s.Scan(@"<style\s+scoped>([\s\S]*?)</style>", 1);

                        this.Styles.Append($"[{scopedId}] {{ {style} }}");
                    }
                }
                else if (s.Match(@"<style>([\s\S]*?)</style>"))
                {
                    this.Styles.Append(s.Scan(@"<style>([\s\S]*?)</style>", 1));
                }
                else if (s.Match(@"<script>([\s\S]*?)</script>"))
                {
                    this.Scripts.Append(s.Scan(@"<script>([\s\S]*?)</script>", 1));
                }
                else if (s.Match(@"<script\s+mixin>([\s\S]*?)</script>"))
                {
                    this.Mixins.Add(s.Scan(@"<script\s+mixin>([\s\S]*?)</script>", 1));
                }
                else if (s.Match(@"<script\s+global>([\s\S]*?)</script>"))
                {
                    globalScripts.Append(s.Scan(@"<script\s+global>([\s\S]*?)</script>", 1));
                }
                else if (s.Match(@"<template>([\s\S]*?)</template>"))
                {
                    this.Template.Append(s.Scan(@"<template>([\s\S]*?)</template>", 1));
                }
                else
                {
                    this.Template.Append(s.Read(1));
                }
            }

            // change template if scoped was used
            if (scoped)
            {
                var htm = Regex.Replace(this.Template.ToString(), @"^\s*(<[\w-]+)", $"$1 {scopedId}");

                this.Template.Clear();

                this.Template.Append(htm);
            }
        }
Beispiel #2
0
        public HtmlFile(Stream stream)
        {
            var content = "";

            using (stream)
                using (var reader = new StreamReader(stream))
                {
                    content = reader.ReadToEnd();
                }

            var s = new StringScanner(content, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

            while (!s.HasTerminated)
            {
                var directive = this.ReadDirective(s);

                if (directive.Key == null)
                {
                    break;
                }

                switch (directive.Key.ToLower())
                {
                case "name":
                    this.Name = directive.Value;
                    break;

                case "viewmodel":
                    this.ViewModel = directive.Value;
                    break;

                case "inheritattrs":
                    this.InheritAttrs = Convert.ToBoolean(directive.Value);
                    break;

                case "auth":
                    this.IsAutenticated = true;
                    break;

                case "role":
                    this.Roles.Add(directive.Value);
                    break;
                }
            }

            while (!s.HasTerminated)
            {
                var tag = this.ReadTag(s);

                if (tag == null)
                {
                    break;
                }

                switch (tag.TagName)
                {
                case "template":
                    this.Template = tag;
                    break;

                case "style":
                    this.Styles.Add(tag);
                    break;

                case "link":
                    if (tag.Attributes.TryGetValue("href", out var href))
                    {
                        this.Includes.Add(href);
                    }
                    break;

                case "script":
                    if (tag.Attributes.TryGetValue("src", out var src))
                    {
                        this.Includes.Add(src);
                    }
                    else
                    {
                        this.ClientScripts.Add(tag);
                    }
                    break;
                }
            }
        }