-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged PR 306: v1.2.0 - sub/superscript support
- Add support for subscripts and superscripts - Make JSON output nicer by having bonus answers closer to their parts - Make it rarer for tags to be nested in a technically invalid order (e.g. `<b><u></b></u>`) - Bump version to 1.2.0
- Loading branch information
1 parent
00c6f58
commit 7368cc1
Showing
14 changed files
with
245 additions
and
133 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
YetAnotherPacketParser/YetAnotherPacketParser/Compiler/FormattedTextExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace YetAnotherPacketParser.Compiler | ||
{ | ||
internal static class FormattedTextExtensions | ||
{ | ||
public static void WriteFormattedText(this FormattedText node, StringBuilder builder) | ||
{ | ||
Verify.IsNotNull(node, nameof(node)); | ||
|
||
if (!node.Segments.Any()) | ||
{ | ||
return; | ||
} | ||
|
||
bool previousBolded = false; | ||
bool previousItalic = false; | ||
bool previousUnderlined = false; | ||
bool previousSubscript = false; | ||
bool previousSuperscript = false; | ||
|
||
foreach (FormattedTextSegment segment in node.Segments) | ||
{ | ||
// Close tags before opening new ones | ||
if (previousSuperscript && !segment.IsSuperscript) | ||
{ | ||
builder.Append("</sup>"); | ||
previousSuperscript = false; | ||
} | ||
|
||
if (previousSubscript && !segment.IsSubscript) | ||
{ | ||
builder.Append("</sub>"); | ||
previousSubscript = false; | ||
} | ||
|
||
if (previousItalic && !segment.Italic) | ||
{ | ||
builder.Append("</em>"); | ||
previousItalic = false; | ||
} | ||
|
||
if (previousUnderlined && !segment.Underlined) | ||
{ | ||
builder.Append("</u>"); | ||
previousUnderlined = false; | ||
} | ||
|
||
if (previousBolded ^ segment.Bolded) | ||
{ | ||
builder.Append(segment.Bolded ? "<b>" : "</b>"); | ||
previousBolded = segment.Bolded; | ||
} | ||
|
||
if (!previousBolded && segment.Bolded) | ||
{ | ||
builder.Append("<b>"); | ||
previousBolded = true; | ||
} | ||
|
||
if (!previousUnderlined && segment.Underlined) | ||
{ | ||
builder.Append("<u>"); | ||
previousUnderlined = true; | ||
} | ||
|
||
if (!previousItalic && segment.Italic) | ||
{ | ||
builder.Append("<em>"); | ||
previousItalic = true; | ||
} | ||
|
||
if (!previousSubscript && segment.IsSubscript) | ||
{ | ||
builder.Append("<sub>"); | ||
previousSubscript = true; | ||
} | ||
|
||
if (!previousSuperscript && segment.IsSuperscript) | ||
{ | ||
builder.Append("<sup>"); | ||
previousSuperscript = true; | ||
} | ||
|
||
builder.Append(segment.Text); | ||
} | ||
|
||
// Close any remaining tags | ||
if (previousBolded) | ||
{ | ||
builder.Append("</b>"); | ||
} | ||
|
||
if (previousUnderlined) | ||
{ | ||
builder.Append("</u>"); | ||
} | ||
|
||
if (previousItalic) | ||
{ | ||
builder.Append("</em>"); | ||
} | ||
|
||
if (previousSubscript) | ||
{ | ||
builder.Append("</sub>"); | ||
} | ||
|
||
if (previousSuperscript) | ||
{ | ||
builder.Append("</sup>"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.