Skip to content

Commit

Permalink
Merge pull request #51 from alopezlago/alopezlago/public_1_2_0
Browse files Browse the repository at this point in the history
v1.2.0 - sub/superscript support, nicer JSON outputs
  • Loading branch information
alopezlago committed Mar 12, 2024
2 parents 00c6f58 + 7368cc1 commit b710fb9
Show file tree
Hide file tree
Showing 14 changed files with 245 additions and 133 deletions.
@@ -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>");
}
}
}
}
@@ -1,5 +1,4 @@
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YetAnotherPacketParser.Ast;
Expand Down Expand Up @@ -63,7 +62,7 @@ private static void WriteBonus(BonusNode bonus, StringBuilder builder)
builder.Append("<p>");
builder.Append(bonus.Number);
builder.Append(". ");
WriteFormattedText(bonus.Leadin, builder);
bonus.Leadin.WriteFormattedText(builder);
builder.Append("<br>");
foreach (BonusPartNode bonusPart in bonus.Parts)
{
Expand Down Expand Up @@ -91,66 +90,11 @@ private static void WriteBonusPart(BonusPartNode bonusPart, StringBuilder builde

private static void WriteQuestion(QuestionNode node, StringBuilder builder)
{
WriteFormattedText(node.Question, builder);
node.Question.WriteFormattedText(builder);
builder.AppendLine("<br>");
builder.Append("ANSWER: ");
WriteFormattedText(node.Answer, builder);
node.Answer.WriteFormattedText(builder);
builder.AppendLine("<br>");
}

// TODO: move this somewhere where it can be tested, or merge with JsonCompiler (different tags needed)
// Would lose some efficiency that way, since we'd recreate a StringBuilder each time
private static void WriteFormattedText(FormattedText node, StringBuilder builder)
{
Verify.IsNotNull(node, nameof(node));

if (!node.Segments.Any())
{
return;
}

bool previousBolded = false;
bool previousItalic = false;
bool previousUnderlined = false;

foreach (FormattedTextSegment segment in node.Segments)
{
if (previousBolded ^ segment.Bolded)
{
builder.Append(segment.Bolded ? "<b>" : "</b>");
previousBolded = segment.Bolded;
}

if (previousUnderlined ^ segment.Underlined)
{
builder.Append(segment.Underlined ? "<u>" : "</u>");
previousUnderlined = segment.Underlined;
}

if (previousItalic ^ segment.Italic)
{
builder.Append(segment.Italic ? "<em>" : "</em>");
previousItalic = segment.Italic;
}

builder.Append(segment.Text);
}

// Close any remaining tags
if (previousBolded)
{
builder.Append("</b>");
}

if (previousUnderlined)
{
builder.Append("</u>");
}

if (previousItalic)
{
builder.Append("</em>");
}
}
}
}
Expand Up @@ -43,18 +43,18 @@ public JsonBonusNode(BonusNode bonusNode, bool omitSanitizedFields)

public string? Leadin_sanitized { get; }

public string? Metadata { get; }
public ICollection<string> Parts { get; }

public ICollection<string>? Parts_sanitized { get; }

public ICollection<string> Answers { get; }

public ICollection<string>? Answers_sanitized { get; }

public ICollection<string> Parts { get; }

public ICollection<string>? Parts_sanitized { get; }

public ICollection<int> Values { get; }

public ICollection<char?>? DifficultyModifiers { get; }

public string? Metadata { get; }
}
}
Expand Up @@ -8,55 +8,8 @@ internal static class JsonTextFormatter
internal static string ToStringWithTags(FormattedText node)
{
Verify.IsNotNull(node, nameof(node));

if (!node.Segments.Any())
{
return string.Empty;
}

bool previousBolded = false;
bool previousItalic = false;
bool previousUnderlined = false;

StringBuilder builder = new StringBuilder();
foreach (FormattedTextSegment segment in node.Segments)
{
if (previousBolded ^ segment.Bolded)
{
builder.Append(segment.Bolded ? "<b>" : "</b>");
previousBolded = segment.Bolded;
}

if (previousUnderlined ^ segment.Underlined)
{
builder.Append(segment.Underlined ? "<u>" : "</u>");
previousUnderlined = segment.Underlined;
}

if (previousItalic ^ segment.Italic)
{
builder.Append(segment.Italic ? "<em>" : "</em>");
previousItalic = segment.Italic;
}

builder.Append(segment.Text);
}

// Close any remaining tags
if (previousBolded)
{
builder.Append("</b>");
}

if (previousUnderlined)
{
builder.Append("</u>");
}

if (previousItalic)
{
builder.Append("</em>");
}
node.WriteFormattedText(builder);

return builder.ToString();
}
Expand Down
Expand Up @@ -135,7 +135,9 @@ private FormattedText SanitizeFormattedTexts(FormattedText rawFormattedTexts)
sanitizedText,
rawSegment.Italic,
rawSegment.Bolded,
rawSegment.Underlined);
rawSegment.Underlined,
rawSegment.IsSubscript,
rawSegment.IsSuperscript);
sanitizedFormattedTexts.Add(sanitizedFormattedText);
}

Expand Down
Expand Up @@ -29,7 +29,12 @@ public FormattedText Substring(int startIndex)
{
string substringText = segment.Text.Substring(startIndex - index);
segments.Add(new FormattedTextSegment(
substringText, segment.Italic, segment.Bolded, segment.Underlined));
substringText,
segment.Italic,
segment.Bolded,
segment.Underlined,
segment.IsSubscript,
segment.IsSuperscript));
}
else if (index >= startIndex)
{
Expand Down
Expand Up @@ -4,12 +4,20 @@ namespace YetAnotherPacketParser
{
internal class FormattedTextSegment
{
public FormattedTextSegment(string text, bool italic = false, bool bolded = false, bool underlined = false)
public FormattedTextSegment(
string text,
bool italic = false,
bool bolded = false,
bool underlined = false,
bool isSubscript = false,
bool isSuperscript = false)
{
this.Text = text ?? throw new ArgumentNullException(nameof(text));
this.Italic = italic;
this.Bolded = bolded;
this.Underlined = underlined;
this.IsSubscript = isSubscript;
this.IsSuperscript = isSuperscript;
}

public string Text { get; }
Expand All @@ -20,12 +28,18 @@ public FormattedTextSegment(string text, bool italic = false, bool bolded = fals

public bool Underlined { get; }

public bool IsSubscript { get; }

public bool IsSuperscript { get; }

public override string ToString()
{
string boldedString = this.Bolded ? "bolded, " : string.Empty;
string italicString = this.Italic ? "italic, " : string.Empty;
string underlinedString = this.Underlined ? "underlined, " : string.Empty;
string propertiesString = $"{boldedString}{italicString}{underlinedString}".Trim();
string subscriptString = this.IsSubscript ? "subscript, " : string.Empty;
string superscriptString = this.IsSuperscript ? "superscript, " : string.Empty;
string propertiesString = $"{boldedString}{italicString}{underlinedString}{subscriptString}{superscriptString}".Trim();
return $"({propertiesString}) {this.Text}";
}

Expand All @@ -39,15 +53,19 @@ public override bool Equals(object? obj)
return this.Text == other.Text &&
this.Bolded == other.Bolded &&
this.Italic == other.Italic &&
this.Underlined == other.Underlined;
this.Underlined == other.Underlined &&
this.IsSubscript == other.IsSubscript &&
this.IsSuperscript == other.IsSuperscript;
}

public override int GetHashCode()
{
return (this.Text?.GetHashCode(StringComparison.Ordinal) ?? 0) ^
this.Bolded.GetHashCode() ^
(this.Italic.GetHashCode() << 1) ^
(this.Underlined.GetHashCode() << 2);
(this.Underlined.GetHashCode() << 2) ^
(this.IsSubscript.GetHashCode() << 3) ^
(this.IsSuperscript.GetHashCode() << 4);
}
}
}

0 comments on commit b710fb9

Please sign in to comment.