-
Notifications
You must be signed in to change notification settings - Fork 868
/
XmlComment.cs
610 lines (529 loc) · 20.7 KB
/
XmlComment.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using Markdig;
using Markdig.Helpers;
using Markdig.Renderers.Roundtrip;
using Markdig.Syntax;
using Markdig.Syntax.Inlines;
using Docfx.Common;
using Docfx.DataContracts.ManagedReference;
using Docfx.Plugins;
namespace Docfx.Dotnet;
internal class XmlComment
{
private const string idSelector = @"((?![0-9])[\w_])+[\w\(\)\.\{\}\[\]\|\*\^~#@!`,_<>:]*";
private static readonly Regex CommentIdRegex = new(@"^(?<type>N|T|M|P|F|E|Overload):(?<id>" + idSelector + ")$", RegexOptions.Compiled);
private static readonly Regex RegionRegex = new(@"^\s*#region\s*(.*)$");
private static readonly Regex XmlRegionRegex = new(@"^\s*<!--\s*<([^/\s].*)>\s*-->$");
private static readonly Regex EndRegionRegex = new(@"^\s*#endregion\s*.*$");
private static readonly Regex XmlEndRegionRegex = new(@"^\s*<!--\s*</(.*)>\s*-->$");
private readonly XmlCommentParserContext _context;
public string Summary { get; private set; }
public string Remarks { get; private set; }
public string Returns { get; private set; }
public List<ExceptionInfo> Exceptions { get; private set; }
public List<LinkInfo> SeeAlsos { get; private set; }
public List<string> Examples { get; private set; }
public Dictionary<string, string> Parameters { get; private set; }
public Dictionary<string, string> TypeParameters { get; private set; }
private XmlComment(string xml, XmlCommentParserContext context)
{
// Treat <doc> as <member>
if (xml.StartsWith("<doc>") && xml.EndsWith("</doc>"))
{
var innerXml = xml.Substring(5, xml.Length - 11);
var innerXmlTrim = innerXml.Trim();
// Workaround external XML doc not wrapped in summary tag: https://github.com/dotnet/roslyn/pull/66668
if (innerXmlTrim.StartsWith('<') && innerXmlTrim.EndsWith('>'))
xml = $"<member>{innerXml}</member>";
else
xml = $"<member><summary>{innerXml}</summary></member>";
}
// Workaround: https://github.com/dotnet/roslyn/pull/66668
if (!xml.StartsWith("<member", StringComparison.Ordinal) && !xml.EndsWith("</member>", StringComparison.Ordinal))
{
xml = $"<member>{xml}</member>";
}
// Transform triple slash comment
var doc = XmlCommentTransformer.Transform(xml);
_context = context;
ResolveLangword(doc);
ResolveCrefLink(doc, "//seealso[@cref]", context.AddReferenceDelegate);
ResolveCrefLink(doc, "//see[@cref]", context.AddReferenceDelegate);
ResolveCrefLink(doc, "//exception[@cref]", context.AddReferenceDelegate);
ResolveCode(doc, context);
var nav = doc.CreateNavigator();
Summary = GetSingleNodeValue(nav, "/member/summary");
Remarks = GetSingleNodeValue(nav, "/member/remarks");
Returns = GetSingleNodeValue(nav, "/member/returns");
Exceptions = ToListNullOnEmpty(GetMultipleCrefInfo(nav, "/member/exception"));
SeeAlsos = ToListNullOnEmpty(GetMultipleLinkInfo(nav, "/member/seealso"));
Examples = GetMultipleExampleNodes(nav, "/member/example").ToList();
Parameters = GetListContent(nav, "/member/param", "parameter", context);
TypeParameters = GetListContent(nav, "/member/typeparam", "type parameter", context);
// Nulls and empty list are treated differently in overwrite files:
// null values can be replaced, but empty list are merged by merge key
static List<T> ToListNullOnEmpty<T>(IEnumerable<T> items)
{
var list = items.ToList();
return list.Count == 0 ? null : list;
}
}
public static XmlComment Parse(string xml, XmlCommentParserContext context = null)
{
if (string.IsNullOrEmpty(xml)) return null;
// Quick turnaround for badly formed XML comment
if (xml.StartsWith("<!-- Badly formed XML comment ignored for member ", StringComparison.Ordinal))
{
Logger.LogWarning($"Invalid triple slash comment is ignored: {xml}");
return null;
}
try
{
return new XmlComment(xml, context ?? new());
}
catch (XmlException)
{
return null;
}
}
public string GetParameter(string name)
{
return Parameters.TryGetValue(name, out var value) ? value : null;
}
public string GetTypeParameter(string name)
{
return TypeParameters.TryGetValue(name, out var value) ? value : null;
}
private void ResolveCode(XDocument doc, XmlCommentParserContext context)
{
foreach (var node in doc.XPathSelectElements("//code").ToList())
{
if (node.Attribute("data-inline") is { } inlineAttribute)
{
inlineAttribute.Remove();
continue;
}
var indent = ((IXmlLineInfo)node).LinePosition - 2;
var (lang, value) = ResolveCodeSource(node, context);
value = TrimEachLine(value ?? node.Value, new(' ', indent));
var code = new XElement("code", value);
code.SetAttributeValue("class", $"lang-{lang ?? "csharp"}");
node.ReplaceWith(new XElement("pre", code));
}
}
private (string lang, string code) ResolveCodeSource(XElement node, XmlCommentParserContext context)
{
var source = node.Attribute("source")?.Value;
if (string.IsNullOrEmpty(source))
return default;
var lang = Path.GetExtension(source).TrimStart('.').ToLowerInvariant();
var code = context.ResolveCode?.Invoke(source);
if (code is null)
return (lang, null);
var region = node.Attribute("region")?.Value;
if (region is null)
return (lang, code);
var (regionRegex, endRegionRegex) = GetRegionRegex(source);
var builder = new StringBuilder();
var regionCount = 0;
foreach (var line in ReadLines(code))
{
if (!string.IsNullOrEmpty(region))
{
var match = regionRegex.Match(line);
if (match.Success)
{
var name = match.Groups[1].Value.Trim();
if (name == region)
{
++regionCount;
continue;
}
else if (regionCount > 0)
{
++regionCount;
}
}
else if (regionCount > 0 && endRegionRegex.IsMatch(line))
{
--regionCount;
if (regionCount == 0)
{
break;
}
}
if (regionCount > 0)
{
builder.AppendLine(line);
}
}
else
{
builder.AppendLine(line);
}
}
return (lang, builder.ToString());
}
private static IEnumerable<string> ReadLines(string text)
{
string line;
using var sr = new StringReader(text);
while ((line = sr.ReadLine()) != null)
{
yield return line;
}
}
private Dictionary<string, string> GetListContent(XPathNavigator navigator, string xpath, string contentType, XmlCommentParserContext context)
{
var iterator = navigator.Select(xpath);
var result = new Dictionary<string, string>();
if (iterator == null)
{
return result;
}
foreach (XPathNavigator nav in iterator)
{
string name = nav.GetAttribute("name", string.Empty);
string description = GetXmlValue(nav);
if (!string.IsNullOrEmpty(name))
{
if (result.ContainsKey(name))
{
string path = context.Source?.Remote != null ? Path.Combine(EnvironmentContext.BaseDirectory, context.Source.Remote.RelativePath) : context.Source?.Path;
Logger.LogWarning($"Duplicate {contentType} '{name}' found in comments, the latter one is ignored.", file: StringExtension.ToDisplayPath(path), line: context.Source?.StartLine.ToString());
}
else
{
result.Add(name, description);
}
}
}
return result;
}
private static (Regex, Regex) GetRegionRegex(String source)
{
var ext = Path.GetExtension(source);
switch (ext.ToUpper())
{
case ".XML":
case ".XAML":
case ".HTML":
case ".CSHTML":
case ".VBHTML":
return (XmlRegionRegex, XmlEndRegionRegex);
}
return (RegionRegex, EndRegionRegex);
}
private void ResolveLangword(XNode node)
{
foreach (var item in node.XPathSelectElements("//see[@langword]").ToList())
{
var langword = item.Attribute("langword").Value;
if (SymbolUrlResolver.GetLangwordUrl(langword) is { } href)
{
var a = new XElement("a", langword);
a.SetAttributeValue("href", href);
item.ReplaceWith(a);
}
else
{
var code = new XElement("code", langword);
code.SetAttributeValue("data-inline", "true");
item.ReplaceWith(code);
}
}
}
private void ResolveCrefLink(XNode node, string nodeSelector, Action<string, string> addReference)
{
if (node == null || string.IsNullOrEmpty(nodeSelector))
{
return;
}
var nodes = node.XPathSelectElements(nodeSelector + "[@cref]").ToList();
foreach (var item in nodes)
{
var cref = item.Attribute("cref").Value;
var success = false;
// Strict check is needed as value could be an invalid href,
// e.g. !:Dictionary<TKey, string> when user manually changed the intellisensed generic type
var match = CommentIdRegex.Match(cref);
if (match.Success)
{
var id = match.Groups["id"].Value;
var type = match.Groups["type"].Value;
if (type == "Overload")
{
id += '*';
}
// When see and seealso are top level nodes in triple slash comments, do not convert it into xref node
if (item.Parent?.Parent != null)
{
XElement replacement;
if (string.IsNullOrEmpty(item.Value))
{
replacement = XElement.Parse($"<xref href=\"{HttpUtility.UrlEncode(id)}\" data-throw-if-not-resolved=\"false\"></xref>");
}
else
{
replacement = XElement.Parse($"<xref href=\"{HttpUtility.UrlEncode(id)}?text={HttpUtility.UrlEncode(item.Value)}\" data-throw-if-not-resolved=\"false\"></xref>");
}
item.ReplaceWith(replacement);
}
addReference?.Invoke(id, cref);
success = true;
}
if (!success)
{
var detailedInfo = new StringBuilder();
if (_context != null && _context.Source != null)
{
if (!string.IsNullOrEmpty(_context.Source.Name))
{
detailedInfo.Append(" for ");
detailedInfo.Append(_context.Source.Name);
}
if (!string.IsNullOrEmpty(_context.Source.Path))
{
detailedInfo.Append(" defined in ");
detailedInfo.Append(_context.Source.Path);
detailedInfo.Append(" Line ");
detailedInfo.Append(_context.Source.StartLine);
}
}
Logger.Log(LogLevel.Warning, $"Invalid cref value \"{cref}\" found in XML documentation comment {detailedInfo}.");
if (cref.StartsWith("!:"))
{
item.ReplaceWith(cref.Substring(2));
}
}
}
}
private IEnumerable<string> GetMultipleExampleNodes(XPathNavigator navigator, string selector)
{
var iterator = navigator.Select(selector);
if (iterator == null)
{
yield break;
}
foreach (XPathNavigator nav in iterator)
{
yield return GetXmlValue(nav);
}
}
private IEnumerable<ExceptionInfo> GetMultipleCrefInfo(XPathNavigator navigator, string selector)
{
var iterator = navigator.Clone().Select(selector);
if (iterator == null)
{
yield break;
}
foreach (XPathNavigator nav in iterator)
{
string description = GetXmlValue(nav);
string commentId = nav.GetAttribute("cref", string.Empty);
string refId = nav.GetAttribute("refId", string.Empty);
if (!string.IsNullOrEmpty(refId))
{
yield return new ExceptionInfo
{
Description = description,
Type = refId,
CommentId = commentId
};
}
else if (!string.IsNullOrEmpty(commentId))
{
// Check if exception type is valid and trim prefix
var match = CommentIdRegex.Match(commentId);
if (match.Success)
{
var id = match.Groups["id"].Value;
var type = match.Groups["type"].Value;
if (type == "T")
{
yield return new ExceptionInfo
{
Description = description,
Type = id,
CommentId = commentId
};
}
}
}
}
}
private IEnumerable<LinkInfo> GetMultipleLinkInfo(XPathNavigator navigator, string selector)
{
var iterator = navigator.Clone().Select(selector);
if (iterator == null)
{
yield break;
}
foreach (XPathNavigator nav in iterator)
{
string altText = nav.InnerXml.Trim();
if (string.IsNullOrEmpty(altText))
{
altText = null;
}
string commentId = nav.GetAttribute("cref", string.Empty);
string url = nav.GetAttribute("href", string.Empty);
string refId = nav.GetAttribute("refId", string.Empty);
if (!string.IsNullOrEmpty(refId))
{
yield return new LinkInfo
{
AltText = altText,
LinkId = refId,
CommentId = commentId,
LinkType = LinkType.CRef
};
}
else if (!string.IsNullOrEmpty(commentId))
{
// Check if cref type is valid and trim prefix
var match = CommentIdRegex.Match(commentId);
if (match.Success)
{
var id = match.Groups["id"].Value;
var type = match.Groups["type"].Value;
if (type == "Overload")
{
id += '*';
}
yield return new LinkInfo
{
AltText = altText,
LinkId = id,
CommentId = commentId,
LinkType = LinkType.CRef
};
}
}
else if (!string.IsNullOrEmpty(url))
{
yield return new LinkInfo
{
AltText = altText ?? url,
LinkId = url,
LinkType = LinkType.HRef
};
}
}
}
private string GetSingleNodeValue(XPathNavigator nav, string selector)
{
return GetXmlValue(nav.Clone().SelectSingleNode(selector));
}
private string GetXmlValue(XPathNavigator node)
{
if (node is null)
return null;
if (_context.SkipMarkup)
return TrimEachLine(node.InnerXml);
return GetInnerXmlAsMarkdown(TrimEachLine(node.InnerXml));
}
private static string TrimEachLine(string text, string indent = "")
{
var minLeadingWhitespace = int.MaxValue;
var lines = ReadLines(text).ToList();
foreach (var line in lines)
{
if (string.IsNullOrWhiteSpace(line))
continue;
var leadingWhitespace = 0;
while (leadingWhitespace < line.Length && char.IsWhiteSpace(line[leadingWhitespace]))
leadingWhitespace++;
minLeadingWhitespace = Math.Min(minLeadingWhitespace, leadingWhitespace);
}
var builder = new StringBuilder();
// Trim leading empty lines
var trimStart = true;
// Apply indentation to all lines except the first,
// since the first new line in <pre></code> is significant
var firstLine = true;
foreach (var line in lines)
{
if (trimStart && string.IsNullOrWhiteSpace(line))
continue;
if (firstLine)
firstLine = false;
else
builder.Append(indent);
if (string.IsNullOrWhiteSpace(line))
{
builder.AppendLine();
continue;
}
trimStart = false;
builder.AppendLine(line.Substring(minLeadingWhitespace));
}
return builder.ToString().TrimEnd();
}
private static string GetInnerXmlAsMarkdown(string xml)
{
if (!xml.Contains('&'))
return xml;
xml = HandleBlockQuote(xml);
var markdown = Markdown.Parse(xml, trackTrivia: true);
DecodeMarkdownCode(markdown);
var sw = new StringWriter();
var rr = new RoundtripRenderer(sw);
rr.Write(markdown);
return sw.ToString();
static string HandleBlockQuote(string xml)
{
// > is encoded to > in XML. When interpreted as markdown, > is as blockquote
// Decode standalone > to > to enable the block quote markdown syntax
return Regex.Replace(xml, @"^(\s*)>", "$1>", RegexOptions.Multiline);
}
static void DecodeMarkdownCode(MarkdownObject node)
{
// CommonMark: Entity and numeric character references are treated as literal text in code spans and code blocks
switch (node)
{
case CodeInline codeInline:
codeInline.ContentWithTrivia = new(XmlDecode(codeInline.ContentWithTrivia.ToString()), codeInline.ContentWithTrivia.NewLine);
break;
case CodeBlock codeBlock:
var lines = new StringLineGroup(codeBlock.Lines.Count);
foreach (var line in codeBlock.Lines.Lines)
{
var newLine = line;
newLine.Slice = new(XmlDecode(line.Slice.ToString()), line.Slice.NewLine);
lines.Add(newLine);
}
codeBlock.Lines = lines;
break;
case ContainerBlock containerBlock:
foreach (var child in containerBlock)
DecodeMarkdownCode(child);
break;
case ContainerInline containerInline:
foreach (var child in containerInline)
DecodeMarkdownCode(child);
break;
case LeafBlock leafBlock when leafBlock.Inline is not null:
foreach (var child in leafBlock.Inline)
DecodeMarkdownCode(child);
break;
}
}
static string XmlDecode(string xml)
{
return xml
.Replace(">", ">")
.Replace("<", "<")
.Replace("&", "&")
.Replace(""", "\"")
.Replace("'", "'");
}
}
}