From 539f57f455b33e0b724ba7ce5ee46ee04c485a13 Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Sat, 11 Jan 2020 18:03:07 +0200 Subject: [PATCH] [HTML/TXT] Render exported message count at the bottom Closes #249 --- .../Formatters/HtmlMessageWriter.cs | 11 ++++++++- .../Formatters/PlainTextMessageWriter.cs | 10 ++++++++ .../Logic/PlainTextRenderingLogic.cs | 23 ++++++++++++++----- .../Resources/HtmlCore.css | 15 ++++++++---- .../Resources/HtmlDark.css | 16 ++++++++++--- .../Resources/HtmlLayoutTemplate.html | 9 +++++++- .../Resources/HtmlLight.css | 16 ++++++++++--- 7 files changed, 82 insertions(+), 18 deletions(-) diff --git a/DiscordChatExporter.Core.Rendering/Formatters/HtmlMessageWriter.cs b/DiscordChatExporter.Core.Rendering/Formatters/HtmlMessageWriter.cs index 54bfc0f..6509813 100644 --- a/DiscordChatExporter.Core.Rendering/Formatters/HtmlMessageWriter.cs +++ b/DiscordChatExporter.Core.Rendering/Formatters/HtmlMessageWriter.cs @@ -21,6 +21,8 @@ namespace DiscordChatExporter.Core.Rendering.Formatters private readonly Template _messageGroupTemplate; private readonly Template _postambleTemplate; + private long _messageCount; + public HtmlMessageWriter(TextWriter writer, RenderContext context, string themeName) : base(writer, context) { @@ -111,6 +113,9 @@ namespace DiscordChatExporter.Core.Rendering.Formatters _messageGroupBuffer.Clear(); _messageGroupBuffer.Add(message); } + + // Increment message count + _messageCount++; } public override async Task WritePostambleAsync() @@ -119,7 +124,11 @@ namespace DiscordChatExporter.Core.Rendering.Formatters if (_messageGroupBuffer.Any()) await RenderCurrentMessageGroupAsync(); - var templateContext = CreateTemplateContext(); + var templateContext = CreateTemplateContext(new Dictionary + { + ["MessageCount"] = _messageCount + }); + await templateContext.EvaluateAsync(_postambleTemplate.Page); } } diff --git a/DiscordChatExporter.Core.Rendering/Formatters/PlainTextMessageWriter.cs b/DiscordChatExporter.Core.Rendering/Formatters/PlainTextMessageWriter.cs index 5476851..8d1fd7c 100644 --- a/DiscordChatExporter.Core.Rendering/Formatters/PlainTextMessageWriter.cs +++ b/DiscordChatExporter.Core.Rendering/Formatters/PlainTextMessageWriter.cs @@ -7,6 +7,8 @@ namespace DiscordChatExporter.Core.Rendering.Formatters { public class PlainTextMessageWriter : MessageWriterBase { + private long _messageCount; + public PlainTextMessageWriter(TextWriter writer, RenderContext context) : base(writer, context) { @@ -21,6 +23,14 @@ namespace DiscordChatExporter.Core.Rendering.Formatters { await Writer.WriteLineAsync(PlainTextRenderingLogic.FormatMessage(Context, message)); await Writer.WriteLineAsync(); + + _messageCount++; + } + + public override async Task WritePostambleAsync() + { + await Writer.WriteLineAsync(); + await Writer.WriteLineAsync(PlainTextRenderingLogic.FormatPostamble(_messageCount)); } } } \ No newline at end of file diff --git a/DiscordChatExporter.Core.Rendering/Logic/PlainTextRenderingLogic.cs b/DiscordChatExporter.Core.Rendering/Logic/PlainTextRenderingLogic.cs index 437756e..f560559 100644 --- a/DiscordChatExporter.Core.Rendering/Logic/PlainTextRenderingLogic.cs +++ b/DiscordChatExporter.Core.Rendering/Logic/PlainTextRenderingLogic.cs @@ -18,7 +18,7 @@ namespace DiscordChatExporter.Core.Rendering.Logic { var buffer = new StringBuilder(); - buffer.AppendLine('='.Repeat(62)); + buffer.Append('=', 62).AppendLine(); buffer.AppendLine($"Guild: {context.Guild.Name}"); buffer.AppendLine($"Channel: {context.Channel.Name}"); @@ -31,7 +31,18 @@ namespace DiscordChatExporter.Core.Rendering.Logic if (context.Before != null) buffer.AppendLine($"Before: {FormatDate(context.Before.Value, context.DateFormat)}"); - buffer.AppendLine('='.Repeat(62)); + buffer.Append('=', 62).AppendLine(); + + return buffer.ToString(); + } + + public static string FormatPostamble(long messageCount) + { + var buffer = new StringBuilder(); + + buffer.Append('=', 62).AppendLine(); + buffer.AppendLine($"Exported {messageCount:N0} message(s)"); + buffer.Append('=', 62).AppendLine(); return buffer.ToString(); } @@ -121,7 +132,7 @@ namespace DiscordChatExporter.Core.Rendering.Logic return FormatMarkdown(context, message.Content); } - public static string FormatAttachments(RenderContext context, IReadOnlyList attachments) + public static string FormatAttachments(IReadOnlyList attachments) { if (!attachments.Any()) return ""; @@ -193,7 +204,7 @@ namespace DiscordChatExporter.Core.Rendering.Logic return buffer.ToString(); } - public static string FormatReactions(RenderContext context, IReadOnlyList reactions) + public static string FormatReactions(IReadOnlyList reactions) { if (!reactions.Any()) return ""; @@ -225,9 +236,9 @@ namespace DiscordChatExporter.Core.Rendering.Logic .AppendLine(FormatMessageHeader(context, message)) .AppendLineIfNotEmpty(FormatMessageContent(context, message)) .AppendLine() - .AppendLineIfNotEmpty(FormatAttachments(context, message.Attachments)) + .AppendLineIfNotEmpty(FormatAttachments(message.Attachments)) .AppendLineIfNotEmpty(FormatEmbeds(context, message.Embeds)) - .AppendLineIfNotEmpty(FormatReactions(context, message.Reactions)); + .AppendLineIfNotEmpty(FormatReactions(message.Reactions)); return buffer.Trim().ToString(); } diff --git a/DiscordChatExporter.Core.Rendering/Resources/HtmlCore.css b/DiscordChatExporter.Core.Rendering/Resources/HtmlCore.css index d7e5857..9c2ddb2 100644 --- a/DiscordChatExporter.Core.Rendering/Resources/HtmlCore.css +++ b/DiscordChatExporter.Core.Rendering/Resources/HtmlCore.css @@ -1,4 +1,4 @@ -/* === GENERAL === */ +/* === General === */ @font-face { font-family: Whitney; @@ -102,7 +102,7 @@ img { height: 2rem; } -/* === INFO === */ +/* === Preamble === */ .info { display: flex; @@ -144,11 +144,10 @@ img { margin-top: 2px; } -/* === CHATLOG === */ +/* === Chatlog === */ .chatlog { max-width: 100%; - margin-bottom: 24px; } .chatlog__message-group { @@ -365,4 +364,12 @@ img { line-height: 1.3; position: relative; top: -.2em; +} + +/* Postamble */ + +.postamble { + margin: 24px 5px 10px 5px; + padding: 16px; + border-top: 1px solid; } \ No newline at end of file diff --git a/DiscordChatExporter.Core.Rendering/Resources/HtmlDark.css b/DiscordChatExporter.Core.Rendering/Resources/HtmlDark.css index 9ebd02a..9e4c873 100644 --- a/DiscordChatExporter.Core.Rendering/Resources/HtmlDark.css +++ b/DiscordChatExporter.Core.Rendering/Resources/HtmlDark.css @@ -1,4 +1,4 @@ -/* === GENERAL === */ +/* === General === */ body { background-color: #36393e; @@ -30,7 +30,7 @@ a { color: #7289da; } -/* === INFO === */ +/* === Preamble === */ .info__guild-name { color: #ffffff; @@ -44,7 +44,7 @@ a { color: #ffffff; } -/* === CHATLOG === */ +/* === Chatlog === */ .chatlog__message-group { border-color: rgba(255, 255, 255, 0.1); @@ -113,4 +113,14 @@ a { .chatlog__reaction-count { color: rgba(255, 255, 255, 0.3); +} + +/* === Postamble === */ + +.postamble { + border-color: rgba(255, 255, 255, 0.1); +} + +.postamble__info { + color: #ffffff; } \ No newline at end of file diff --git a/DiscordChatExporter.Core.Rendering/Resources/HtmlLayoutTemplate.html b/DiscordChatExporter.Core.Rendering/Resources/HtmlLayoutTemplate.html index 6b94cf3..de5e5d4 100644 --- a/DiscordChatExporter.Core.Rendering/Resources/HtmlLayoutTemplate.html +++ b/DiscordChatExporter.Core.Rendering/Resources/HtmlLayoutTemplate.html @@ -48,7 +48,7 @@ -{{~ # Info ~}} +{{~ # Preamble ~}}
@@ -80,5 +80,12 @@ {{~ %SPLIT% ~}}
+{{~ # Postamble ~}} +
+
+ Exported {{ MessageCount | object.format "N0" }} message(s) +
+
+ \ No newline at end of file diff --git a/DiscordChatExporter.Core.Rendering/Resources/HtmlLight.css b/DiscordChatExporter.Core.Rendering/Resources/HtmlLight.css index 872352c..3dbc1df 100644 --- a/DiscordChatExporter.Core.Rendering/Resources/HtmlLight.css +++ b/DiscordChatExporter.Core.Rendering/Resources/HtmlLight.css @@ -1,4 +1,4 @@ -/* === GENERAL === */ +/* === General === */ body { background-color: #ffffff; @@ -32,7 +32,7 @@ a { color: #7289da; } -/* === INFO === */ +/* === Preamble === */ .info__guild-name { color: #2f3136; @@ -46,7 +46,7 @@ a { color: #2f3136; } -/* === CHATLOG === */ +/* === Chatlog === */ .chatlog__message-group { border-color: #eceeef; @@ -116,4 +116,14 @@ a { .chatlog__reaction-count { color: #747f8d; +} + +/* === Postamble === */ + +.postamble { + border-color: #eceeef; +} + +.postamble__info { + color: #2f3136; } \ No newline at end of file