Skip to content

Commit

Permalink
Replace br tags in XML comments with new lines (#2899)
Browse files Browse the repository at this point in the history
Replace any `<br>` tags with a new line instead.
  • Loading branch information
mburumaxwell authored May 16, 2024
1 parent 77cde47 commit 828b7df
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static string Humanize(string text)
.HumanizeCodeTags()
.HumanizeMultilineCodeTags()
.HumanizeParaTags()
.HumanizeBrTags() // must be called after HumanizeParaTags() so that it replaces any additional <br> tags
.DecodeXml();
}

Expand Down Expand Up @@ -108,6 +109,11 @@ private static string HumanizeParaTags(this string text)
return ParaTag().Replace(text, (match) => "<br>" + match.Groups["display"].Value);
}

private static string HumanizeBrTags(this string text)
{
return BrTag().Replace(text, m => Environment.NewLine);
}

private static string DecodeXml(this string text)
{
return WebUtility.HtmlDecode(text);
Expand All @@ -118,6 +124,7 @@ private static string DecodeXml(this string text)
private const string MultilineCodeTagPattern = @"<code>(?<display>.+?)</code>";
private const string ParaTagPattern = @"<para>(?<display>.+?)</para>";
private const string HrefPattern = @"<see href=\""(.*)\"">(.*)<\/see>";
private const string BrPattern = @"(<br ?\/?>)"; // handles <br>, <br/>, <br />

#if NET7_0_OR_GREATER
[GeneratedRegex(RefTagPattern)]
Expand All @@ -134,18 +141,23 @@ private static string DecodeXml(this string text)

[GeneratedRegex(HrefPattern)]
private static partial Regex HrefTag();

[GeneratedRegex(BrPattern)]
private static partial Regex BrTag();
#else
private static readonly Regex _refTag = new(RefTagPattern);
private static readonly Regex _codeTag = new(CodeTagPattern);
private static readonly Regex _multilineCodeTag = new(MultilineCodeTagPattern, RegexOptions.Singleline);
private static readonly Regex _paraTag = new(ParaTagPattern, RegexOptions.Singleline);
private static readonly Regex _hrefTag = new(HrefPattern);
private static readonly Regex _brTag = new(BrPattern);

private static Regex RefTag() => _refTag;
private static Regex CodeTag() => _codeTag;
private static Regex MultilineCodeTag() => _multilineCodeTag;
private static Regex ParaTag() => _paraTag;
private static Regex HrefTag() => _hrefTag;
private static Regex BrTag() => _brTag;
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ Misplaced Tab Indentation
[InlineData(@"<paramref name=""param1"" /> does something", "param1 does something")]
[InlineData("<c>DoWork</c> is a method in <c>TestClass</c>.", "`DoWork` is a method in `TestClass`.")]
[InlineData("<code>DoWork</code> is a method in <code>\nTestClass\n</code>.", "```DoWork``` is a method in ```\nTestClass\n```.")]
[InlineData("<para>This is a paragraph</para>.", "<br>This is a paragraph.")]
[InlineData("<para>This is a paragraph</para>.", "\r\nThis is a paragraph.")]
[InlineData("GET /Todo?iscomplete=true&amp;owner=mike", "GET /Todo?iscomplete=true&owner=mike")]
[InlineData(@"Returns a <see langword=""null""/> item.", "Returns a null item.")]
[InlineData(@"<see href=""https://www.iso.org/iso-4217-currency-codes.html"">ISO currency code</see>", "[ISO currency code](https://www.iso.org/iso-4217-currency-codes.html)")]
[InlineData("First line.<br />Second line.<br/>Third line.<br>Fourth line.", "First line.\r\nSecond line.\r\nThird line.\r\nFourth line.")]
public void Humanize_HumanizesInlineTags(
string input,
string expectedOutput)
Expand Down

0 comments on commit 828b7df

Please sign in to comment.