From 88640a7c5f1eccb7cc491f5c2629c6238a56cbc7 Mon Sep 17 00:00:00 2001 From: Hazel K Date: Sun, 25 Aug 2024 00:12:41 -0400 Subject: [PATCH] feat: render flagged content in spoilers --- .../Reports/Document/DocumentBuilderTests.cs | 12 ++++++++---- ModShark/Reports/Document/DocumentFormat.cs | 3 +++ ModShark/Reports/Document/Format/HTML5Format.cs | 5 +++++ ModShark/Reports/Document/Format/HTMLFormat.cs | 4 ++++ ModShark/Reports/Document/Format/MFMFormat.cs | 3 +++ ModShark/Reports/Document/Format/MarkdownFormat.cs | 3 +++ ModShark/Reports/Document/SegmentBase.cs | 14 ++++++++++++++ ModShark/Reports/Render/RenderService.cs | 5 ++++- 8 files changed, 44 insertions(+), 5 deletions(-) diff --git a/ModShark.Tests/Reports/Document/DocumentBuilderTests.cs b/ModShark.Tests/Reports/Document/DocumentBuilderTests.cs index 90535e8..1fd0383 100644 --- a/ModShark.Tests/Reports/Document/DocumentBuilderTests.cs +++ b/ModShark.Tests/Reports/Document/DocumentBuilderTests.cs @@ -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] @@ -24,7 +24,7 @@ public void ToString_ShouldRenderMFMDocument() RenderDocument(builder); var document = builder.ToString(); - document.Should().Be("Title1\nTitle2\n\nSection1\n\nHeader1\nHeader2\nBold1 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\nTitle2\n\nSection1\n\nHeader1\nHeader2\nBold1 Bold2 Italics1 Italics2 `Code1` `Code2`\n[Link1](https://example.com) [Link2](https://example.com)\n- Item1\n- Item2\n - Item3\n$[blur Spoiler1]$[blur Spoiler2]\n"); } [Test] @@ -35,7 +35,7 @@ public void ToString_ShouldRenderHTMLDocument() RenderDocument(builder); var document = builder.ToString(); - document.Should().Be("

Title1

Title2

Section1

Header1

Header2

Bold1 Bold2 Italics1 Italics2 Code1 Code2
Link1 Link2
"); + document.Should().Be("

Title1

Title2

Section1

Header1

Header2

Bold1 Bold2 Italics1 Italics2 Code1 Code2
Link1 Link2Spoiler1Spoiler2
"); } [Test] @@ -46,7 +46,7 @@ public void ToString_ShouldRenderHTML5Document() RenderDocument(builder); var document = builder.ToString(); - document.Should().Be("

Title1

Title2

Section1

Header1

Header2

Bold1 Bold2 Italics1 Italics2 Code1 Code2
Link1 Link2
"); + document.Should().Be("

Title1

Title2

Section1

Header1

Header2

Bold1 Bold2 Italics1 Italics2 Code1 Code2
Link1 Link2
spoilerSpoiler1
hiddenSpoiler2
"); } [TestCase(0)] @@ -136,6 +136,10 @@ private void RenderDocument(DocumentBuilder builder) .AppendListItem("Item3") .End() .End() + .AppendSpoiler("Spoiler1", "spoiler") + .BeginSpoiler("hidden") + .AppendText("Spoiler2") + .End() .End(); } } \ No newline at end of file diff --git a/ModShark/Reports/Document/DocumentFormat.cs b/ModShark/Reports/Document/DocumentFormat.cs index dd3537f..324b757 100644 --- a/ModShark/Reports/Document/DocumentFormat.cs +++ b/ModShark/Reports/Document/DocumentFormat.cs @@ -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(); } \ No newline at end of file diff --git a/ModShark/Reports/Document/Format/HTML5Format.cs b/ModShark/Reports/Document/Format/HTML5Format.cs index 1df38da..50352a9 100644 --- a/ModShark/Reports/Document/Format/HTML5Format.cs +++ b/ModShark/Reports/Document/Format/HTML5Format.cs @@ -7,4 +7,9 @@ public class HTML5Format : HTMLFormat { public override string SectionStart() => "
"; public override string SectionEnd() => "
"; + + public override string SpoilerStart(string placeholder) + => $"
{Text(placeholder)}"; + public override string SpoilerEnd(string placeholder) + => "
"; } \ No newline at end of file diff --git a/ModShark/Reports/Document/Format/HTMLFormat.cs b/ModShark/Reports/Document/Format/HTMLFormat.cs index 0ea6d11..fd6d8b3 100644 --- a/ModShark/Reports/Document/Format/HTMLFormat.cs +++ b/ModShark/Reports/Document/Format/HTMLFormat.cs @@ -61,6 +61,10 @@ public override string LinkStart(string href) => public override string TitleStart() => "

"; public override string TitleEnd() => "

"; + + // 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() => "
"; } \ No newline at end of file diff --git a/ModShark/Reports/Document/Format/MFMFormat.cs b/ModShark/Reports/Document/Format/MFMFormat.cs index 9642b45..188db95 100644 --- a/ModShark/Reports/Document/Format/MFMFormat.cs +++ b/ModShark/Reports/Document/Format/MFMFormat.cs @@ -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) => "]"; } \ No newline at end of file diff --git a/ModShark/Reports/Document/Format/MarkdownFormat.cs b/ModShark/Reports/Document/Format/MarkdownFormat.cs index 54c06d2..899aae1 100644 --- a/ModShark/Reports/Document/Format/MarkdownFormat.cs +++ b/ModShark/Reports/Document/Format/MarkdownFormat.cs @@ -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"; diff --git a/ModShark/Reports/Document/SegmentBase.cs b/ModShark/Reports/Document/SegmentBase.cs index 45168fb..152a876 100644 --- a/ModShark/Reports/Document/SegmentBase.cs +++ b/ModShark/Reports/Document/SegmentBase.cs @@ -58,4 +58,18 @@ public SegmentBuilder BeginCode() => this, Format.CodeEnd() ); + + public TBuilder AppendSpoiler(string contents, string placeholder = "spoiler") => + AppendText( + Format.SpoilerStart(placeholder), + contents, + Format.SpoilerEnd(placeholder) + ); + + public SegmentBuilder BeginSpoiler(string placeholder = "spoiler") => + new( + Format.SpoilerStart(placeholder), + this, + Format.SpoilerEnd(placeholder) + ); } \ No newline at end of file diff --git a/ModShark/Reports/Render/RenderService.cs b/ModShark/Reports/Render/RenderService.cs index 81b389f..9f93492 100644 --- a/ModShark/Reports/Render/RenderService.cs +++ b/ModShark/Reports/Render/RenderService.cs @@ -357,7 +357,10 @@ private static void AppendFlagsOfType(ListBuilder> subList, st item.AppendText(", "); first = false; - item.AppendCode(text); + item + .BeginSpoiler("hidden") + .AppendCode(text) + .End(); } item.End();