Esempio n. 1
0
        public static List <PageFragment> ParseTemplatePage(StringSegment text)
        {
            var to = new List <PageFragment>();

            if (text.IsNullOrWhiteSpace())
            {
                return(to);
            }

            int pos;
            var lastPos = 0;

            while ((pos = text.IndexOf("{{", lastPos)) != -1)
            {
                var block = text.Subsegment(lastPos, pos - lastPos);
                if (!block.IsNullOrEmpty())
                {
                    to.Add(new PageStringFragment(block));
                }

                var varStartPos = pos + 2;
                var varEndPos   = text.IndexOfNextCharNotInObjects(varStartPos, '|', '}');
                var initialExpr = text.Subsegment(varStartPos, varEndPos - varStartPos).Trim();
                if (varEndPos == -1 || varEndPos >= text.Length)
                {
                    throw new ArgumentException($"Invalid Server HTML Template at '{text.SubstringWithElipsis(0, 50)}'", nameof(text));
                }

                List <JsExpression> filterCommands = null;

                var isFilter = text.GetChar(varEndPos) == '|';
                if (isFilter)
                {
                    bool foundVarEnd = false;

                    filterCommands = text.Subsegment(varEndPos + 1).ParseExpression <JsExpression>(
                        separator: '|',
                        atEndIndex: (str, strPos) =>
                    {
                        while (str.Length > strPos && str.GetChar(strPos).IsWhiteSpace())
                        {
                            strPos++;
                        }

                        if (str.Length > strPos + 1 && str.GetChar(strPos) == '}' && str.GetChar(strPos + 1) == '}')
                        {
                            foundVarEnd = true;
                            varEndPos   = varEndPos + 1 + strPos + 1;
                            return(strPos);
                        }
                        return(null);
                    },
                        allowWhitespaceSensitiveSyntax: true);

                    if (!foundVarEnd)
                    {
                        throw new ArgumentException($"Invalid syntax near '{text.Subsegment(pos).SubstringWithElipsis(0, 50)}'");
                    }
                }
                else
                {
                    varEndPos += 1;
                }

                lastPos = varEndPos + 1;
                var originalText = text.Subsegment(pos, lastPos - pos);

                to.Add(new PageVariableFragment(originalText, initialExpr, filterCommands));
            }

            if (lastPos != text.Length)
            {
                var lastBlock = lastPos == 0 ? text : text.Subsegment(lastPos);
                to.Add(new PageStringFragment(lastBlock));
            }

            return(to);
        }