Exemplo n.º 1
0
        /* Function: GetList
         * Converts the contents of a list tag to NDMarkup and adds it to the output.  The iterator should be on an opening list tag
         * and when it ends it will be past the closing tag.
         */
        protected void GetList(ref XMLIterator iterator, StringBuilder output, TagStack tagStack)
        {
                        #if DEBUG
            if (iterator.IsOnTag("list", TagForm.Opening) == false)
            {
                throw new Exception("GetList() can only be called when the iterator is on an opening list tag.");
            }
                        #endif

            tagStack.OpenTag("list");
            int surroundingListTagIndex = tagStack.Count - 1;

            iterator.Next();

            List <ListItem> items         = new List <ListItem>();
            ListItem        currentItem   = new ListItem();
            StringBuilder   stringBuilder = new StringBuilder();            // To reuse

            while (iterator.IsInBounds)
            {
                if (iterator.IsOnTag("list", TagForm.Closing))
                {
                    iterator.Next();
                    break;
                }

                else if (iterator.IsOnTag("item") ||
                         iterator.IsOnTag("listheader"))
                {
                    if (iterator.TagForm == TagForm.Opening)
                    {
                        currentItem           = new ListItem();
                        currentItem.IsHeading = (iterator.TagType == "listheader");
                    }

                    else if (iterator.TagForm == TagForm.Closing)
                    {
                        if (currentItem.Term != null)
                        {
                            currentItem.Term = Normalize(currentItem.Term.Trim());

                            if (currentItem.Term == "")
                            {
                                currentItem.Term = null;
                            }
                            else if (currentItem.IsHeading)
                            {
                                currentItem.Term = "<b>" + currentItem.Term + "</b>";
                            }
                        }

                        if (currentItem.Description != null)
                        {
                            currentItem.Description = Normalize(currentItem.Description.Trim());

                            if (currentItem.Description == "")
                            {
                                currentItem.Description = null;
                            }
                            else if (currentItem.IsHeading)
                            {
                                currentItem.Description = currentItem.Description.Replace("<p>", "<p><b>");
                                currentItem.Description = currentItem.Description.Replace("</p>", "</b></p>");
                            }
                        }

                        if (currentItem.Term != null || currentItem.Description != null)
                        {
                            items.Add(currentItem);
                        }

                        currentItem = new ListItem();
                    }

                    iterator.Next();
                }

                else if (iterator.IsOnTag("term", TagForm.Opening))
                {
                    tagStack.OpenTag("term");
                    iterator.Next();

                    stringBuilder.Remove(0, stringBuilder.Length);
                    GetSimpleText(ref iterator, stringBuilder, tagStack);
                    currentItem.Term = stringBuilder.ToString();

                    if (iterator.TagType == "term" && iterator.TagForm == TagForm.Closing)
                    {
                        iterator.Next();
                    }

                    tagStack.CloseTag("term");
                }

                else if (iterator.IsOnTag("description", TagForm.Opening))
                {
                    tagStack.OpenTag("description");
                    iterator.Next();

                    stringBuilder.Remove(0, stringBuilder.Length);
                    GetText(ref iterator, stringBuilder, tagStack);
                    currentItem.Description = stringBuilder.ToString();

                    if (iterator.TagType == "description" && iterator.TagForm == TagForm.Closing)
                    {
                        iterator.Next();
                    }

                    tagStack.CloseTag("description");
                }

                else if (iterator.IsOnTag(TagForm.Opening))
                {
                    SkipBlock(ref iterator);
                }

                else
                {
                    iterator.Next();
                }
            }

            tagStack.CloseTag(surroundingListTagIndex);

            if (items.Count > 0)
            {
                bool hasTerms        = false;
                bool hasDescriptions = false;

                for (int i = 0; i < items.Count && (hasTerms == false || hasDescriptions == false); i++)
                {
                    if (items[i].Term != null)
                    {
                        hasTerms = true;
                    }
                    if (items[i].Description != null)
                    {
                        hasDescriptions = true;
                    }
                }

                if (hasTerms && hasDescriptions)
                {
                    output.Append("<dl>");

                    foreach (var item in items)
                    {
                        output.Append("<de>");

                        if (item.Term != null)
                        {
                            output.Append(item.Term);
                        }

                        output.Append("</de><dd>");

                        if (item.Description != null)
                        {
                            output.Append(item.Description);
                        }

                        output.Append("</dd>");
                    }

                    output.Append("</dl>");
                }

                else                 // doesn't have both
                {
                    output.Append("<ul>");

                    foreach (var item in items)
                    {
                        output.Append("<li>");

                        // The format only allows for descriptions without terms, but we'll support terms without descriptions as well.
                        if (item.Term != null)
                        {
                            output.Append(item.Term);
                        }
                        if (item.Description != null)
                        {
                            output.Append(item.Description);
                        }

                        output.Append("</li>");
                    }

                    output.Append("</ul>");
                }
            }
        }
Exemplo n.º 2
0
        /* Function: GetCode
         * Converts the contents of a code tag to NDMarkup and adds it to the output.  The iterator should be on an opening code tag
         * and when it ends it will be past the closing tag.
         */
        protected void GetCode(ref XMLIterator iterator, StringBuilder output, TagStack tagStack)
        {
                        #if DEBUG
            if (iterator.IsOnTag("code", TagForm.Opening) == false)
            {
                throw new Exception("GetCode() can only be called when the iterator is on an opening code tag.");
            }
                        #endif

            output.Append("<pre type=\"code\">");
            tagStack.OpenTag("code", "</pre>");

            int surroundingCodeTagIndex = tagStack.Count - 1;

            iterator.Next();

            List <CodeLine> lines = new List <CodeLine>();

            CodeLine currentLine = new CodeLine();
            currentLine.Indent = -1;              // Don't use text immediately following the code tag to figure out the shared indent.
            currentLine.Text   = null;

            for (;;)
            {
                if (iterator.IsInBounds == false)
                {
                    lines.Add(currentLine);
                    break;
                }
                else if (iterator.IsOnTag(TagForm.Closing))
                {
                    int openingTagIndex = tagStack.FindTag(iterator.TagType);

                    if (openingTagIndex != -1 && openingTagIndex <= surroundingCodeTagIndex)
                    {
                        lines.Add(currentLine);
                        break;
                    }

                    // Otherwise let it fall through to be treated as text.
                }

                if (iterator.IsOn(XMLElementType.LineBreak))
                {
                    lines.Add(currentLine);

                    currentLine        = new CodeLine();
                    currentLine.Indent = 0;
                    currentLine.Text   = null;
                }
                else if (iterator.IsOn(XMLElementType.Indent))
                {
                    currentLine.Indent = iterator.Indent;
                }
                else                 // entity, unhandled tag, text
                {
                    if (currentLine.Text == null)
                    {
                        currentLine.Text = iterator.String;
                    }
                    else
                    {
                        currentLine.Text += iterator.String;
                    }
                }

                iterator.Next();
            }

            Normalize(lines);


            // Build the output.

            for (int i = 0; i < lines.Count; i++)
            {
                if (lines[i].Indent >= 1)
                {
                    output.Append(' ', lines[i].Indent);
                }

                if (lines[i].Text != null)
                {
                    output.EntityEncodeAndAppend(lines[i].Text);
                }

                if (i < lines.Count - 1)
                {
                    output.Append("<br>");
                }
            }

            tagStack.CloseTag(surroundingCodeTagIndex, output);
        }
Exemplo n.º 3
0
        /* Function: GetText
         * Converts a block of formatted text to NDMarkup and adds it to the output.  It ends when it reaches the closing tag for anything
         * already on the tag stack.
         */
        protected void GetText(ref XMLIterator iterator, StringBuilder output, TagStack tagStack)
        {
            output.Append("<p>");
            tagStack.OpenTag(null, "</p>");

            int surroundingPTagIndex = tagStack.Count - 1;

            while (iterator.IsInBounds)
            {
                if (iterator.IsOn(XMLElementType.Text))
                {
                    output.EntityEncodeAndAppend(iterator.String);
                    iterator.Next();
                }

                else if (iterator.IsOn(XMLElementType.EntityChar))
                {
                    output.EntityEncodeAndAppend(iterator.EntityValue);
                    iterator.Next();
                }

                else if (iterator.IsOn(XMLElementType.LineBreak))
                {
                    // Add a literal line break.  We'll replace these with spaces or double spaces later.  Right now we can't decide
                    // which it should be because you can't run a regex directly on a StringBuilder and it would be inefficient to convert
                    // it to a string on every line break.
                    output.Append('\n');

                    iterator.Next();
                }

                else if (iterator.IsOnTag("para"))
                {
                    // Text can appear both inside and outside of <para> tags, and whitespace can appear between <para> tags that
                    // can be mistaken for content, so rather than put in a lot of logic we handle it in a very dirty but simple way.  Every
                    // <para> tag--opening, closing, standalone (technically invalid)--causes a paragraph break.  Normalize() will clean it
                    // up for us afterwards.

                    tagStack.CloseTag(surroundingPTagIndex + 1, output);                      // Reuse our surrounding tag
                    output.Append("</p><p>");
                    iterator.Next();
                }

                else if (iterator.IsOnTag("code", TagForm.Opening))
                {
                    output.Append("</p>");
                    GetCode(ref iterator, output, tagStack);
                    output.Append("<p>");
                }

                else if (iterator.IsOnTag("example", TagForm.Opening))
                {
                    // <example> can be nested in addition to a top-level tag.
                    output.Append("</p><h>");
                    output.EntityEncodeAndAppend(
                        Engine.Locale.Get("NaturalDocs.Engine", "XML.Heading.example")
                        );
                    output.Append("</h><p>");

                    tagStack.OpenTag("example", "</p><p>");
                    iterator.Next();
                }

                else if (iterator.IsOnTag("list", TagForm.Opening))
                {
                    output.Append("</p>");
                    GetList(ref iterator, output, tagStack);
                    output.Append("<p>");
                }

                else if (iterator.IsOnTag("paramref") ||
                         iterator.IsOnTag("typeparamref"))
                {
                    // Can't assume all the properties are set
                    string name = iterator.TagProperty("name");

                    if (name != null)
                    {
                        output.EntityEncodeAndAppend(name);
                    }

                    iterator.Next();
                }

                else if (iterator.IsOnTag("see", TagForm.Standalone))
                {
                    // Can't assume all the properties are set
                    string cref = iterator.TagProperty("cref");

                    if (cref != null)
                    {
                        output.Append("<link type=\"naturaldocs\" originaltext=\"");
                        output.EntityEncodeAndAppend(cref);
                        output.Append("\">");
                    }
                    else
                    {
                        string langword = iterator.TagProperty("langword");

                        if (langword != null)
                        {
                            output.EntityEncodeAndAppend(langword);
                        }
                    }

                    iterator.Next();
                }

                else if (iterator.IsOnTag(TagForm.Opening))
                {
                    tagStack.OpenTag(iterator.TagType);
                    iterator.Next();
                }

                else if (iterator.IsOnTag(TagForm.Closing))
                {
                    int openingTagIndex = tagStack.FindTag(iterator.TagType);

                    if (openingTagIndex == -1)
                    {
                    }
                    else if (openingTagIndex < surroundingPTagIndex)
                    {
                        break;
                    }
                    else
                    {
                        tagStack.CloseTag(openingTagIndex, output);
                    }

                    iterator.Next();
                }

                else
                {
                    // Ignore indent.  Spaces between words will be handled by line breaks.
                    // Ignore unrecognized standalone tags.
                    iterator.Next();
                }
            }

            tagStack.CloseTag(surroundingPTagIndex, output);
        }
Exemplo n.º 4
0
        /* Function: GetSimpleText
         * Converts a block of plain unformatted text to NDMarkup and adds it to the output.  Unlike <GetText()> this will not surround the
         * output in paragraph tags.  It ends when it reaches the closing tag for anything already on the tag stack.
         */
        protected void GetSimpleText(ref XMLIterator iterator, StringBuilder output, TagStack tagStack)
        {
            int surroundingTagCount = tagStack.Count;

            while (iterator.IsInBounds)
            {
                if (iterator.IsOn(XMLElementType.Text))
                {
                    output.EntityEncodeAndAppend(iterator.String);
                    iterator.Next();
                }

                else if (iterator.IsOn(XMLElementType.EntityChar))
                {
                    output.EntityEncodeAndAppend(iterator.EntityValue);
                    iterator.Next();
                }

                else if (iterator.IsOn(XMLElementType.LineBreak))
                {
                    // Add a literal line break.  We'll replace these with spaces or double spaces later.  Right now we can't decide
                    // which it should be because you can't run a regex directly on a StringBuilder and it would be inefficient to convert
                    // it to a string on every line break.
                    output.Append('\n');

                    iterator.Next();
                }

                else if (iterator.IsOnTag("paramref") ||
                         iterator.IsOnTag("typeparamref"))
                {
                    // Can't assume all the properties are set
                    string name = iterator.TagProperty("name");

                    if (name != null)
                    {
                        output.Append(name);
                    }

                    iterator.Next();
                }

                else if (iterator.IsOnTag(TagForm.Opening))
                {
                    tagStack.OpenTag(iterator.TagType);
                    iterator.Next();
                }

                else if (iterator.IsOnTag(TagForm.Closing))
                {
                    int openingTagIndex = tagStack.FindTag(iterator.TagType);

                    if (openingTagIndex == -1)
                    {
                    }
                    else if (openingTagIndex <= surroundingTagCount - 1)
                    {
                        break;
                    }
                    else
                    {
                        tagStack.CloseTag(openingTagIndex, output);
                    }

                    iterator.Next();
                }

                else
                {
                    // Ignore indent.  Spaces between words will be handled by line breaks.
                    // Ignore unrecognized standalone tags.
                    iterator.Next();
                }
            }

            if (tagStack.Count > surroundingTagCount)
            {
                tagStack.CloseTag(surroundingTagCount, output);
            }
        }
Exemplo n.º 5
0
        /* Function: GetText
         *
         * Converts a stretch of formatted text to NDMarkup.
         *
         * Modes:
         *
         *		Normal - The iterator continues until it goes out of bounds.
         *		ListItem - The iterator continues until it reaches a closing li tag.  It also skips certain formatting that is not supported
         *					  in list items in NDMarkup.
         */
        protected void GetText(ref JavadocIterator iterator, StringBuilder output, GetTextMode mode = GetTextMode.Normal)
        {
            output.Append("<p>");

            TagStack tagStack = new TagStack();

            tagStack.OpenTag(null, "</p>");

            while (iterator.IsInBounds)
            {
                if (iterator.IsOn(JavadocElementType.Text))
                {
                    output.EntityEncodeAndAppend(iterator.String);
                    iterator.Next();
                }

                else if (iterator.IsOn(JavadocElementType.EntityChar))
                {
                    output.EntityEncodeAndAppend(iterator.EntityValue);
                    iterator.Next();
                }

                else if (iterator.IsOn(JavadocElementType.LineBreak))
                {
                    // Add a literal line break.  We'll replace these with spaces or double spaces later.  Right now we can't decide
                    // which it should be because you can't run a regex directly on a StringBuilder and it would be inefficient to convert
                    // it to a string on every line break.
                    output.Append('\n');

                    iterator.Next();
                }

                else if (iterator.IsOnHTMLTag("p"))
                {
                    // Text can appear both inside and outside of <p> tags, whitespace can appear between <p> tags that can be
                    // mistaken for content, and people can use <p> tags as standalone rather than opening tags.  Rather than put in
                    // logic to try to account for all of this we handle it in a very dirty but simple way.  Every <p> tag--opening, closing,
                    // or standalone--causes a paragraph break.  Normalize() will clean it up for us afterwards.

                    tagStack.CloseTag(1, output);                      // Reuse our surrounding tag
                    output.Append("</p><p>");
                    iterator.Next();
                }

                else if (iterator.IsOnHTMLTag("b") ||
                         iterator.IsOnHTMLTag("strong"))
                {
                    if (iterator.HTMLTagForm == TagForm.Opening)
                    {
                        tagStack.OpenTag(iterator.TagType, "</b>");
                        output.Append("<b>");
                    }
                    else if (iterator.HTMLTagForm == TagForm.Closing)
                    {
                        tagStack.CloseTag(iterator.TagType, output);
                    }

                    iterator.Next();
                }

                else if (iterator.IsOnHTMLTag("i") ||
                         iterator.IsOnHTMLTag("em"))
                {
                    if (iterator.HTMLTagForm == TagForm.Opening)
                    {
                        tagStack.OpenTag(iterator.TagType, "</i>");
                        output.Append("<i>");
                    }
                    else if (iterator.HTMLTagForm == TagForm.Closing)
                    {
                        tagStack.CloseTag(iterator.TagType, output);
                    }

                    iterator.Next();
                }

                else if (iterator.IsOnHTMLTag("u"))
                {
                    if (iterator.HTMLTagForm == TagForm.Opening)
                    {
                        tagStack.OpenTag(iterator.TagType, "</u>");
                        output.Append("<u>");
                    }
                    else if (iterator.HTMLTagForm == TagForm.Closing)
                    {
                        tagStack.CloseTag(iterator.TagType, output);
                    }

                    iterator.Next();
                }

                else if (iterator.IsOnHTMLTag("pre", TagForm.Opening) && mode == GetTextMode.Normal)                  // Ignore pre's in list items
                {
                    output.Append("</p>");
                    GetPre(ref iterator, output);
                    output.Append("<p>");
                }

                else if (iterator.IsOnHTMLTag("ul") ||
                         iterator.IsOnHTMLTag("ol"))
                {
                    if (iterator.HTMLTagForm == TagForm.Opening)
                    {
                        output.Append("</p>");
                        GetList(ref iterator, output);
                        output.Append("<p>");
                    }
                    else if (iterator.HTMLTagForm == TagForm.Closing && mode == GetTextMode.ListItem)
                    {
                        break;
                    }
                    else
                    {
                        iterator.Next();
                    }
                }

                else if (iterator.IsOnHTMLTag("li", TagForm.Closing) && mode == GetTextMode.ListItem)
                {
                    break;
                }

                else if (iterator.IsOnHTMLTag("a", TagForm.Opening))
                {
                    string href = iterator.HTMLTagProperty("href");

                    if (href == null || href == "" || href == "#" || href.StartsWith("{@docRoot}") ||
                        href.StartsWith("javascript:", StringComparison.CurrentCultureIgnoreCase))
                    {
                        iterator.Next();
                    }
                    else
                    {
                        GetHTMLLink(ref iterator, output);
                    }
                }

                else if (iterator.IsOnJavadocTag("code") ||
                         iterator.IsOnJavadocTag("literal"))
                {
                    // These get added without searching the contents for nested tags
                    output.EntityEncodeAndAppend(iterator.JavadocTagValue);
                    iterator.Next();
                }

                else if (iterator.IsOnJavadocTag("link") ||
                         iterator.IsOnJavadocTag("linkPlain"))
                {
                    Tokenizer     linkContent  = new Tokenizer(iterator.JavadocTagValue);
                    TokenIterator linkIterator = linkContent.FirstToken;

                    string symbol = GetJavadocLinkSymbol(ref linkIterator);
                    linkIterator.NextPastWhitespace();

                    string description = GetSimpleText(linkIterator, linkContent.LastToken);
                    description = Normalize(description);

                    if (description == null || description == "")
                    {
                        output.Append("<link type=\"naturaldocs\" originaltext=\"");
                        output.EntityEncodeAndAppend(symbol);
                        output.Append("\">");
                    }
                    else
                    {
                        output.Append("<link type=\"naturaldocs\" originaltext=\"");
                        output.EntityEncodeAndAppend(description);
                        output.Append(" at ");
                        output.EntityEncodeAndAppend(symbol);
                        output.Append("\">");
                    }

                    iterator.Next();
                }

                else if (iterator.IsOnJavadocTag("value"))
                {
                    string symbol = iterator.JavadocTagValue;

                    if (symbol == null || symbol == "")
                    {
                        output.EntityEncodeAndAppend(
                            Locale.Get("NaturalDocs.Engine", "Javadoc.Substitution.value")
                            );
                    }
                    else
                    {
                        string substitution      = Locale.Get("NaturalDocs.Engine", "Javadoc.Substitution.value(symbol)", '\x1F');
                        int    substitutionIndex = substitution.IndexOf('\x1F');

                        if (substitutionIndex == -1)
                        {
                            output.EntityEncodeAndAppend(substitution);
                        }
                        else
                        {
                            if (substitutionIndex > 0)
                            {
                                output.EntityEncodeAndAppend(substitution, 0, substitutionIndex);
                            }

                            output.Append("<link type=\"naturaldocs\" originaltext=\"");
                            output.EntityEncodeAndAppend(symbol);
                            output.Append("\">");

                            if (substitutionIndex < substitution.Length - 1)
                            {
                                output.EntityEncodeAndAppend(substitution, substitutionIndex + 1, substitution.Length - (substitutionIndex + 1));
                            }
                        }
                    }

                    iterator.Next();
                }

                else
                {
                    // Ignore indent.  Spaces between words will be handled by line breaks.
                    // Ignore HTML comments.
                    // Ignore unrecognized HTML tags.
                    iterator.Next();
                }
            }

            tagStack.CloseAllTags(output);
        }