nextTag


/// <summary>Recursively populates the next-highest tag</summary>
private tag nextTag(string textBlock, int depth)
{
    int startIndex = textBlock.IndexOf('>');
    tag newTag = new tag(textBlock.Substring(1, startIndex-1), depth);
    int endIndex = textBlock.IndexOf("</" + newTag.name + ">");
    bool haveEmptyTag = endIndex == startIndex + 1;

    List<string> contents = new List<string>();
    List<tag> tagChildren = new List<tag>();

    string stringFormat = "";

    if ( haveEmptyTag ) { return newTag; }
    else
    {
        string remainingInternals = textBlock.Substring(startIndex + 1, endIndex - startIndex - 1);

        int firstThan = -2;
        int lastThan = -2;
        int nextThan = -2;
        int afterEndThan = -2;

        bool haveContentOrTags = false;
        bool haveContent = false;
        bool haveTag = false;
       
        string nameAttr = "";
        string tagName = "";
        string tagEnd = "";
        string tagBlock = "";

        string content = "";

        haveContentOrTags = remainingInternals.Length > 0;
        while (haveContentOrTags)
        {
            firstThan = remainingInternals.IndexOf("<");
            lastThan = remainingInternals.IndexOf(">");
            nextThan = lastThan + 1;
            haveTag = firstThan > -1;
            haveContent = (!haveTag || (haveTag && firstThan>0));

            if (haveContent)
            {
                if (lastThan > -1)
                {
                    nextThan = remainingInternals.Substring(lastThan).IndexOf('<') + lastThan;
                }

                content = (!haveTag)
                        ? remainingInternals
                        : remainingInternals.Substring(lastThan + 1, nextThan - lastThan - 1);

                if (content.Length > 0)
                {
                    contents.Add(content);
                    stringFormat += "c" + contents.Count;
                    remainingInternals = remainingInternals.Substring(content.Length);
                }
            }

            // continuing so we look for a tag
            // note: remainingInternals, at this point, should start with a ">"
            if (haveTag && remainingInternals.Length > 0)
            {
                lastThan = remainingInternals.IndexOf(">");
                nameAttr = remainingInternals.Substring(1, lastThan - 1);
                tagName = nameAttr;
                if (tagName.IndexOf(" ") > -1) { tagName = tagName.Substring(0, tagName.IndexOf(" ")); }
                tagEnd = "</" + tagName + ">";
                afterEndThan = remainingInternals.IndexOf(tagEnd) + tagEnd.Length;
                tagBlock = remainingInternals.Substring(0, afterEndThan);
                tagChildren.Add(nextTag(tagBlock, depth+1));
                stringFormat += "t" + tagChildren.Count;
                remainingInternals = remainingInternals.Substring(tagBlock.Length);
            }
            haveContentOrTags = remainingInternals.Length > 0;
        }
        newTag.contents = contents;
        newTag.tagChildren = tagChildren;
        newTag.stringFormat = stringFormat;
    }
    return newTag;
}