Exemple #1
0
        public static void WriteContentTo(this XElement element, DocumentationWriter writer, bool inlineOnly = false)
        {
            using (IEnumerator <XNode> en = element.Nodes().GetEnumerator())
            {
                if (en.MoveNext())
                {
                    XNode node;

                    var  isFirst = true;
                    bool isLast;

                    do
                    {
                        node = en.Current;

                        isLast = !en.MoveNext();

                        if (node is XText t)
                        {
                            string value = t.Value;
                            value = TextUtility.RemoveLeadingTrailingNewLine(value, isFirst, isLast);

                            if (inlineOnly)
                            {
                                value = TextUtility.ToSingleLine(value);
                            }

                            writer.WriteString(value);
                        }
                        else if (node is XElement e)
                        {
                            switch (XmlTagMapper.GetTagOrDefault(e.Name.LocalName))
                            {
                            case XmlTag.C:
                            {
                                string value = e.Value;
                                value = TextUtility.ToSingleLine(value);
                                writer.WriteInlineCode(value);
                                break;
                            }

                            case XmlTag.Code:
                            {
                                if (inlineOnly)
                                {
                                    break;
                                }

                                string value = e.Value;
                                value = TextUtility.RemoveLeadingTrailingNewLine(value);

                                writer.WriteCodeBlock(value);

                                break;
                            }

                            case XmlTag.List:
                            {
                                if (inlineOnly)
                                {
                                    break;
                                }

                                string type = e.Attribute("type")?.Value;

                                if (!string.IsNullOrEmpty(type))
                                {
                                    switch (type)
                                    {
                                    case "bullet":
                                    {
                                        WriteList(writer, e.Elements());
                                        break;
                                    }

                                    case "number":
                                    {
                                        WriteList(writer, e.Elements(), isOrdered: true);
                                        break;
                                    }

                                    case "table":
                                    {
                                        WriteTable(writer, e.Elements());
                                        break;
                                    }

                                    default:
                                    {
                                        Debug.Fail(type);
                                        break;
                                    }
                                    }
                                }

                                break;
                            }

                            case XmlTag.Para:
                            {
                                writer.WriteLine();
                                writer.WriteLine();
                                WriteContentTo(e, writer);
                                writer.WriteLine();
                                writer.WriteLine();
                                break;
                            }

                            case XmlTag.ParamRef:
                            {
                                string parameterName = e.Attribute("name")?.Value;

                                if (parameterName != null)
                                {
                                    writer.WriteBold(parameterName);
                                }

                                break;
                            }

                            case XmlTag.See:
                            {
                                string commentId = e.Attribute("cref")?.Value;

                                if (commentId != null)
                                {
                                    ISymbol symbol = writer.DocumentationModel.GetFirstSymbolForDeclarationId(commentId);

                                    //XTODO: repair roslyn documentation
                                    Debug.Assert(
                                        symbol != null ||
                                        commentId == "T:Microsoft.CodeAnalysis.CSharp.SyntaxNode" ||
                                        commentId == "T:Microsoft.CodeAnalysis.CSharp.SyntaxToken" ||
                                        commentId == "T:Microsoft.CodeAnalysis.CSharp.SyntaxTrivia" ||
                                        commentId == "T:Microsoft.CodeAnalysis.VisualBasic.SyntaxNode" ||
                                        commentId == "T:Microsoft.CodeAnalysis.VisualBasic.SyntaxToken" ||
                                        commentId == "T:Microsoft.CodeAnalysis.VisualBasic.SyntaxTrivia",
                                        commentId);

                                    if (symbol != null)
                                    {
                                        writer.WriteLink(symbol, TypeSymbolDisplayFormats.Name_ContainingTypes_TypeParameters, SymbolDisplayAdditionalMemberOptions.UseItemPropertyName | SymbolDisplayAdditionalMemberOptions.UseOperatorName);
                                    }
                                    else
                                    {
                                        writer.WriteBold(TextUtility.RemovePrefixFromDocumentationCommentId(commentId));
                                    }
                                }

                                break;
                            }

                            case XmlTag.TypeParamRef:
                            {
                                string typeParameterName = e.Attribute("name")?.Value;

                                if (typeParameterName != null)
                                {
                                    writer.WriteBold(typeParameterName);
                                }

                                break;
                            }

                            case XmlTag.Example:
                            case XmlTag.Exception:
                            case XmlTag.Exclude:
                            case XmlTag.Include:
                            case XmlTag.InheritDoc:
                            case XmlTag.Param:
                            case XmlTag.Permission:
                            case XmlTag.Remarks:
                            case XmlTag.Returns:
                            case XmlTag.SeeAlso:
                            case XmlTag.Summary:
                            case XmlTag.TypeParam:
                            case XmlTag.Value:
                            {
                                break;
                            }

                            default:
                            {
                                Debug.Fail(e.Name.LocalName);
                                break;
                            }
                            }
                        }
                        else
                        {
                            Debug.Fail(node.NodeType.ToString());
                        }

                        isFirst = false;
                    } while (!isLast);
                }
            }
        }