void IDocumentParser.Execute(RazorParser parser)
        {
            _parser = parser;
            var prevIndex = _parser.charIndex - 1;
            var prevChar  = _parser.GetCharacterAtIndex(prevIndex);

            if (prevChar == RazorParser.QuoteChr || prevChar == RazorParser.SingleQuoteChr)
            {
                _parser.WriteHtml(RazorParser.TagStartChr);
                return;
            }
            var textIndex = _parser.FindNextChars(_parser.charIndex, RazorParser.TextTag);

            _tcounter = 1;
            if (textIndex > -1)
            {
                _parser.charIndex = textIndex;
                _textMode         = true;
                _parser.StartViewResponse();
            }
            else
            {
                _parser.charIndex++;
                if (NeedViewResponse())
                {
                    _parser.StartViewResponse();
                }
                _buffer.Append(RazorParser.TagStartChr);
            }
            _parser.ProcessBlock(Process);
        }
Ejemplo n.º 2
0
        private void Process()
        {
            _razorIndex      = _parser.charIndex;
            _charBeforeRazor = _parser.GetCharacterAtIndex(_razorIndex - 1);
            _razorCounter    = 1;
            var nextChar = _parser.NextChar();

            while (nextChar == RazorParser.RazorChr)
            {
                _razorCounter++;
                _razorIndex = _parser.charIndex++;
                nextChar    = _parser.NextChar();
            }

            if (_razorCounter > 1)
            {
                var razorDisplayCount = (int)Math.Floor(_razorCounter / 2.0);
                _parser.WriteHtml(String.Join(string.Empty, Enumerable.Range(0, razorDisplayCount).Select(x => RazorParser.RazorChr)));
                if (_razorCounter % 2 == 0)
                {
                    return;
                }
                else
                {
                    _charBeforeRazor = _parser.GetCharacterAtIndex(_razorIndex - 1);
                }
            }

            if (_parser.IsTerminateChar(nextChar))
            {
                throw new InvalidOperationException("Invalid operation detected. Razor syntax is incomplete");
            }
            if (nextChar == RazorParser.ExplicitBlockStart)
            {
                _blockType         = BlockType.ExplicitExpression;
                _parser.charIndex += 2;
                _ecounter          = 1;
            }
            else if (nextChar == RazorParser.TextChr)
            {
                _blockType         = BlockType.Text;
                _parser.charIndex += 2;
            }
            else if (nextChar == RazorParser.CommentChar)
            {
                _parser.charIndex += 2;
                _blockType         = BlockType.Comment;
                if (_root != null)
                {
                    _parser.WritePlainCode("/*");
                }
                else
                {
                    _parser.WritePlainHtml("<!--");
                }
            }
            else if (nextChar == RazorParser.BlockStartBrace)
            {
                _blockType         = BlockType.Code;
                _parser.charIndex += 2;
                _bcounter          = 1;
            }
            else
            {
                _parser.charIndex++;
                var index               = _parser.charIndex;
                int operandIfIndex      = _parser.FindNextChars(index, "if("),
                    operandWithIndex    = _parser.FindNextChars(index, "with("),
                    operandForIndex     = _parser.FindNextChars(index, "for("),
                    operandWhileIndex   = _parser.FindNextChars(index, "while("),
                    operandSwitchIndex  = _parser.FindNextChars(index, "switch("),
                    operandHelperIndex  = _parser.FindNextChars(index, "helper "),
                    operandDoWhileIndex = _parser.FindNextChars(index, "do{");

                if (operandIfIndex > -1)
                {
                    _blockType = BlockType.If;
                    _parser.WritePlainCode("if(");
                    _parser.charIndex = operandIfIndex;
                }
                else if (operandHelperIndex > -1)
                {
                    _blockType     = BlockType.Helper;
                    _parser.helper = true;
                    _parser.WritePlainCode("function ");
                    _parser.charIndex = operandHelperIndex;
                }
                else if (operandForIndex > -1)
                {
                    _blockType = BlockType.For;
                    _parser.WritePlainCode("for(");
                    _parser.charIndex = operandForIndex;
                }
                else if (operandWhileIndex > -1)
                {
                    _blockType = BlockType.While;
                    _parser.WritePlainCode("while(");
                    _parser.charIndex = operandWhileIndex;
                }
                else if (operandWithIndex > -1)
                {
                    _blockType = BlockType.With;
                    _parser.WritePlainCode("with(");
                    _parser.charIndex = operandWithIndex;
                }
                else if (operandSwitchIndex > -1)
                {
                    _blockType = BlockType.Switch;
                    _parser.WritePlainCode("switch(");
                    _parser.charIndex = operandSwitchIndex;
                }
                else if (operandDoWhileIndex > -1)
                {
                    _blockType = BlockType.DoWhile;
                    _parser.WritePlainCode("do{");
                    _parser.charIndex = operandDoWhileIndex;
                    _bcounter         = 1;
                }
                else
                {
                    _blockType = BlockType.Expression;
                }
            }
            Parse();
        }
        private bool Process(char chr, int index)
        {
            if (chr == RazorParser.RazorChr)
            {
                if (_buffer.Length > 0)
                {
                    _parser.WriteHtml(_buffer.ToString());
                    _buffer.Clear();
                }
                IDocumentParser parser = new RazorDocumentParser(_razor, _root != null ? _root : this);
                parser.Execute(_parser);
            }
            else
            {
                WriteBuffer(chr);
                char prev = _parser.GetCharacterAtIndex(index - 1), next = _parser.GetCharacterAtIndex(index + 1);

                if (_textMode && _buffer.Length >= 6 && chr == RazorParser.TagEndChr)
                {
                    if (_parser.FindNextChars(index - 5, RazorParser.TextTag) > -1)
                    {
                        _buffer.Remove(_buffer.Length - 6, 6);
                    }
                }
                else if (chr == RazorParser.TagStartChr && (prev != RazorParser.QuoteChr || prev != RazorParser.SingleQuoteChr) && (next != RazorParser.TagCloseChr))
                {
                    if (_textMode)
                    {
                        if (_parser.FindNextChars(index, RazorParser.TextTag) > -1)
                        {
                            _tcounter++;
                        }
                    }
                    else
                    {
                        _tcounter++;
                    }
                }
                else if (chr == RazorParser.TagCloseChr && (prev == RazorParser.TagStartChr || next == RazorParser.TagEndChr))
                {
                    if (_textMode)
                    {
                        if (_parser.FindNextChars(index - 1, RazorParser.TextCloseTag) > -1)
                        {
                            _tcounter--;
                        }
                    }
                    else
                    {
                        _tcounter--;
                    }
                }

                if (_tcounter == 0)
                {
                    while (true)
                    {
                        chr = _parser.GetCharacterAtIndex(++_parser.charIndex);
                        if (!_textMode)
                        {
                            WriteBuffer(chr);
                        }
                        if (chr == RazorParser.TagEndChr)
                        {
                            break;
                        }
                    }
                    if (_textMode)
                    {
                        _buffer.Remove(_buffer.Length - 2, 2);
                    }
                    _parser.WriteHtml(_buffer.ToString());
                    if (_razor == null)
                    {
                        _parser.EndViewResponse();
                    }
                    _buffer.Clear();
                    return(true);
                }
            }
            return(false);
        }