From 65184aff5200cf6d7377f90ec73f9f6fa662f828 Mon Sep 17 00:00:00 2001 From: carlossanlop <1175054+carlossanlop@users.noreply.github.com> Date: Thu, 15 Aug 2024 15:14:45 -0700 Subject: [PATCH] Improve the logic that wraps paragraphs with - 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. --- src/PortToDocs/src/libraries/XmlHelper.cs | 52 ++++++++++++++++++++--- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/src/PortToDocs/src/libraries/XmlHelper.cs b/src/PortToDocs/src/libraries/XmlHelper.cs index e4582c6..f22a71c 100644 --- a/src/PortToDocs/src/libraries/XmlHelper.cs +++ b/src/PortToDocs/src/libraries/XmlHelper.cs @@ -203,19 +203,57 @@ public static string GetFormattedAsXml(string value, bool removeUndesiredEndline private static string ReplaceEndLinesWithParas(string updatedValue) { string[] splitted = updatedValue.Split(_splittingSeparators, _splittingStringSplitOptions); - bool moreThanOne = splitted.Count() > 1; + + if (splitted.Length == 1) + { + // No need to add paras + return splitted[0]; + } StringBuilder newValue = new(); - foreach (string s in splitted) + ReadOnlySpan ros; + bool addStartParaNext = true; + for (int i = 0; i < splitted.Length; i++) { - if (moreThanOne && !s.StartsWith("")) + ros = splitted[i]; + + if (ros.StartsWith("") && ros.EndsWith("")) { - newValue.Append(""); + // No change + newValue.Append(ros); + // Next time we find a line not surrounded by paras, we need to add the starting one first + addStartParaNext = true; } - newValue.Append(s); - if (moreThanOne && !s.EndsWith("")) + else { - newValue.Append(""); + if (addStartParaNext) + { + newValue.Append(""); + addStartParaNext = false; + } + + if (splitted[i].Equals("- or -")) + { + newValue.Append("-or-"); + } + else + { + newValue.Append(ros); + } + if (!ros.EndsWith("")) + { + if (ros[^1] is '.' or ':' || (i + 1) >= splitted.Length || splitted[i].Equals("-or-") || splitted[i].Equals("- or -")) + { + // We're done with this line + newValue.Append(""); + addStartParaNext = true; + } + else + { + // Space separator, the next line will get appended next + newValue.Append(' '); + } + } } }