forked from neuecc/MarkdownGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VSDocParser.cs
165 lines (146 loc) · 6.3 KB
/
VSDocParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace MarkdownGeneratorCore
{
public enum MemberType
{
Field = 'F',
Property = 'P',
Type = 'T',
Event = 'E',
Method = 'M',
None = 0
}
public class XmlDocumentComment
{
public MemberType MemberType { get; set; }
public string ClassName { get; set; }
public string MemberName { get; set; }
public string Summary { get; set; }
public string Remarks { get; set; }
public Dictionary<string, string> Parameters { get; set; }
public string Returns { get; set; }
public override string ToString()
{
return MemberType + ":" + ClassName + "." + MemberName;
}
}
public static class VSDocParser
{
public static XmlDocumentComment[] ParseXmlComment(XDocument xDocument)
{
return ParseXmlComment(xDocument, null);
}
// cheap, quick hack parser:)
internal static XmlDocumentComment[] ParseXmlComment(XDocument xDocument, Regex namespaceRegex)
{
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, namespaceRegex));
var parsed = Regex.Replace(summaryXml, @"<(type)*paramref name=""([^\""]*)""\s*\/>", e => $"`{e.Groups[1].Value}`");
var summary = parsed;
if (summary != "")
{
summary = string.Join(Environment.NewLine, summary.Split(Environment.NewLine).Select(y => y.Trim()));
}
var sourcesElement = x.Elements("sources").FirstOrDefault() ?? x.Element("sources");
if (sourcesElement != null)
{
summary += Environment.NewLine + "**Sources:**" + Environment.NewLine;
var sinks = sourcesElement.Elements("source").Select(s => s.Value.Trim()).ToList();
if (!sinks.Any())
{
summary += "(none)" + Environment.NewLine;
}
else
{
foreach (var s in sinks)
{
summary += "* " + s + Environment.NewLine;
}
}
summary += Environment.NewLine;
}
var sinksElement = x.Elements("sinks").FirstOrDefault() ?? x.Element("sinks");
if (sinksElement != null)
{
if (sourcesElement == null)
summary += Environment.NewLine;
summary += "**Sinks:**" + Environment.NewLine;
var sinks = sinksElement.Elements("sink").Select(s => s.Value.Trim()).ToList();
if (!sinks.Any())
{
summary += "(none)" + Environment.NewLine;
}
else
{
foreach (var s in sinks)
{
summary += "* " + s + Environment.NewLine;
}
}
summary += Environment.NewLine;
}
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;
return new XmlDocumentComment
{
MemberType = memberType,
ClassName = className,
MemberName = match.Groups[3].Value,
Summary = summary.Trim(),
Remarks = remarks.Trim(),
Parameters = parameters,
Returns = returns.Trim()
};
})
.Where(x => x != null)
.ToArray();
}
private static string ResolveSeeElement(Match m, Regex ns)
{
var typeName = m.Groups[1].Value;
if (ns != null)
{
if (ns.IsMatch(typeName))
{
return $"[{typeName}]({Regex.Replace(typeName, $"\\.(?:.(?!\\.))+$", me => me.Groups[0].Value.Replace(".", "#").ToLower())})";
}
}
return $"`{typeName}`";
}
class Item1EqualityCompaerer<T1, T2> : EqualityComparer<Tuple<T1, T2>>
{
public override bool Equals(Tuple<T1, T2> x, Tuple<T1, T2> y)
{
return x.Item1.Equals(y.Item1);
}
public override int GetHashCode(Tuple<T1, T2> obj)
{
return obj.Item1.GetHashCode();
}
}
}
}