|
|
|
@ -3,6 +3,7 @@ using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Resources;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using DiscordChatExporter.Models;
|
|
|
|
|
using HtmlAgilityPack;
|
|
|
|
|
using Tyrrrz.Extensions;
|
|
|
|
@ -18,104 +19,109 @@ namespace DiscordChatExporter.Services
|
|
|
|
|
_settingsService = settingsService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Export(string filePath, ChannelChatLog log, Theme theme)
|
|
|
|
|
public Task ExportAsync(string filePath, ChannelChatLog log, Theme theme)
|
|
|
|
|
{
|
|
|
|
|
var doc = GetTemplate();
|
|
|
|
|
var style = GetStyle(theme);
|
|
|
|
|
var dateFormat = _settingsService.DateFormat;
|
|
|
|
|
|
|
|
|
|
// Set theme
|
|
|
|
|
var themeHtml = doc.GetElementbyId("theme");
|
|
|
|
|
themeHtml.InnerHtml = style;
|
|
|
|
|
|
|
|
|
|
// Title
|
|
|
|
|
var titleHtml = doc.DocumentNode.Element("html").Element("head").Element("title");
|
|
|
|
|
titleHtml.InnerHtml = $"{log.Guild.Name} - {log.Channel.Name}";
|
|
|
|
|
|
|
|
|
|
// Info
|
|
|
|
|
var infoHtml = doc.GetElementbyId("info");
|
|
|
|
|
var infoLeftHtml = infoHtml.AppendChild(HtmlNode.CreateNode("<div class=\"info-left\"></div>"));
|
|
|
|
|
infoLeftHtml.AppendChild(HtmlNode.CreateNode(
|
|
|
|
|
$"<img class=\"guild-icon\" src=\"{log.Guild.IconUrl}\" />"));
|
|
|
|
|
var infoRightHtml = infoHtml.AppendChild(HtmlNode.CreateNode("<div class=\"info-right\"></div>"));
|
|
|
|
|
infoRightHtml.AppendChild(HtmlNode.CreateNode(
|
|
|
|
|
$"<div class=\"guild-name\">{log.Guild.Name}</div>"));
|
|
|
|
|
infoRightHtml.AppendChild(HtmlNode.CreateNode(
|
|
|
|
|
$"<div class=\"channel-name\">{log.Channel.Name}</div>"));
|
|
|
|
|
infoRightHtml.AppendChild(HtmlNode.CreateNode(
|
|
|
|
|
$"<div class=\"misc\">{log.MessageGroups.SelectMany(g => g.Messages).Count():N0} messages</div>"));
|
|
|
|
|
|
|
|
|
|
// Log
|
|
|
|
|
var logHtml = doc.GetElementbyId("log");
|
|
|
|
|
foreach (var messageGroup in log.MessageGroups)
|
|
|
|
|
return Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
// Container
|
|
|
|
|
var messageHtml = logHtml.AppendChild(HtmlNode.CreateNode("<div class=\"msg\"></div>"));
|
|
|
|
|
var doc = GetTemplate();
|
|
|
|
|
var style = GetStyle(theme);
|
|
|
|
|
var dateFormat = _settingsService.DateFormat;
|
|
|
|
|
|
|
|
|
|
// Set theme
|
|
|
|
|
var themeHtml = doc.GetElementbyId("theme");
|
|
|
|
|
themeHtml.InnerHtml = style;
|
|
|
|
|
|
|
|
|
|
// Title
|
|
|
|
|
var titleHtml = doc.DocumentNode.Element("html").Element("head").Element("title");
|
|
|
|
|
titleHtml.InnerHtml = $"{log.Guild.Name} - {log.Channel.Name}";
|
|
|
|
|
|
|
|
|
|
// Info
|
|
|
|
|
var infoHtml = doc.GetElementbyId("info");
|
|
|
|
|
var infoLeftHtml = infoHtml.AppendChild(HtmlNode.CreateNode("<div class=\"info-left\"></div>"));
|
|
|
|
|
infoLeftHtml.AppendChild(HtmlNode.CreateNode(
|
|
|
|
|
$"<img class=\"guild-icon\" src=\"{log.Guild.IconUrl}\" />"));
|
|
|
|
|
var infoRightHtml = infoHtml.AppendChild(HtmlNode.CreateNode("<div class=\"info-right\"></div>"));
|
|
|
|
|
infoRightHtml.AppendChild(HtmlNode.CreateNode(
|
|
|
|
|
$"<div class=\"guild-name\">{log.Guild.Name}</div>"));
|
|
|
|
|
infoRightHtml.AppendChild(HtmlNode.CreateNode(
|
|
|
|
|
$"<div class=\"channel-name\">{log.Channel.Name}</div>"));
|
|
|
|
|
infoRightHtml.AppendChild(HtmlNode.CreateNode(
|
|
|
|
|
$"<div class=\"misc\">{log.MessageGroups.SelectMany(g => g.Messages).Count():N0} messages</div>"));
|
|
|
|
|
|
|
|
|
|
// Log
|
|
|
|
|
var logHtml = doc.GetElementbyId("log");
|
|
|
|
|
foreach (var messageGroup in log.MessageGroups)
|
|
|
|
|
{
|
|
|
|
|
// Container
|
|
|
|
|
var messageHtml = logHtml.AppendChild(HtmlNode.CreateNode("<div class=\"msg\"></div>"));
|
|
|
|
|
|
|
|
|
|
// Left
|
|
|
|
|
var messageLeftHtml = messageHtml.AppendChild(HtmlNode.CreateNode("<div class=\"msg-left\"></div>"));
|
|
|
|
|
// Left
|
|
|
|
|
var messageLeftHtml =
|
|
|
|
|
messageHtml.AppendChild(HtmlNode.CreateNode("<div class=\"msg-left\"></div>"));
|
|
|
|
|
|
|
|
|
|
// Avatar
|
|
|
|
|
messageLeftHtml.AppendChild(
|
|
|
|
|
HtmlNode.CreateNode($"<img class=\"msg-avatar\" src=\"{messageGroup.Author.AvatarUrl}\" />"));
|
|
|
|
|
// Avatar
|
|
|
|
|
messageLeftHtml.AppendChild(
|
|
|
|
|
HtmlNode.CreateNode($"<img class=\"msg-avatar\" src=\"{messageGroup.Author.AvatarUrl}\" />"));
|
|
|
|
|
|
|
|
|
|
// Right
|
|
|
|
|
var messageRightHtml = messageHtml.AppendChild(HtmlNode.CreateNode("<div class=\"msg-right\"></div>"));
|
|
|
|
|
// Right
|
|
|
|
|
var messageRightHtml =
|
|
|
|
|
messageHtml.AppendChild(HtmlNode.CreateNode("<div class=\"msg-right\"></div>"));
|
|
|
|
|
|
|
|
|
|
// Author
|
|
|
|
|
var authorName = HtmlDocument.HtmlEncode(messageGroup.Author.Name);
|
|
|
|
|
messageRightHtml.AppendChild(HtmlNode.CreateNode($"<span class=\"msg-user\">{authorName}</span>"));
|
|
|
|
|
// Author
|
|
|
|
|
var authorName = HtmlDocument.HtmlEncode(messageGroup.Author.Name);
|
|
|
|
|
messageRightHtml.AppendChild(HtmlNode.CreateNode($"<span class=\"msg-user\">{authorName}</span>"));
|
|
|
|
|
|
|
|
|
|
// Date
|
|
|
|
|
var timeStamp = HtmlDocument.HtmlEncode(messageGroup.TimeStamp.ToString(dateFormat));
|
|
|
|
|
messageRightHtml.AppendChild(HtmlNode.CreateNode($"<span class=\"msg-date\">{timeStamp}</span>"));
|
|
|
|
|
// Date
|
|
|
|
|
var timeStamp = HtmlDocument.HtmlEncode(messageGroup.TimeStamp.ToString(dateFormat));
|
|
|
|
|
messageRightHtml.AppendChild(HtmlNode.CreateNode($"<span class=\"msg-date\">{timeStamp}</span>"));
|
|
|
|
|
|
|
|
|
|
// Individual messages
|
|
|
|
|
foreach (var message in messageGroup.Messages)
|
|
|
|
|
{
|
|
|
|
|
// Content
|
|
|
|
|
if (message.Content.IsNotBlank())
|
|
|
|
|
// Individual messages
|
|
|
|
|
foreach (var message in messageGroup.Messages)
|
|
|
|
|
{
|
|
|
|
|
var content = FormatMessageContent(message.Content);
|
|
|
|
|
var contentHtml =
|
|
|
|
|
messageRightHtml.AppendChild(
|
|
|
|
|
HtmlNode.CreateNode($"<div class=\"msg-content\">{content}</div>"));
|
|
|
|
|
|
|
|
|
|
// Edited timestamp
|
|
|
|
|
if (message.EditedTimeStamp != null)
|
|
|
|
|
// Content
|
|
|
|
|
if (message.Content.IsNotBlank())
|
|
|
|
|
{
|
|
|
|
|
contentHtml.AppendChild(
|
|
|
|
|
HtmlNode.CreateNode(
|
|
|
|
|
$"<span class=\"msg-edited\" title=\"{message.EditedTimeStamp.Value.ToString(dateFormat)}\">(edited)</span>"));
|
|
|
|
|
var content = FormatMessageContent(message.Content);
|
|
|
|
|
var contentHtml =
|
|
|
|
|
messageRightHtml.AppendChild(
|
|
|
|
|
HtmlNode.CreateNode($"<div class=\"msg-content\">{content}</div>"));
|
|
|
|
|
|
|
|
|
|
// Edited timestamp
|
|
|
|
|
if (message.EditedTimeStamp != null)
|
|
|
|
|
{
|
|
|
|
|
contentHtml.AppendChild(
|
|
|
|
|
HtmlNode.CreateNode(
|
|
|
|
|
$"<span class=\"msg-edited\" title=\"{message.EditedTimeStamp.Value.ToString(dateFormat)}\">(edited)</span>"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Attachments
|
|
|
|
|
foreach (var attachment in message.Attachments)
|
|
|
|
|
{
|
|
|
|
|
if (attachment.Type == AttachmentType.Image)
|
|
|
|
|
{
|
|
|
|
|
messageRightHtml.AppendChild(
|
|
|
|
|
HtmlNode.CreateNode("<div class=\"msg-attachment\">" +
|
|
|
|
|
$"<a href=\"{attachment.Url}\">" +
|
|
|
|
|
$"<img class=\"msg-attachment\" src=\"{attachment.Url}\" />" +
|
|
|
|
|
"</a>" +
|
|
|
|
|
"</div>"));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
// Attachments
|
|
|
|
|
foreach (var attachment in message.Attachments)
|
|
|
|
|
{
|
|
|
|
|
messageRightHtml.AppendChild(
|
|
|
|
|
HtmlNode.CreateNode("<div class=\"msg-attachment\">" +
|
|
|
|
|
$"<a href=\"{attachment.Url}\">" +
|
|
|
|
|
$"Attachment: {attachment.FileName} ({NormalizeFileSize(attachment.FileSize)})" +
|
|
|
|
|
"</a>" +
|
|
|
|
|
"</div>"));
|
|
|
|
|
if (attachment.Type == AttachmentType.Image)
|
|
|
|
|
{
|
|
|
|
|
messageRightHtml.AppendChild(
|
|
|
|
|
HtmlNode.CreateNode("<div class=\"msg-attachment\">" +
|
|
|
|
|
$"<a href=\"{attachment.Url}\">" +
|
|
|
|
|
$"<img class=\"msg-attachment\" src=\"{attachment.Url}\" />" +
|
|
|
|
|
"</a>" +
|
|
|
|
|
"</div>"));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
messageRightHtml.AppendChild(
|
|
|
|
|
HtmlNode.CreateNode("<div class=\"msg-attachment\">" +
|
|
|
|
|
$"<a href=\"{attachment.Url}\">" +
|
|
|
|
|
$"Attachment: {attachment.FileName} ({NormalizeFileSize(attachment.FileSize)})" +
|
|
|
|
|
"</a>" +
|
|
|
|
|
"</div>"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
doc.Save(filePath);
|
|
|
|
|
doc.Save(filePath);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|