From 278d07b636cfc74f2b0d7b6fe986ea03eb54ea4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20S=C3=A1nchez=20L=C3=B3pez?= <1175054+carlossanlop@users.noreply.github.com> Date: Tue, 6 Aug 2024 10:50:34 -0700 Subject: [PATCH 1/2] Convert paragraphs divided by newlines into para items. --- src/PortToDocs/src/app/PortToDocs.csproj | 2 +- src/PortToDocs/src/libraries/XmlHelper.cs | 30 ++++- .../tests/PortToDocs.Strings.Tests.cs | 111 ++++++++++++++++++ 3 files changed, 141 insertions(+), 2 deletions(-) diff --git a/src/PortToDocs/src/app/PortToDocs.csproj b/src/PortToDocs/src/app/PortToDocs.csproj index 47899a2..971cfce 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); From 5b7e75f2ff8b4ab11549a4993d165af36c5961a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20S=C3=A1nchez=20L=C3=B3pez?= <1175054+carlossanlop@users.noreply.github.com> Date: Tue, 6 Aug 2024 10:56:35 -0700 Subject: [PATCH 2/2] Update the filesystem tests --- .../xml_expected/MyAssembly/MyType.xml | 12 +++++----- .../xml_expected/MyAssembly/MyType.xml | 24 ++++++++----------- 2 files changed, 16 insertions(+), 20 deletions(-) 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. +