private void ParseMustache(ITemplateDefinition template, string text)
        {
            var matches = _mustacheRegex.Matches(text);

            if (matches == null || matches.Count == 0)
            {
                template.AddHtml(text);
                return;
            }

            var types = new Dictionary <string, Type>(StringComparer.InvariantCultureIgnoreCase);

            var pos            = 0;
            var startOfContent = false;
            var matchIndex     = 0;

            while (!startOfContent)
            {
                if (char.IsWhiteSpace(text[pos]))
                {
                    pos++;
                }
                else if (matches.Count > matchIndex && pos == matches[matchIndex].Index)
                {
                    pos = matches[matchIndex].Index + matches[matchIndex].Length;
                    matchIndex++;
                }
                else
                {
                    startOfContent = true;
                }
            }

            foreach (Match match in matches)
            {
                if (match.Index > pos)
                {
                    template.AddHtml(text.Substring(pos, match.Index - pos));
                }

                if (pos <= match.Index)
                {
                    pos = match.Index + match.Length;
                }

                var mustache = match.Groups[1].Value.Replace(" ", "");

                if (TryParseAlias(mustache, types))
                {
                    continue;
                }
                if (TryParseStartRepeat(mustache, template, types))
                {
                    continue;
                }
                if (TryParseEndRepeat(mustache, template, types))
                {
                    continue;
                }
                if (TryParseField(mustache, template, types))
                {
                    continue;
                }
            }

            if (pos < text.Length)
            {
                template.AddHtml(text.Substring(pos));
            }
        }
Ejemplo n.º 2
0
        private void ParseResource(
            TemplateResource resource,
            ITemplateDefinition templateDefinition,
            IPackage package)
        {
            var encoding = resource.Encoding ?? Encoding.UTF8;
            var text     = encoding.GetString(resource.Content);

            var section        = "--html";
            var newSection     = true;
            var blankLineCount = 0;

            var lines = text
                        .Replace("\r", string.Empty)
                        .Split('\n')
                        .Select(s => s.Trim());

            foreach (var line in lines)
            {
                if (line.StartsWith("--"))
                {
                    section        = line.ToLower().Trim();
                    newSection     = true;
                    blankLineCount = 0;
                    continue;
                }

                if (line.Length == 0)
                {
                    if (!newSection)
                    {
                        blankLineCount++;
                    }
                    continue;
                }


                if (section.StartsWith("--javascript"))
                {
                    for (var i = 0; i < blankLineCount; i++)
                    {
                        templateDefinition.AddScriptLine(string.Empty);
                    }

                    templateDefinition.AddScriptLine(line);
                }
                else if (section.StartsWith("--css"))
                {
                    for (var i = 0; i < blankLineCount; i++)
                    {
                        templateDefinition.AddStyleLine(string.Empty);
                    }

                    templateDefinition.AddStyleLine(line);
                }
                else if (section.StartsWith("--head"))
                {
                    for (var i = 0; i < blankLineCount; i++)
                    {
                        templateDefinition.AddHeadLine(string.Empty);
                    }

                    templateDefinition.AddHeadLine(line);
                }
                else if (section.StartsWith("--init"))
                {
                    for (var i = 0; i < blankLineCount; i++)
                    {
                        templateDefinition.AddInitializationLine(string.Empty);
                    }

                    templateDefinition.AddInitializationLine(line);
                }
                else if (section.StartsWith("--html"))
                {
                    for (var i = 0; i < blankLineCount; i++)
                    {
                        templateDefinition.AddLineBreak();
                    }

                    templateDefinition.AddHtml(line);
                    templateDefinition.AddLineBreak();
                }

                newSection     = false;
                blankLineCount = 0;
            }
        }