Ejemplo n.º 1
0
        protected static DjangoVariable[] ParseVariables(string[] words, int wordStart, int maxVars = Int32.MaxValue)
        {
            List <DjangoVariable> variables = new List <DjangoVariable>();

            foreach (var word in words)
            {
                bool hasNewline = false;
                if (word.Contains('\r') || word.Contains('\n'))
                {
                    hasNewline = true;
                    if (word.Trim().Length == 0)
                    {
                        break;
                    }
                }
                if (!String.IsNullOrEmpty(word))
                {
                    variables.Add(DjangoVariable.Parse(word, wordStart));
                    if (variables.Count == maxVars)
                    {
                        break;
                    }
                }

                if (hasNewline)
                {
                    break;
                }

                wordStart += word.Length + 1;
            }
            return(variables.ToArray());
        }
Ejemplo n.º 2
0
 public DjangoForBlock(BlockParseInfo parseInfo, int inStart, DjangoVariable variable, int argsEnd, int reversedStart, Tuple<string, int>[] definedVars)
     : base(parseInfo) {
     InStart = inStart;
     Variable = variable;
     ArgsEnd = argsEnd;
     ReversedStart = reversedStart;
     _definedVars = definedVars;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Parses the text and returns a DjangoBlock.  Returns null if the block is empty
        /// or consists entirely of whitespace.
        /// </summary>
        public static DjangoBlock Parse(string text, bool trim = false)
        {
            int start = 0;

            if (text.StartsWith("{%"))
            {
                text = DjangoVariable.GetTrimmedFilterText(text, ref start);
                if (text == null)
                {
                    return(null);
                }
            }

            int firstChar = 0;

            for (int i = 0; i < text.Length; i++)
            {
                if (text[i] != ' ')
                {
                    firstChar = i;
                    break;
                }
            }

            int length = 0;

            for (int i = firstChar; i < text.Length && text[i] != ' '; i++, length++)
            {
                ;
            }

            if (length > 0)
            {
                string blockCmd = text.Substring(firstChar, length);
                if (Char.IsLetterOrDigit(blockCmd[0]))
                {
                    string args = text.Substring(firstChar + length, text.Length - (firstChar + length));
                    if (trim)
                    {
                        args = args.TrimEnd();
                    }

                    Func <BlockParseInfo, DjangoBlock> parser;
                    if (!_parsers.TryGetValue(blockCmd, out parser))
                    {
                        parser = DjangoUnknownBlock.Parse;
                    }

                    return(parser(new BlockParseInfo(blockCmd, args, firstChar + start)));
                }
            }

            return(null);
        }
Ejemplo n.º 4
0
        public static DjangoBlock Parse(BlockParseInfo parseInfo) {
            int start = 0;
            for (int i = 0; i < parseInfo.Args.Length && parseInfo.Args[i] == ' '; i++, start++) {
            }

            var variable = DjangoVariable.Parse(
                "var|" + parseInfo.Args.Substring(start),
                parseInfo.Start + start + parseInfo.Command.Length
            );

            return new DjangoFilterBlock(parseInfo, variable);
        }
Ejemplo n.º 5
0
        protected void ClassifyTemplateBody(ITextSnapshot snapshot, List <ClassificationSpan> spans, TemplateRegion region, int prefixLength, int suffixLength)
        {
            switch (region.Kind)
            {
            case TemplateTokenKind.Comment:
                spans.Add(
                    new ClassificationSpan(
                        new SnapshotSpan(
                            snapshot,
                            new Span(region.Start + prefixLength, region.Text.Length - (prefixLength + suffixLength))
                            ),
                        _classifierProvider._commentClassType
                        )
                    );
                break;

            case TemplateTokenKind.Variable:
                var filterInfo = DjangoVariable.Parse(region.Text);

                if (filterInfo != null)
                {
                    foreach (var curSpan in filterInfo.GetSpans())
                    {
                        spans.Add(ToClassificationSpan(curSpan, snapshot, region.Start));
                    }
                }
                break;

            case TemplateTokenKind.Block:
                var blockInfo = region.Block ?? DjangoBlock.Parse(region.Text);
                if (blockInfo != null)
                {
                    foreach (var curSpan in blockInfo.GetSpans())
                    {
                        spans.Add(ToClassificationSpan(curSpan, snapshot, region.Start));
                    }
                }
                else if (region.Text.Length > (prefixLength + suffixLength))            // unterminated block at end of file
                {
                    spans.Add(
                        new ClassificationSpan(
                            new SnapshotSpan(
                                snapshot,
                                new Span(region.Start + prefixLength, region.Text.Length - (prefixLength + suffixLength))
                                ),
                            _classifierProvider._classType
                            )
                        );
                }
                break;
            }
        }
Ejemplo n.º 6
0
 protected override void Reparse(string text)
 {
     Variable = DjangoVariable.Parse(text);
 }
Ejemplo n.º 7
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.º 8
0
 public DjangoFilterBlock(BlockParseInfo parseInfo, DjangoVariable variable)
     : base(parseInfo)
 {
     _variable = variable;
 }