-
Notifications
You must be signed in to change notification settings - Fork 1
/
XmlDocumentationFormatter.cs
64 lines (52 loc) · 2.86 KB
/
XmlDocumentationFormatter.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
using System.Text.RegularExpressions;
using ICSharpCode.Decompiler.Documentation;
using ICSharpCode.Decompiler.TypeSystem;
namespace NetAmermaid
{
/// <summary>Wraps the <see cref="IDocumentationProvider"/> to prettify XML documentation comments.
/// Make sure to enable XML documentation output, see
/// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/xmldoc/#create-xml-documentation-output .</summary>
public class XmlDocumentationFormatter
{
/// <summary>Matches XML indent.</summary>
protected const string linePadding = @"^[ \t]+|[ \t]+$";
/// <summary>Matches reference tags indluding "see href", "see cref" and "paramref name"
/// with the cref value being prefixed by symbol-specific letter and a colon
/// including the quotes around the attribute value and the closing slash of the tag containing the attribute.</summary>
protected const string referenceAttributes = @"(see\s.ref=""(.:)?)|(paramref\sname="")|(""\s/)";
private readonly IDocumentationProvider docs;
private readonly Regex noiseAndPadding;
public XmlDocumentationFormatter(IDocumentationProvider docs, string[]? strippedNamespaces)
{
this.docs = docs;
List<string> regexes = new() { linePadding, referenceAttributes };
if (strippedNamespaces?.Any() == true)
regexes.AddRange(strippedNamespaces.Select(ns => $"({ns.Replace(".", "\\.")}\\.)"));
noiseAndPadding = new Regex(regexes.Join("|"), RegexOptions.Multiline); // builds an OR | combined regex
}
internal Dictionary<string, string>? GetXmlDocs(ITypeDefinition type, params IMember[][] memberCollections)
{
Dictionary<string, string>? docs = new();
AddXmlDocEntry(docs, type);
foreach (IMember[] members in memberCollections)
foreach (IMember member in members)
AddXmlDocEntry(docs, member);
return docs?.Keys.Any() == true ? docs : default;
}
protected virtual string? GetDoco(IEntity entity)
{
string? comment = docs.GetDocumentation(entity)?
.ReplaceAll(new[] { "<summary>", "</summary>" }, null)
.ReplaceAll(new[] { "<para>", "</para>" }, ClassDiagrammer.NewLine).Trim() // to format
.Replace('<', '[').Replace('>', ']'); // to prevent ugly escaped output
return comment == null ? null : noiseAndPadding.Replace(comment, string.Empty).NormalizeHorizontalWhiteSpace();
}
private void AddXmlDocEntry(Dictionary<string, string> docs, IEntity entity)
{
string? doc = GetDoco(entity);
if (string.IsNullOrEmpty(doc)) return;
string key = entity is IMember member ? member.Name : string.Empty;
docs[key] = doc;
}
}
}