diff --git a/src/PortToDocs/src/app/PortToDocs.csproj b/src/PortToDocs/src/app/PortToDocs.csproj index 68cd2e7..514823f 100644 --- a/src/PortToDocs/src/app/PortToDocs.csproj +++ b/src/PortToDocs/src/app/PortToDocs.csproj @@ -7,7 +7,7 @@ enable true true - 1.2 + 1.3 diff --git a/src/PortToDocs/src/libraries/XmlHelper.cs b/src/PortToDocs/src/libraries/XmlHelper.cs index 8b75694..8a5cb69 100644 --- a/src/PortToDocs/src/libraries/XmlHelper.cs +++ b/src/PortToDocs/src/libraries/XmlHelper.cs @@ -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; @@ -120,6 +122,9 @@ internal class XmlHelper { @"\", @"`${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) @@ -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("")) + { + newValue.Append(""); + } + newValue.Append(s); + if (moreThanOne && !s.EndsWith("")) + { + newValue.Append(""); + } + } + + return newValue.ToString(); + } + public static string GetFormattedAsMarkdown(string value, bool isMember) { XElement xeFormat = new XElement("format"); diff --git a/src/PortToDocs/tests/PortToDocs.Strings.Tests.cs b/src/PortToDocs/tests/PortToDocs.Strings.Tests.cs index fc0f29f..d0b9df3 100644 --- a/src/PortToDocs/tests/PortToDocs.Strings.Tests.cs +++ b/src/PortToDocs/tests/PortToDocs.Strings.Tests.cs @@ -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 = @" + + + MyAssembly + + + + I am paragraph one. +I am paragraph number two. + I have no newlines. + + +"; + + string originalDocs = @" + + + MyAssembly + + + To be added. + To be added. + + +"; + + string expectedDocs = @" + + + MyAssembly + + + + I am paragraph one. + I am paragraph number two. + + I have no newlines. + + +"; + + 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 = @" + + + MyAssembly + + + + I am paragraph one. +I am paragraph number two. +I am paragraph number three. + + +"; + + string originalDocs = @" + + + MyAssembly + + + To be added. + To be added. + + +"; + + string expectedDocs = @" + + + MyAssembly + + + + I am paragraph one. + I am paragraph number two. + I am paragraph number three. + + To be added. + + +"; + + 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() { new StringTestData(originalDocsFile, expectedDocsFile) }, configuration); diff --git a/src/PortToDocs/tests/TestData/Exception_ExistingCref/xml_expected/MyAssembly/MyType.xml b/src/PortToDocs/tests/TestData/Exception_ExistingCref/xml_expected/MyAssembly/MyType.xml index 26a5ed9..6c311a5 100644 --- a/src/PortToDocs/tests/TestData/Exception_ExistingCref/xml_expected/MyAssembly/MyType.xml +++ b/src/PortToDocs/tests/TestData/Exception_ExistingCref/xml_expected/MyAssembly/MyType.xml @@ -1,4 +1,4 @@ - + MyAssembly @@ -32,11 +32,11 @@ Word1 Word2 Word3 Word4 Word5 Word6 Word7 Word8. Word1 Word2 Word3 Word4 Word5 Word6 Word7. Word1 Word2 Word3 Word4 Word5 Word6. - Word1 Word2 Word3 Word4 Word5. - --or- - -Word1 Word2 Word3 Word4 Word5 Word6 Word7 Word8 Word9 Word10. + + Word1 Word2 Word3 Word4 Word5. + -or- + Word1 Word2 Word3 Word4 Word5 Word6 Word7 Word8 Word9 Word10. + diff --git a/src/PortToDocs/tests/TestData/Exceptions/xml_expected/MyAssembly/MyType.xml b/src/PortToDocs/tests/TestData/Exceptions/xml_expected/MyAssembly/MyType.xml index 5e1b5dc..b0088df 100644 --- a/src/PortToDocs/tests/TestData/Exceptions/xml_expected/MyAssembly/MyType.xml +++ b/src/PortToDocs/tests/TestData/Exceptions/xml_expected/MyAssembly/MyType.xml @@ -1,4 +1,4 @@ - + MyAssembly @@ -29,19 +29,15 @@ This is the method summary. To be added. This is the original text of ArgumentNullException thrown for MyMethod. - This is the original text of IndexOutOfRangeException thrown for MyMethod. - --or- - -A proper alternative. - --or- - -An improper alternative. - --or- - -A somewhat proper alternative. + + This is the original text of IndexOutOfRangeException thrown for MyMethod. + -or- + A proper alternative. + -or- + An improper alternative. + -or- + A somewhat proper alternative. +