Esempio n. 1
0
        public static XmlComment[] ParseComment(XDocument xDocument, string namespaceMatch)
        {
            return(xDocument.Descendants("member")
                   .Select(x => {
                var match = Regex.Match(x.Attribute("name").Value, @"(.):(.+)\.([^.()]+)?(\(.+\)|$)");
                if (!match.Groups[1].Success)
                {
                    return null;
                }

                var memberType = (MemberType)match.Groups[1].Value[0];
                if (memberType == MemberType.None)
                {
                    return null;
                }

                var summaryXml = x.Elements("summary").FirstOrDefault()?.ToString()
                                 ?? x.Element("summary")?.ToString()
                                 ?? "";
                summaryXml = Regex.Replace(summaryXml, @"<\/?summary>", string.Empty);
                summaryXml = Regex.Replace(summaryXml, @"<para\s*/>", Environment.NewLine);
                summaryXml = Regex.Replace(summaryXml, @"<see cref=""\w:([^\""]*)""\s*\/>", m => ResolveSeeElement(m, namespaceMatch));

                var parsed = Regex.Replace(summaryXml, @"<(type)*paramref name=""([^\""]*)""\s*\/>", e => $"`{e.Groups[1].Value}`");

                var summary = parsed;

                if (summary != "")
                {
                    summary = string.Join("  ", summary.Split(new[] { "\r", "\n", "\t" }, StringSplitOptions.RemoveEmptyEntries).Select(y => y.Trim()));
                }

                var returns = ((string)x.Element("returns")) ?? "";
                var remarks = ((string)x.Element("remarks")) ?? "";
                var parameters = x.Elements("param")
                                 .Select(e => Tuple.Create(e.Attribute("name").Value, e))
                                 .Distinct(new Item1EqualityCompaerer <string, XElement>())
                                 .ToDictionary(e => e.Item1, e => e.Item2.Value);

                var className = (memberType == MemberType.Type)
                        ? match.Groups[2].Value + "." + match.Groups[3].Value
                        : match.Groups[2].Value;

                XmlComment xmlComment = new XmlComment
                {
                    MemberType = memberType,
                    ClassName = className,
                    MemberName = match.Groups[3].Value.Replace("#", "."),
                    Summary = summary.Trim(),
                    Remarks = remarks.Trim(),
                    Parameters = parameters,
                    Returns = returns.Trim()
                };

                if (xmlComment.MemberName.Equals(".ctor"))
                {
                    xmlComment.MemberType = MemberType.Constructor;
                }

                return xmlComment;
            })
                   .Where(x => x != null)
                   .ToArray());
        }