Skip to content

Commit

Permalink
Improve the logic that wraps paragraphs with <para></para>
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
carlossanlop committed Aug 15, 2024
1 parent a471c51 commit 65184af
Showing 1 changed file with 45 additions and 7 deletions.
52 changes: 45 additions & 7 deletions src/PortToDocs/src/libraries/XmlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> ros;
bool addStartParaNext = true;
for (int i = 0; i < splitted.Length; i++)
{
if (moreThanOne && !s.StartsWith("<para>"))
ros = splitted[i];

if (ros.StartsWith("<para>") && ros.EndsWith("</para>"))
{
newValue.Append("<para>");
// 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("</para>"))
else
{
newValue.Append("</para>");
if (addStartParaNext)
{
newValue.Append("<para>");
addStartParaNext = false;
}

if (splitted[i].Equals("- or -"))
{
newValue.Append("-or-");
}
else
{
newValue.Append(ros);
}
if (!ros.EndsWith("</para>"))
{
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("</para>");
addStartParaNext = true;
}
else
{
// Space separator, the next line will get appended next
newValue.Append(' ');
}
}
}
}

Expand Down

0 comments on commit 65184af

Please sign in to comment.