Пример #1
0
        // Whenever we encounter an opening character, and the previous
        // character wasn't an escape character (\) create a new TagObject
        // and start counting till the closing character is reached
        public void ParseText(string text)
        {
            bool tagInProgress = false;            // tells us whether or not we're in the middle of a tag (between < and >)
            bool tagIsClosing  = false;            // tells us if the tag we're reading right now is closing a group (with </ )

            _tagStack.Clear();
            _listOfTags.Clear();
            int line            = 0;
            int actualTextIndex = 0;             // this will only be incremented when a "normal" character is read

            for (int i = 0; i < text.Length; i++)
            {
                char nextChar;
                if (i < text.Length - 1)
                {
                    nextChar = text[i + 1];
                }
                else
                {
                    nextChar = '\0';
                }

                if (!tagInProgress)
                {
                    bool currentCharacterIsOpeningTag = (text[i] == openingCharacter);
                    bool nextCharacterIsEndingTag     = (nextChar == endTagCharacter);

                    if (currentCharacterIsOpeningTag)
                    {
                        tagInProgress = true;
                        if (nextCharacterIsEndingTag)
                        {
                            tagIsClosing = true;

                            var o = _tagStack.Peek();

                            if (o.startIndex == actualTextIndex)
                            {
                                o.endIndex = actualTextIndex;
                                o.Value    = "";
                            }
                            else
                            {
                                o.endIndex = actualTextIndex - 1;
                                o.Value    = _textWithNoTags.Substring(o.startIndex, o.endIndex - o.startIndex + 1);
                            }

                            o.lineIndex = line;
                            //tempTagObject.endIndex = actualTextIndex - 1;
                        }
                        else
                        {
                            // a new tag is happening
                            _tempTagObject = new TagObject();
                            _tagStack.Push(_tempTagObject);
                            _tagStack.Peek().startIndex = actualTextIndex;
                            //tempTagObject.startIndex = actualTextIndex;
                        }
                    }
                    else
                    {
                        _textWithNoTags += text[i];
                        actualTextIndex++;
                    }
                }
                else
                {
                    bool currentCharacterIsClosingTag = (text[i] == closingCharacter);
                    if (currentCharacterIsClosingTag)
                    {
                        tagInProgress = false;
                        if (tagIsClosing)
                        {
                            //Debug.Log($"closing tag for {_tempTagObject.content}, which starts at {_tempTagObject.startIndex}");

                            TagObject finishedTagObject = _tagStack.Pop();
                            _listOfTags.Add(finishedTagObject);
                            tagIsClosing = false;
                        }
                        else
                        {
                            // we're done reading an opening tag
                            // so let's put this on the stack
                            //tagStack.Push(tempTagObject);
                            ///Debug.Log($"Pushing the tag {_tempTagObject.content} onto the stack");
                        }
                    }
                    else if (!tagIsClosing)
                    {
                        _tagStack.Peek().content += text[i];
                        //tempTagObject.contents += text[i];
                    }
                }

                if (text[i] == '\n')
                {
                    line++;
                }
            }

            /*Debug.Log("========");
             * Debug.Log($"Raw text: {_textWithNoTags}");
             * Debug.Log("========");
             *
             * foreach (TagObject tag in _listOfTags) {
             *     tag.WriteInformation();
             *     Debug.Log("========");
             * }*/
        }
Пример #2
0
        public void ParseText(string text)
        {
            // Whenever we encounter an opening character, and the previous
            // character wasn't an escape character (\) create a new TagObject
            // and start counting till the closing character is reached

            string            textWithNoTags = "";                      // the text without the HTML tags
            bool              tagInProgress  = false;                   // tells us whether or not we're in the middle of a tag (between < and >)
            bool              tagIsClosing   = false;                   // tells us if the tag we're reading right now is closing a group (with </ )
            TagObject         tempTagObject  = new TagObject();         // temporary object to store tag info in
            Stack <TagObject> tagStack       = new Stack <TagObject>(); // stack that holds all tag objects
            List <TagObject>  listOfTags     = new List <TagObject>();  // final list of tag objects

            int actualTextIndex = 0;                                    // this will only be incremented when a "normal" character is read


            for (int i = 0; i < text.Length; i++)
            {
                char nextChar;
                if (i < text.Length - 1)
                {
                    nextChar = text[i + 1];
                }
                else
                {
                    nextChar = '\0';
                }

                if (!tagInProgress)
                {
                    bool currentCharacterIsOpeningTag = (text[i] == openingCharacter);
                    bool nextCharacterIsEndingTag     = (nextChar == endTagCharacter);

                    if (currentCharacterIsOpeningTag)
                    {
                        tagInProgress = true;
                        if (nextCharacterIsEndingTag)
                        {
                            tagIsClosing = true;

                            tagStack.Peek().endIndex = actualTextIndex - 1;
                            //tempTagObject.endIndex = actualTextIndex - 1;
                        }
                        else
                        {
                            // a new tag is happening
                            tempTagObject = new TagObject();
                            tagStack.Push(tempTagObject);
                            tagStack.Peek().startIndex = actualTextIndex;
                            //tempTagObject.startIndex = actualTextIndex;
                        }
                    }
                    else
                    {
                        textWithNoTags += text[i];
                        actualTextIndex++;
                    }
                }
                else
                {
                    bool currentCharacterIsClosingTag = (text[i] == closingCharacter);
                    if (currentCharacterIsClosingTag)
                    {
                        tagInProgress = false;
                        if (tagIsClosing)
                        {
                            Console.WriteLine($"closing tag for {tempTagObject.contents}, which starts at {tempTagObject.startIndex}");

                            TagObject finishedTagObject = tagStack.Pop();
                            listOfTags.Add(finishedTagObject);
                            tagIsClosing = false;
                        }
                        else
                        {
                            // we're done reading an opening tag
                            // so let's put this on the stack
                            //tagStack.Push(tempTagObject);
                            Console.WriteLine($"Pushing the tag {tempTagObject.contents} onto the stack");
                        }
                    }
                    else if (!tagIsClosing)
                    {
                        tagStack.Peek().contents += text[i];
                        //tempTagObject.contents += text[i];
                    }
                }
            }
            // Console.WriteLine("========");
            // Console.WriteLine($"Raw text: {textWithNoTags}");
            // Console.WriteLine("========");

            // foreach (TagObject tag in listOfTags) {
            //  tag.WriteInformation();
            //  Console.WriteLine("========");
            // }

            tags = listOfTags;
        }
Пример #3
0
 string _textWithNoTags = "";          // the text without the HTML tags
 public TagParser()
 {
     _tempTagObject = new TagObject();             // temporary object to store tag info in
     _tagStack      = new Stack <TagObject>();     // stack that holds all tag objects
     _listOfTags    = new List <TagObject>();      // final list of tag objects
 }