-
Notifications
You must be signed in to change notification settings - Fork 868
/
TripleSlashCommentModel.cs
820 lines (740 loc) · 30.8 KB
/
TripleSlashCommentModel.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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.DocAsCode.Metadata.ManagedReference
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using Microsoft.DocAsCode.Common;
using Microsoft.DocAsCode.Plugins;
using Microsoft.DocAsCode.DataContracts.ManagedReference;
using System.Globalization;
public class TripleSlashCommentModel
{
private const string idSelector = @"((?![0-9])[\w_])+[\w\(\)\.\{\}\[\]\|\*\^~#@!`,_<>:]*";
private static Regex CommentIdRegex = new Regex(@"^(?<type>N|T|M|P|F|E|Overload):(?<id>" + idSelector + ")$", RegexOptions.Compiled);
private static Regex LineBreakRegex = new Regex(@"\r?\n", RegexOptions.Compiled);
private static Regex CodeElementRegex = new Regex(@"<code[^>]*>([\s\S]*?)</code>", RegexOptions.Compiled);
private static Regex RegionRegex = new Regex(@"^\s*#region\s*(.*)$");
private static Regex EndRegionRegex = new Regex(@"^\s*#endregion\s*.*$");
private readonly ITripleSlashCommentParserContext _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> Sees { 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; }
public bool IsInheritDoc { get; private set; }
private TripleSlashCommentModel(string xml, SyntaxLanguage language, ITripleSlashCommentParserContext context)
{
// Transform triple slash comment
XDocument doc = TripleSlashCommentTransformer.Transform(xml, language);
_context = context;
if (!context.PreserveRawInlineComments)
{
ResolveSeeCref(doc, context.AddReferenceDelegate, context.ResolveCRef);
ResolveSeeAlsoCref(doc, context.AddReferenceDelegate, context.ResolveCRef);
ResolveExceptionCref(doc, context.AddReferenceDelegate, context.ResolveCRef);
}
ResolveCodeSource(doc, context);
var nav = doc.CreateNavigator();
Summary = GetSummary(nav, context);
Remarks = GetRemarks(nav, context);
Returns = GetReturns(nav, context);
Exceptions = GetExceptions(nav, context);
Sees = GetSees(nav, context);
SeeAlsos = GetSeeAlsos(nav, context);
Examples = GetExamples(nav, context);
Parameters = GetParameters(nav, context);
TypeParameters = GetTypeParameters(nav, context);
IsInheritDoc = GetIsInheritDoc(nav, context);
}
public static TripleSlashCommentModel CreateModel(string xml, SyntaxLanguage language, ITripleSlashCommentParserContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (string.IsNullOrEmpty(xml)) return null;
// Quick turnaround for badly formed XML comment
if (xml.StartsWith("<!-- Badly formed XML comment ignored for member "))
{
Logger.LogWarning($"Invalid triple slash comment is ignored: {xml}");
return null;
}
try
{
var model = new TripleSlashCommentModel(xml, language, context);
return model;
}
catch (XmlException)
{
return null;
}
}
public void CopyInheritedData(TripleSlashCommentModel src)
{
if (src == null)
{
throw new ArgumentNullException(nameof(src));
}
Summary = Summary ?? src.Summary;
Remarks = Remarks ?? src.Remarks;
Returns = Returns ?? src.Returns;
if (Exceptions == null && src.Exceptions != null)
{
Exceptions = src.Exceptions.Select(e => e.Clone()).ToList();
}
if (Sees == null && src.Sees != null)
{
Sees = src.Sees.Select(s => s.Clone()).ToList();
}
if (SeeAlsos == null && src.SeeAlsos != null)
{
SeeAlsos = src.SeeAlsos.Select(s => s.Clone()).ToList();
}
if (Examples == null && src.Examples != null)
{
Examples = new List<string>(src.Examples);
}
if (Parameters == null && src.Parameters != null)
{
Parameters = new Dictionary<string, string>(src.Parameters);
}
if (TypeParameters == null && src.TypeParameters != null)
{
TypeParameters = new Dictionary<string, string>(src.TypeParameters);
}
}
public string GetParameter(string name)
{
if (string.IsNullOrEmpty(name))
{
return null;
}
return GetValue(name, Parameters);
}
public string GetTypeParameter(string name)
{
if (string.IsNullOrEmpty(name))
{
return null;
}
return GetValue(name, TypeParameters);
}
private static string GetValue(string name, Dictionary<string, string> dictionary)
{
if (dictionary == null)
{
return null;
}
if (dictionary.TryGetValue(name, out string description))
{
return description;
}
return null;
}
/// <summary>
/// Get summary node out from triple slash comments
/// </summary>
/// <param name="xml"></param>
/// <param name="normalize"></param>
/// <returns></returns>
/// <example>
/// <code> <see cref="Hello"/></code>
/// </example>
private string GetSummary(XPathNavigator nav, ITripleSlashCommentParserContext context)
{
// Resolve <see cref> to @ syntax
// Also support <seealso cref>
string selector = "/member/summary";
return GetSingleNodeValue(nav, selector);
}
/// <summary>
/// Get remarks node out from triple slash comments
/// </summary>
/// <remarks>
/// <para>This is a sample of exception node</para>
/// </remarks>
/// <param name="xml"></param>
/// <param name="normalize"></param>
/// <returns></returns>
private string GetRemarks(XPathNavigator nav, ITripleSlashCommentParserContext context)
{
string selector = "/member/remarks";
return GetSingleNodeValue(nav, selector);
}
private string GetReturns(XPathNavigator nav, ITripleSlashCommentParserContext context)
{
// Resolve <see cref> to @ syntax
// Also support <seealso cref>
string selector = "/member/returns";
return GetSingleNodeValue(nav, selector);
}
/// <summary>
/// Get exceptions nodes out from triple slash comments
/// </summary>
/// <param name="xml"></param>
/// <param name="normalize"></param>
/// <returns></returns>
/// <exception cref="XmlException">This is a sample of exception node</exception>
private List<ExceptionInfo> GetExceptions(XPathNavigator nav, ITripleSlashCommentParserContext context)
{
string selector = "/member/exception";
var result = GetMulitpleCrefInfo(nav, selector).ToList();
if (result.Count == 0)
{
return null;
}
return result;
}
/// <summary>
/// To get `see` tags out
/// </summary>
/// <param name="xml"></param>
/// <param name="context"></param>
/// <returns></returns>
/// <see cref="SpecIdHelper"/>
/// <see cref="SourceSwitch"/>
private List<LinkInfo> GetSees(XPathNavigator nav, ITripleSlashCommentParserContext context)
{
var result = GetMultipleLinkInfo(nav, "/member/see").ToList();
if (result.Count == 0)
{
return null;
}
return result;
}
/// <summary>
/// To get `seealso` tags out
/// </summary>
/// <param name="xml"></param>
/// <param name="context"></param>
/// <returns></returns>
/// <seealso cref="WaitForChangedResult"/>
/// <seealso cref="http://google.com">ABCS</seealso>
private List<LinkInfo> GetSeeAlsos(XPathNavigator nav, ITripleSlashCommentParserContext context)
{
var result = GetMultipleLinkInfo(nav, "/member/seealso").ToList();
if (result.Count == 0)
{
return null;
}
return result;
}
/// <summary>
/// To get `example` tags out
/// </summary>
/// <param name="xml"></param>
/// <param name="context"></param>
/// <returns></returns>
/// <example>
/// This sample shows how to call the <see cref="GetExceptions(string, ITripleSlashCommentParserContext)"/> method.
/// <code>
/// class TestClass
/// {
/// static int Main()
/// {
/// return GetExceptions(null, null).Count();
/// }
/// }
/// </code>
/// </example>
private List<string> GetExamples(XPathNavigator nav, ITripleSlashCommentParserContext context)
{
// Resolve <see cref> to @ syntax
// Also support <seealso cref>
return GetMultipleExampleNodes(nav, "/member/example").ToList();
}
private bool GetIsInheritDoc(XPathNavigator nav, ITripleSlashCommentParserContext context)
{
var node = nav.SelectSingleNode("/member/inheritdoc");
if (node == null)
{
return false;
}
if (node.HasAttributes)
{
//The Sandcastle implementation of <inheritdoc /> supports two attributes: 'cref' and 'select'.
//These attributes allow changing the source of the inherited doc and controlling what is inherited.
//Until these attributes are supported, ignoring inheritdoc elements with attributes, so as not to misinterpret them.
Logger.LogWarning("Attributes on <inheritdoc /> elements are not supported; inheritdoc element will be ignored.");
return false;
}
return true;
}
private void ResolveCodeSource(XDocument doc, ITripleSlashCommentParserContext context)
{
foreach (XElement node in doc.XPathSelectElements("//code"))
{
var source = node.Attribute("source");
if (source == null || string.IsNullOrEmpty(source.Value))
{
continue;
}
if (context.Source == null || string.IsNullOrEmpty(context.Source.Path))
{
Logger.LogWarning($"Unable to get source file path for {node.ToString()}");
return;
}
var region = node.Attribute("region");
var path = source.Value;
if (!Path.IsPathRooted(path))
{
var basePath = !string.IsNullOrEmpty(context.CodeSourceBasePath) ? context.CodeSourceBasePath : Path.GetDirectoryName(Path.Combine(EnvironmentContext.BaseDirectory, context.Source.Path));
path = Path.Combine(basePath, path);
}
ResolveCodeSource(node, path, region?.Value);
}
}
private void ResolveCodeSource(XElement element, string source, string region)
{
if (!File.Exists(source))
{
Logger.LogWarning($"Source file '{source}' not found.");
return;
}
var builder = new StringBuilder();
var regionCount = 0;
foreach (var line in File.ReadLines(source))
{
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);
}
}
element.SetValue(builder.ToString());
}
private Dictionary<string, string> GetListContent(XPathNavigator navigator, string xpath, string contentType, ITripleSlashCommentParserContext 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 Dictionary<string, string> GetParameters(XPathNavigator navigator, ITripleSlashCommentParserContext context)
{
return GetListContent(navigator, "/member/param", "parameter", context);
}
private Dictionary<string, string> GetTypeParameters(XPathNavigator navigator, ITripleSlashCommentParserContext context)
{
return GetListContent(navigator, "/member/typeparam", "type parameter", context);
}
private void ResolveSeeAlsoCref(XNode node, Action<string, string> addReference, Func<string, CRefTarget> resolveCRef)
{
// Resolve <see cref> to <xref>
ResolveCrefLink(node, "//seealso[@cref]", addReference, resolveCRef);
}
private void ResolveSeeCref(XNode node, Action<string, string> addReference, Func<string, CRefTarget> resolveCRef)
{
// Resolve <see cref> to <xref>
ResolveCrefLink(node, "//see[@cref]", addReference, resolveCRef);
}
private void ResolveExceptionCref(XNode node, Action<string, string> addReference, Func<string, CRefTarget> resolveCRef)
{
ResolveCrefLink(node, "//exception[@cref]", addReference, resolveCRef);
}
private void ResolveCrefLink(XNode node, string nodeSelector, Action<string, string> addReference, Func<string, CRefTarget> resolveCRef)
{
if (node == null || string.IsNullOrEmpty(nodeSelector))
{
return;
}
try
{
var nodes = node.XPathSelectElements(nodeSelector + "[@cref]").ToList();
foreach (var item in nodes)
{
var cref = item.Attribute("cref").Value;
var success = false;
if (resolveCRef != null)
{
// The resolveCRef delegate resolves the cref and returns the name of a reference if successful.
var cRefTarget = resolveCRef.Invoke(cref);
if (cRefTarget != null)
{
if (item.Parent?.Parent == null)
{
// <see> or <seealso> is top-level tag. Keep it, but set resolved references.
item.SetAttributeValue("refId", cRefTarget.Id);
item.SetAttributeValue("cref", cRefTarget.CommentId);
}
else
{
// <see> occurs in text. Replace it with an <xref> node using the resolved reference.
var replacement = XElement.Parse($"<xref href=\"{HttpUtility.UrlEncode(cRefTarget.Id)}\" data-throw-if-not-resolved=\"false\"></xref>");
item.ReplaceWith(replacement);
}
success = true;
}
else
{
item.Remove();
success = false;
}
}
else
{
// 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)
{
var replacement = XElement.Parse($"<xref href=\"{HttpUtility.UrlEncode(id)}\" 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 triple-slash-comments{detailedInfo}, ignored.");
}
}
}
catch
{
}
}
private IEnumerable<string> GetMultipleExampleNodes(XPathNavigator navigator, string selector)
{
var iterator = navigator.Select(selector);
if (iterator == null)
{
yield break;
}
foreach (XPathNavigator nav in iterator)
{
string description = GetXmlValue(nav);
yield return description;
}
}
private IEnumerable<ExceptionInfo> GetMulitpleCrefInfo(XPathNavigator navigator, string selector)
{
var iterator = navigator.Clone().Select(selector);
if (iterator == null)
{
yield break;
}
foreach (XPathNavigator nav in iterator)
{
string description = GetXmlValue(nav);
if (string.IsNullOrEmpty(description))
{
description = null;
}
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 = GetXmlValue(nav);
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)
{
var node = nav.Clone().SelectSingleNode(selector);
if (node == null)
{
// throw new ArgumentException(selector + " is not found");
return null;
}
else
{
return GetXmlValue(node);
}
}
private string GetXmlValue(XPathNavigator node)
{
// NOTE: use node.InnerXml instead of node.Value, to keep decorative nodes,
// e.g.
// <remarks><para>Value</para></remarks>
// decode InnerXml as it encodes
// IXmlLineInfo.LinePosition starts from 1 and it would ignore '<'
// e.g.
// <summary/> the LinePosition is the column number of 's', so it should be minus 2
var lineInfo = node as IXmlLineInfo;
int column = lineInfo.HasLineInfo() ? lineInfo.LinePosition - 2 : 0;
return NormalizeXml(RemoveLeadingSpaces(GetInnerXml(node)), column);
}
/// <summary>
/// Remove least common whitespces in each line of xml
/// </summary>
/// <param name="xml"></param>
/// <returns>xml after removing least common whitespaces</returns>
private static string RemoveLeadingSpaces(string xml)
{
var lines = LineBreakRegex.Split(xml);
var normalized = new List<string>();
var preIndex = 0;
var leadingSpaces = from line in lines
where !string.IsNullOrWhiteSpace(line)
select line.TakeWhile(char.IsWhiteSpace).Count();
if (leadingSpaces.Any())
{
preIndex = leadingSpaces.Min();
}
if (preIndex == 0)
{
return xml;
}
foreach (var line in lines)
{
if (string.IsNullOrWhiteSpace(line))
{
normalized.Add(string.Empty);
}
else
{
normalized.Add(line.Substring(preIndex));
}
}
return string.Join("\n", normalized);
}
/// <summary>
/// Split xml into lines. Trim meaningless whitespaces.
/// if a line starts with xml node, all leading whitespaces would be trimmed
/// otherwise text node start position always aligns with the start position of its parent line(the last previous line that starts with xml node)
/// Trim newline character for code element.
/// </summary>
/// <param name="xml"></param>
/// <param name="parentIndex">the start position of the last previous line that starts with xml node</param>
/// <returns>normalized xml</returns>
private static string NormalizeXml(string xml, int parentIndex)
{
var lines = LineBreakRegex.Split(xml);
var normalized = new List<string>();
foreach (var line in lines)
{
if (string.IsNullOrWhiteSpace(line))
{
normalized.Add(string.Empty);
}
else
{
// TO-DO: special logic for TAB case
int index = line.TakeWhile(char.IsWhiteSpace).Count();
if (line[index] == '<')
{
parentIndex = index;
}
normalized.Add(line.Substring(Math.Min(parentIndex, index)));
}
}
// trim newline character for code element
return CodeElementRegex.Replace(
string.Join("\n", normalized),
m =>
{
var group = m.Groups[1];
if (group.Length == 0)
{
return m.Value;
}
return m.Value.Replace(group.ToString(), group.ToString().Trim('\n'));
});
}
/// <summary>
/// `>` is always encoded to `>` in XML, when triple-slash-comments is considered as Markdown content, `>` is considered as blockquote
/// Decode `>` to enable the Markdown syntax considering `>` is not a Must-Encode in Text XElement
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
private static string GetInnerXml(XPathNavigator node)
{
using (var sw = new StringWriter(CultureInfo.InvariantCulture))
{
using (var tw = new XmlWriterWithGtDecoded(sw))
{
if (node.MoveToFirstChild())
{
do
{
tw.WriteNode(node, true);
} while (node.MoveToNext());
node.MoveToParent();
}
}
return sw.ToString();
}
}
private sealed class XmlWriterWithGtDecoded : XmlTextWriter
{
public XmlWriterWithGtDecoded(TextWriter tw) : base(tw) { }
public XmlWriterWithGtDecoded(Stream w, Encoding encoding) : base(w, encoding) { }
public override void WriteString(string text)
{
var encoded = text.Replace("&", "&").Replace("<", "<").Replace("'", "'").Replace("\"", """);
WriteRaw(encoded);
}
}
}
}