/* 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); } }
/* 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); }
/* 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); }