Skip to content

Commit 467397b

Browse files
authored
Fix more <para> cases (#173)
* Improve the logic that wraps paragraphs with <para></para> - Join paragraphs separated by newlines. - Split paragraphs that end in "." or ":". - Treat "-or-" and "- or -" as their own paragraphs. - Make sure single paragraphs are not wrapped. - Make sure empty newlines are ignored. * Tests * Version bump.
1 parent a471c51 commit 467397b

File tree

3 files changed

+108
-10
lines changed

3 files changed

+108
-10
lines changed

src/PortToDocs/src/app/PortToDocs.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<Nullable>enable</Nullable>
88
<IsPackable>true</IsPackable>
99
<PackAsTool>true</PackAsTool>
10-
<Version>1.4</Version>
10+
<Version>1.5</Version>
1111
</PropertyGroup>
1212

1313
<ItemGroup>

src/PortToDocs/src/libraries/XmlHelper.cs

+45-7
Original file line numberDiff line numberDiff line change
@@ -203,19 +203,57 @@ public static string GetFormattedAsXml(string value, bool removeUndesiredEndline
203203
private static string ReplaceEndLinesWithParas(string updatedValue)
204204
{
205205
string[] splitted = updatedValue.Split(_splittingSeparators, _splittingStringSplitOptions);
206-
bool moreThanOne = splitted.Count() > 1;
206+
207+
if (splitted.Length == 1)
208+
{
209+
// No need to add paras
210+
return splitted[0];
211+
}
207212

208213
StringBuilder newValue = new();
209-
foreach (string s in splitted)
214+
ReadOnlySpan<char> ros;
215+
bool addStartParaNext = true;
216+
for (int i = 0; i < splitted.Length; i++)
210217
{
211-
if (moreThanOne && !s.StartsWith("<para>"))
218+
ros = splitted[i];
219+
220+
if (ros.StartsWith("<para>") && ros.EndsWith("</para>"))
212221
{
213-
newValue.Append("<para>");
222+
// No change
223+
newValue.Append(ros);
224+
// Next time we find a line not surrounded by paras, we need to add the starting one first
225+
addStartParaNext = true;
214226
}
215-
newValue.Append(s);
216-
if (moreThanOne && !s.EndsWith("</para>"))
227+
else
217228
{
218-
newValue.Append("</para>");
229+
if (addStartParaNext)
230+
{
231+
newValue.Append("<para>");
232+
addStartParaNext = false;
233+
}
234+
235+
if (splitted[i].Equals("- or -"))
236+
{
237+
newValue.Append("-or-");
238+
}
239+
else
240+
{
241+
newValue.Append(ros);
242+
}
243+
if (!ros.EndsWith("</para>"))
244+
{
245+
if (ros[^1] is '.' or ':' || (i + 1) >= splitted.Length || splitted[i].Equals("-or-") || splitted[i].Equals("- or -"))
246+
{
247+
// We're done with this line
248+
newValue.Append("</para>");
249+
addStartParaNext = true;
250+
}
251+
else
252+
{
253+
// Space separator, the next line will get appended next
254+
newValue.Append(' ');
255+
}
256+
}
219257
}
220258
}
221259

src/PortToDocs/tests/PortToDocs.Strings.Tests.cs

+62-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Text;
67
using System.Xml.Linq;
@@ -2466,6 +2467,67 @@ I am paragraph number three.</summary>
24662467
TestWithStrings(originalIntellisense, originalDocs, expectedDocs, configuration);
24672468
}
24682469

2470+
[Fact]
2471+
public void Para_EdgeCases()
2472+
{
2473+
// Convert triple slash new lines to para xml items. If there are paras too, keep them.
2474+
2475+
string originalIntellisense = @"<?xml version=""1.0""?>
2476+
<doc>
2477+
<assembly>
2478+
<name>MyAssembly</name>
2479+
</assembly>
2480+
<members>
2481+
<member name=""T:MyNamespace.MyType"">
2482+
<summary><para>I am paragraph one with pre-existing paras.</para>
2483+
I am paragraph number two but I am
2484+
divided into two lines. I am paragraph three but I am in the same line, my spacing between lines should be ignored
2485+
and the next newlines should be ignored.
2486+
2487+
2488+
<para>I am the fourth paragraph with pre-existing paras.</para>
2489+
</summary>
2490+
</member>
2491+
</members>
2492+
</doc>";
2493+
2494+
string originalDocs = @"<Type Name=""MyType"" FullName=""MyNamespace.MyType"">
2495+
<TypeSignature Language=""DocId"" Value=""T:MyNamespace.MyType"" />
2496+
<AssemblyInfo>
2497+
<AssemblyName>MyAssembly</AssemblyName>
2498+
</AssemblyInfo>
2499+
<Docs>
2500+
<summary>To be added.</summary>
2501+
<remarks>To be added.</remarks>
2502+
</Docs>
2503+
<Members></Members>
2504+
</Type>";
2505+
2506+
string expectedDocs = @"<Type Name=""MyType"" FullName=""MyNamespace.MyType"">
2507+
<TypeSignature Language=""DocId"" Value=""T:MyNamespace.MyType"" />
2508+
<AssemblyInfo>
2509+
<AssemblyName>MyAssembly</AssemblyName>
2510+
</AssemblyInfo>
2511+
<Docs>
2512+
<summary>
2513+
<para>I am paragraph one with pre-existing paras.</para>
2514+
<para>I am paragraph number two but I am divided into two lines. I am paragraph three but I am in the same line, my spacing between lines should be ignored and the next newlines should be ignored.</para>
2515+
<para>I am the fourth paragraph with pre-existing paras.</para>
2516+
</summary>
2517+
<remarks>To be added.</remarks>
2518+
</Docs>
2519+
<Members></Members>
2520+
</Type>";
2521+
2522+
Configuration configuration = new()
2523+
{
2524+
MarkdownRemarks = true
2525+
};
2526+
configuration.IncludedAssemblies.Add(FileTestData.TestAssembly);
2527+
2528+
TestWithStrings(originalIntellisense, originalDocs, expectedDocs, configuration);
2529+
}
2530+
24692531
[Fact]
24702532
public void Convert_CodeDataDevCommentType_To_ExpectedElementNames()
24712533
{
@@ -2619,6 +2681,4 @@ private static void TestWithStrings(List<string> intellisenseFiles, List<StringT
26192681
}
26202682
}
26212683
}
2622-
2623-
26242684
}

0 commit comments

Comments
 (0)