Skip to content

Commit

Permalink
feat: render flagged content in spoilers
Browse files Browse the repository at this point in the history
  • Loading branch information
warriordog committed Aug 25, 2024
1 parent 84b71c2 commit 88640a7
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 5 deletions.
12 changes: 8 additions & 4 deletions ModShark.Tests/Reports/Document/DocumentBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void ToString_ShouldRenderMarkdownDocument()
RenderDocument(builder);
var document = builder.ToString();

document.Should().Be("# Title1\n# Title2\n\nSection1\n\n## Header1\n## Header2\n**Bold1** **Bold2** *Italics1* *Italics2* `Code1` `Code2`\n[Link1](https://example.com) [Link2](https://example.com)\n* Item1\n* Item2\n * Item3\n\n");
document.Should().Be("# Title1\n# Title2\n\nSection1\n\n## Header1\n## Header2\n**Bold1** **Bold2** *Italics1* *Italics2* `Code1` `Code2`\n[Link1](https://example.com) [Link2](https://example.com)\n* Item1\n* Item2\n * Item3\n||Spoiler1||||Spoiler2||\n");
}

[Test]
Expand All @@ -24,7 +24,7 @@ public void ToString_ShouldRenderMFMDocument()
RenderDocument(builder);
var document = builder.ToString();

document.Should().Be("<b>Title1</b>\n<b>Title2</b>\n\nSection1\n\n<b>Header1</b>\n<b>Header2</b>\n<b>Bold1</b> <b>Bold2</b> <i>Italics1</i> <i>Italics2</i> `Code1` `Code2`\n[Link1](https://example.com) [Link2](https://example.com)\n- Item1\n- Item2\n - Item3\n\n");
document.Should().Be("<b>Title1</b>\n<b>Title2</b>\n\nSection1\n\n<b>Header1</b>\n<b>Header2</b>\n<b>Bold1</b> <b>Bold2</b> <i>Italics1</i> <i>Italics2</i> `Code1` `Code2`\n[Link1](https://example.com) [Link2](https://example.com)\n- Item1\n- Item2\n - Item3\n$[blur Spoiler1]$[blur Spoiler2]\n");
}

[Test]
Expand All @@ -35,7 +35,7 @@ public void ToString_ShouldRenderHTMLDocument()
RenderDocument(builder);
var document = builder.ToString();

document.Should().Be("<h1>Title1</h1><h1>Title2</h1><div>Section1</div><div><h2>Header1</h2><h2>Header2</h2><span style=\"font-weight: bold\">Bold1</span> <span style=\"font-weight: bold\">Bold2</span> <span style=\"font-style: italic\">Italics1</span> <span style=\"font-style: italic\">Italics2</span> <code>Code1</code> <code>Code2</code><br><a href=\"https://example.com\">Link1</a> <a href=\"https://example.com\">Link2</a><ul><li>Item1</li><li>Item2</li><li><ul><li>Item3</li></ul></li></ul></div>");
document.Should().Be("<h1>Title1</h1><h1>Title2</h1><div>Section1</div><div><h2>Header1</h2><h2>Header2</h2><span style=\"font-weight: bold\">Bold1</span> <span style=\"font-weight: bold\">Bold2</span> <span style=\"font-style: italic\">Italics1</span> <span style=\"font-style: italic\">Italics2</span> <code>Code1</code> <code>Code2</code><br><a href=\"https://example.com\">Link1</a> <a href=\"https://example.com\">Link2</a><ul><li>Item1</li><li>Item2</li><li><ul><li>Item3</li></ul></li></ul>Spoiler1Spoiler2</div>");
}

[Test]
Expand All @@ -46,7 +46,7 @@ public void ToString_ShouldRenderHTML5Document()
RenderDocument(builder);
var document = builder.ToString();

document.Should().Be("<h1>Title1</h1><h1>Title2</h1><section>Section1</section><section><h2>Header1</h2><h2>Header2</h2><span style=\"font-weight: bold\">Bold1</span> <span style=\"font-weight: bold\">Bold2</span> <span style=\"font-style: italic\">Italics1</span> <span style=\"font-style: italic\">Italics2</span> <code>Code1</code> <code>Code2</code><br><a href=\"https://example.com\">Link1</a> <a href=\"https://example.com\">Link2</a><ul><li>Item1</li><li>Item2</li><li><ul><li>Item3</li></ul></li></ul></section>");
document.Should().Be("<h1>Title1</h1><h1>Title2</h1><section>Section1</section><section><h2>Header1</h2><h2>Header2</h2><span style=\"font-weight: bold\">Bold1</span> <span style=\"font-weight: bold\">Bold2</span> <span style=\"font-style: italic\">Italics1</span> <span style=\"font-style: italic\">Italics2</span> <code>Code1</code> <code>Code2</code><br><a href=\"https://example.com\">Link1</a> <a href=\"https://example.com\">Link2</a><ul><li>Item1</li><li>Item2</li><li><ul><li>Item3</li></ul></li></ul><details style=\"display: inline\"><summary>spoiler</summary>Spoiler1</details><details style=\"display: inline\"><summary>hidden</summary>Spoiler2</details></section>");
}

[TestCase(0)]
Expand Down Expand Up @@ -136,6 +136,10 @@ private void RenderDocument(DocumentBuilder builder)
.AppendListItem("Item3")
.End()
.End()
.AppendSpoiler("Spoiler1", "spoiler")
.BeginSpoiler("hidden")
.AppendText("Spoiler2")
.End()
.End();
}
}
3 changes: 3 additions & 0 deletions ModShark/Reports/Document/DocumentFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,8 @@ public abstract class DocumentFormat
public abstract string TitleStart();
public abstract string TitleEnd();

public abstract string SpoilerStart(string placeholder);
public abstract string SpoilerEnd(string placeholder);

public abstract string LineBreak();
}
5 changes: 5 additions & 0 deletions ModShark/Reports/Document/Format/HTML5Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ public class HTML5Format : HTMLFormat
{
public override string SectionStart() => "<section>";
public override string SectionEnd() => "</section>";

public override string SpoilerStart(string placeholder)
=> $"<details style=\"display: inline\"><summary>{Text(placeholder)}</summary>";
public override string SpoilerEnd(string placeholder)
=> "</details>";
}
4 changes: 4 additions & 0 deletions ModShark/Reports/Document/Format/HTMLFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public override string LinkStart(string href) =>

public override string TitleStart() => "<h1>";
public override string TitleEnd() => "</h1>";

// HTML3 (as used in email) does not support any kind of spoiler effect
public override string SpoilerStart(string placeholder) => "";
public override string SpoilerEnd(string placeholder) => "";

public override string LineBreak() => "<br>";
}
3 changes: 3 additions & 0 deletions ModShark/Reports/Document/Format/MFMFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ public class MFMFormat : MarkdownFormat

public override string TitleStart() => HeaderStart();
public override string TitleEnd() => HeaderEnd();

public override string SpoilerStart(string placeholder) => "$[blur ";
public override string SpoilerEnd(string placeholder) => "]";
}
3 changes: 3 additions & 0 deletions ModShark/Reports/Document/Format/MarkdownFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public override string Text(string text)
public override string TitleStart() => "# ";
public override string TitleEnd() => LineBreak();

public override string SpoilerStart(string placeholder) => "||";
public override string SpoilerEnd(string placeholder) => "||";

public override string LineBreak() => "\n";


Expand Down
14 changes: 14 additions & 0 deletions ModShark/Reports/Document/SegmentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,18 @@ public SegmentBuilder<TBuilder> BeginCode() =>
this,
Format.CodeEnd()
);

public TBuilder AppendSpoiler(string contents, string placeholder = "spoiler") =>
AppendText(
Format.SpoilerStart(placeholder),
contents,
Format.SpoilerEnd(placeholder)
);

public SegmentBuilder<TBuilder> BeginSpoiler(string placeholder = "spoiler") =>
new(
Format.SpoilerStart(placeholder),
this,
Format.SpoilerEnd(placeholder)
);
}
5 changes: 4 additions & 1 deletion ModShark/Reports/Render/RenderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,10 @@ private static void AppendFlagsOfType<T>(ListBuilder<ListBuilder<T>> subList, st
item.AppendText(", ");
first = false;

item.AppendCode(text);
item
.BeginSpoiler("hidden")
.AppendCode(text)
.End();
}

item.End();
Expand Down

0 comments on commit 88640a7

Please sign in to comment.