Skip to content

Commit

Permalink
Convert paragraphs divided by newlines into para items.
Browse files Browse the repository at this point in the history
  • Loading branch information
carlossanlop committed Aug 6, 2024
1 parent 7171357 commit 278d07b
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/PortToDocs/src/app/PortToDocs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Nullable>enable</Nullable>
<IsPackable>true</IsPackable>
<PackAsTool>true</PackAsTool>
<Version>1.2</Version>
<Version>1.3</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
30 changes: 29 additions & 1 deletion src/PortToDocs/src/libraries/XmlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
Expand Down Expand Up @@ -120,6 +122,9 @@ internal class XmlHelper
{ @"\<see langword\=""(?'seeLangwordContents'[a-zA-Z0-9_\-]+)""[ ]*\/\>", @"`${seeLangwordContents}`" },
};

private static readonly string[] _splittingSeparators = new string[] { "\r", "\n", "\r\n" };
private static readonly StringSplitOptions _splittingStringSplitOptions = StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries;

public static string GetAttributeValue(XElement parent, string name)
{
if (parent == null)
Expand Down Expand Up @@ -182,12 +187,35 @@ public static string GetNodesInPlainText(XElement element)

public static string GetFormattedAsXml(string value, bool removeUndesiredEndlines)
{
string updatedValue = removeUndesiredEndlines ? RemoveUndesiredEndlines(value) : value;
string updatedValue = ReplaceEndLinesWithParas(value);
updatedValue = removeUndesiredEndlines ? RemoveUndesiredEndlines(updatedValue) : updatedValue;
updatedValue = ReplaceNormalElementPatterns(updatedValue);
updatedValue = SubstituteRegexPatterns(updatedValue, _replaceableNormalElementRegexPatterns);
return updatedValue;
}

private static string ReplaceEndLinesWithParas(string updatedValue)
{
string[] splitted = updatedValue.Split(_splittingSeparators, _splittingStringSplitOptions);
bool moreThanOne = splitted.Count() > 1;

StringBuilder newValue = new();
foreach (string s in splitted)
{
if (moreThanOne && !s.StartsWith("<para>"))
{
newValue.Append("<para>");
}
newValue.Append(s);
if (moreThanOne && !s.EndsWith("</para>"))
{
newValue.Append("</para>");
}
}

return newValue.ToString();
}

public static string GetFormattedAsMarkdown(string value, bool isMember)
{
XElement xeFormat = new XElement("format");
Expand Down
111 changes: 111 additions & 0 deletions src/PortToDocs/tests/PortToDocs.Strings.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2355,6 +2355,117 @@ public void Preserve_Para()
TestWithStrings(originalIntellisense, originalDocs, expectedDocs, configuration);
}

[Fact]
public void Convert_NewLines_To_Para()
{
// Convert triple slash new lines to para xml items.

string originalIntellisense = @"<?xml version=""1.0""?>
<doc>
<assembly>
<name>MyAssembly</name>
</assembly>
<members>
<member name=""T:MyNamespace.MyType"">
<summary>I am paragraph one.
I am paragraph number two.</summary>
<remarks>I have no newlines.</remarks>
</member>
</members>
</doc>";

string originalDocs = @"<Type Name=""MyType"" FullName=""MyNamespace.MyType"">
<TypeSignature Language=""DocId"" Value=""T:MyNamespace.MyType"" />
<AssemblyInfo>
<AssemblyName>MyAssembly</AssemblyName>
</AssemblyInfo>
<Docs>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
<Members></Members>
</Type>";

string expectedDocs = @"<Type Name=""MyType"" FullName=""MyNamespace.MyType"">
<TypeSignature Language=""DocId"" Value=""T:MyNamespace.MyType"" />
<AssemblyInfo>
<AssemblyName>MyAssembly</AssemblyName>
</AssemblyInfo>
<Docs>
<summary>
<para>I am paragraph one.</para>
<para>I am paragraph number two.</para>
</summary>
<remarks>I have no newlines.</remarks>
</Docs>
<Members></Members>
</Type>";

Configuration configuration = new()
{
MarkdownRemarks = false
};
configuration.IncludedAssemblies.Add(FileTestData.TestAssembly);

TestWithStrings(originalIntellisense, originalDocs, expectedDocs, configuration);
}

[Fact]
public void Convert_NewLines_To_Para_Preserve_Existing_Para()
{
// Convert triple slash new lines to para xml items. If there are paras too, keep them.

string originalIntellisense = @"<?xml version=""1.0""?>
<doc>
<assembly>
<name>MyAssembly</name>
</assembly>
<members>
<member name=""T:MyNamespace.MyType"">
<summary><para>I am paragraph one.</para>
I am paragraph number two.
I am paragraph number three.</summary>
</member>
</members>
</doc>";

string originalDocs = @"<Type Name=""MyType"" FullName=""MyNamespace.MyType"">
<TypeSignature Language=""DocId"" Value=""T:MyNamespace.MyType"" />
<AssemblyInfo>
<AssemblyName>MyAssembly</AssemblyName>
</AssemblyInfo>
<Docs>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
<Members></Members>
</Type>";

string expectedDocs = @"<Type Name=""MyType"" FullName=""MyNamespace.MyType"">
<TypeSignature Language=""DocId"" Value=""T:MyNamespace.MyType"" />
<AssemblyInfo>
<AssemblyName>MyAssembly</AssemblyName>
</AssemblyInfo>
<Docs>
<summary>
<para>I am paragraph one.</para>
<para>I am paragraph number two.</para>
<para>I am paragraph number three.</para>
</summary>
<remarks>To be added.</remarks>
</Docs>
<Members></Members>
</Type>";

Configuration configuration = new()
{
MarkdownRemarks = true
};
configuration.IncludedAssemblies.Add(FileTestData.TestAssembly);

TestWithStrings(originalIntellisense, originalDocs, expectedDocs, configuration);
}

private static void TestWithStrings(string intellisenseFile, string originalDocsFile, string expectedDocsFile, Configuration configuration) =>
TestWithStrings(intellisenseFile, new List<StringTestData>() { new StringTestData(originalDocsFile, expectedDocsFile) }, configuration);

Expand Down

0 comments on commit 278d07b

Please sign in to comment.