Ejemplo n.º 1
0
        public static DjangoForBlock Parse(BlockParseInfo parseInfo)
        {
            var words   = parseInfo.Args.Split(' ');
            int inStart = -1;

            int inOffset = 0, inIndex = -1;
            var definitions = new List <Tuple <string, int> >();

            for (int i = 0; i < words.Length; i++)
            {
                var word = words[i];
                if (word == "in")
                {
                    inStart = inOffset + parseInfo.Start + parseInfo.Command.Length;
                    inIndex = i;
                    break;
                }
                else if (words[i].IndexOfAny(NewLines) != -1)
                {
                    // unterminated tag
                    break;
                }

                if (!String.IsNullOrEmpty(word))
                {
                    definitions.Add(new Tuple <string, int>(word, inOffset + parseInfo.Start + parseInfo.Command.Length));
                }
                inOffset += words[i].Length + 1;
            }

            // parse the arguments...
            int            reversedStart = -1;
            DjangoVariable variable      = null;
            int            argsEnd       = -1;

            if (inIndex != -1)
            {
                string filterText = "";
                argsEnd = inStart + "in".Length + 1;
                for (int i = inIndex + 1; i < words.Length; i++)
                {
                    int    nlStart = words[i].IndexOfAny(NewLines);
                    string trimmed = words[i];
                    if (nlStart != -1)
                    {
                        trimmed = words[i].Substring(0, nlStart);
                    }

                    if (i != inIndex + 1 && trimmed == words[i])   // if we trimmed we don't have an extra space
                    {
                        filterText += " ";
                        argsEnd    += 1;
                    }

                    if (trimmed == "reversed")
                    {
                        reversedStart = argsEnd;
                        break;
                    }

                    filterText += trimmed;
                    argsEnd    += trimmed.Length;
                    if (trimmed != words[i])
                    {
                        // unterminated tag
                        break;
                    }
                }

                var trimmedFilter = filterText.TrimStart(' ');

                variable = DjangoVariable.Parse(trimmedFilter,
                                                inStart + "in".Length + 1 + filterText.Length - trimmedFilter.Length);
            }

            return(new DjangoForBlock(parseInfo, inStart, variable, argsEnd, reversedStart, definitions.ToArray()));
        }
Ejemplo n.º 2
0
 protected override void Reparse(string text)
 {
     Variable = DjangoVariable.Parse(text);
 }