From 4283ef22b1c97735193af613cdc6568765f4eee7 Mon Sep 17 00:00:00 2001 From: Oleksii Holub <1935960+Tyrrrz@users.noreply.github.com> Date: Tue, 28 Jun 2022 20:20:33 +0300 Subject: [PATCH] Add HTML minification Closes #345 Closes #743 --- .../DiscordChatExporter.Core.csproj | 1 + .../Writers/Html/MessageGroupTemplate.cshtml | 20 ++++---- .../Writers/Html/PostambleTemplate.cshtml | 2 + .../Writers/Html/PreambleTemplate.cshtml | 2 + .../Exporting/Writers/HtmlMessageWriter.cs | 46 +++++++++++-------- 5 files changed, 42 insertions(+), 29 deletions(-) diff --git a/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj b/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj index fe51cbb..f556361 100644 --- a/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj +++ b/DiscordChatExporter.Core/DiscordChatExporter.Core.csproj @@ -10,6 +10,7 @@ + diff --git a/DiscordChatExporter.Core/Exporting/Writers/Html/MessageGroupTemplate.cshtml b/DiscordChatExporter.Core/Exporting/Writers/Html/MessageGroupTemplate.cshtml index 71ababb..ddcadcb 100644 --- a/DiscordChatExporter.Core/Exporting/Writers/Html/MessageGroupTemplate.cshtml +++ b/DiscordChatExporter.Core/Exporting/Writers/Html/MessageGroupTemplate.cshtml @@ -85,7 +85,7 @@ @if (!string.IsNullOrWhiteSpace(message.ReferencedMessage.Content)) { - @Raw(FormatEmbedMarkdown(message.ReferencedMessage.Content)) + @Raw(FormatEmbedMarkdown(message.ReferencedMessage.Content)) } else if (message.ReferencedMessage.Attachments.Any() || message.ReferencedMessage.Embeds.Any()) { @@ -134,7 +134,7 @@ {
@{/* Text */} - @Raw(FormatMarkdown(message.Content)) + @Raw(FormatMarkdown(message.Content)) @{/* Edited timestamp */} @if (message.EditedTimestamp is not null) @@ -262,12 +262,12 @@ @if (!string.IsNullOrWhiteSpace(embed.Url)) { -
@Raw(FormatEmbedMarkdown(embed.Title))
+
@Raw(FormatEmbedMarkdown(embed.Title))
} else { -
@Raw(FormatEmbedMarkdown(embed.Title))
+
@Raw(FormatEmbedMarkdown(embed.Title))
}
} @@ -330,12 +330,12 @@ @if (!string.IsNullOrWhiteSpace(embed.Url)) { -
@Raw(FormatEmbedMarkdown(embed.Title))
+
@Raw(FormatEmbedMarkdown(embed.Title))
} else { -
@Raw(FormatEmbedMarkdown(embed.Title))
+
@Raw(FormatEmbedMarkdown(embed.Title))
} } @@ -344,7 +344,7 @@ @if (!string.IsNullOrWhiteSpace(embed.Description)) {
-
@Raw(FormatEmbedMarkdown(embed.Description))
+
@Raw(FormatEmbedMarkdown(embed.Description))
} @@ -358,14 +358,14 @@ @if (!string.IsNullOrWhiteSpace(field.Name)) {
-
@Raw(FormatEmbedMarkdown(field.Name))
+
@Raw(FormatEmbedMarkdown(field.Name))
} @if (!string.IsNullOrWhiteSpace(field.Value)) {
-
@Raw(FormatEmbedMarkdown(field.Value))
+
@Raw(FormatEmbedMarkdown(field.Value))
} @@ -462,4 +462,4 @@ } - + \ No newline at end of file diff --git a/DiscordChatExporter.Core/Exporting/Writers/Html/PostambleTemplate.cshtml b/DiscordChatExporter.Core/Exporting/Writers/Html/PostambleTemplate.cshtml index d979f09..3d508bd 100644 --- a/DiscordChatExporter.Core/Exporting/Writers/Html/PostambleTemplate.cshtml +++ b/DiscordChatExporter.Core/Exporting/Writers/Html/PostambleTemplate.cshtml @@ -4,7 +4,9 @@ @inherits MiniRazor.TemplateBase @{/* Close elements opened by preamble */} + +
Exported @Model.MessagesWritten.ToString("N0") message(s)
diff --git a/DiscordChatExporter.Core/Exporting/Writers/Html/PreambleTemplate.cshtml b/DiscordChatExporter.Core/Exporting/Writers/Html/PreambleTemplate.cshtml index a91ea71..7b96a76 100644 --- a/DiscordChatExporter.Core/Exporting/Writers/Html/PreambleTemplate.cshtml +++ b/DiscordChatExporter.Core/Exporting/Writers/Html/PreambleTemplate.cshtml @@ -807,4 +807,6 @@
@{/* Preamble cuts off at this point */} +
+ \ No newline at end of file diff --git a/DiscordChatExporter.Core/Exporting/Writers/HtmlMessageWriter.cs b/DiscordChatExporter.Core/Exporting/Writers/HtmlMessageWriter.cs index 0ede4b5..5d70355 100644 --- a/DiscordChatExporter.Core/Exporting/Writers/HtmlMessageWriter.cs +++ b/DiscordChatExporter.Core/Exporting/Writers/HtmlMessageWriter.cs @@ -6,6 +6,7 @@ using System.Threading; using System.Threading.Tasks; using DiscordChatExporter.Core.Discord.Data; using DiscordChatExporter.Core.Exporting.Writers.Html; +using WebMarkupMin.Core; namespace DiscordChatExporter.Core.Exporting.Writers; @@ -14,6 +15,7 @@ internal class HtmlMessageWriter : MessageWriter private readonly TextWriter _writer; private readonly string _themeName; + private readonly HtmlMinifier _minifier = new(); private readonly List _messageGroup = new(); public HtmlMessageWriter(Stream stream, ExportContext context, string themeName) @@ -23,22 +25,22 @@ internal class HtmlMessageWriter : MessageWriter _themeName = themeName; } - private bool CanJoinGroup(Message message) - { - var lastMessage = _messageGroup.LastOrDefault(); - if (lastMessage is null) - return true; - - return - // Must be from the same author - lastMessage.Author.Id == message.Author.Id && - // Author's name must not have changed between messages - string.Equals(lastMessage.Author.FullName, message.Author.FullName, StringComparison.Ordinal) && - // Duration between messages must be 7 minutes or less - (message.Timestamp - lastMessage.Timestamp).Duration().TotalMinutes <= 7 && - // Other message must not be a reply - message.Reference is null; - } + private bool CanJoinGroup(Message message) => + // First message in the group can always join + _messageGroup.LastOrDefault() is not { } lastMessage || + // Must be from the same author + lastMessage.Author.Id == message.Author.Id && + // Author's name must not have changed between messages + string.Equals(lastMessage.Author.FullName, message.Author.FullName, StringComparison.Ordinal) && + // Duration between messages must be 7 minutes or less + (message.Timestamp - lastMessage.Timestamp).Duration().TotalMinutes <= 7 && + // Other message must not be a reply + message.Reference is null; + + // Use to preserve blocks of code inside the templates + private string Minify(string html) => _minifier + .Minify(html, false) + .MinifiedContent; public override async ValueTask WritePreambleAsync(CancellationToken cancellationToken = default) { @@ -47,7 +49,9 @@ internal class HtmlMessageWriter : MessageWriter // We are not writing directly to output because Razor // does not actually do asynchronous writes to stream. await _writer.WriteLineAsync( - await PreambleTemplate.RenderAsync(templateContext, cancellationToken) + Minify( + await PreambleTemplate.RenderAsync(templateContext, cancellationToken) + ) ); } @@ -60,7 +64,9 @@ internal class HtmlMessageWriter : MessageWriter // We are not writing directly to output because Razor // does not actually do asynchronous writes to stream. await _writer.WriteLineAsync( - await MessageGroupTemplate.RenderAsync(templateContext, cancellationToken) + Minify( + await MessageGroupTemplate.RenderAsync(templateContext, cancellationToken) + ) ); } @@ -96,7 +102,9 @@ internal class HtmlMessageWriter : MessageWriter // We are not writing directly to output because Razor // does not actually do asynchronous writes to stream. await _writer.WriteLineAsync( - await PostambleTemplate.RenderAsync(templateContext, cancellationToken) + Minify( + await PostambleTemplate.RenderAsync(templateContext, cancellationToken) + ) ); }