Compare commits

...

44 Commits

Author SHA1 Message Date
Alexey Golub 37be80e9e1 Update version 2020-01-12 21:14:00 +02:00
Alexey Golub 1475ffaf0e Use the new Rune API for handling emoji code points 2020-01-12 21:04:02 +02:00
Alexey Golub aa50dd6dc7 [HTML] Clean up layouts and styles 2020-01-12 20:16:08 +02:00
Alexey Golub 539f57f455 [HTML/TXT] Render exported message count at the bottom
Closes #249
2020-01-11 18:03:07 +02:00
Alexey Golub e1f83997aa [GUI] Fix token textbox alignment 2020-01-11 18:01:35 +02:00
Alexey Golub 26d713a17c Refactor 2020-01-11 17:28:01 +02:00
Alexey Golub 5c2e725739 Move partitioning to message renderer facade 2020-01-11 14:18:04 +02:00
Alexey Golub bf56902134 Refactor partitioning, don't create empty files
Closes #246
2020-01-10 21:05:07 +02:00
Alexey Golub b4df267372 Update NuGet packages 2020-01-10 20:10:01 +02:00
Alexey Golub 4ebc681327 Format XAML with XAMLStyler 2020-01-10 20:06:13 +02:00
Alexey Golub 5cb6adf19e Add an option to not save token
Closes #252
2020-01-10 20:03:31 +02:00
Alexey Golub cec8409ac4 Check if auto-update is enabled in UpdateService 2020-01-03 21:44:30 +02:00
Alexey Golub 3af9421304 Add changelog to CD 2019-12-15 15:35:32 +02:00
Alexey Golub cc6f930dc6 Update readme 2019-12-10 19:27:41 +02:00
Alexey Golub e6aac3cdb9 Update changelog and readme 2019-12-08 14:00:12 +02:00
Alexey Golub 8f1aec241b Update version 2019-12-08 13:45:45 +02:00
Alexey Golub 41c828eb3e [HTML] Refactor renderer 2019-12-07 22:22:39 +02:00
Alexey Golub 964317dac0 Make StringPart readonly struct 2019-12-07 20:16:53 +02:00
Alexey Golub 2a223599f9 Streaming exporter
Fixes #125
Closes #177
2019-12-07 18:43:24 +02:00
Alexey Golub fc38afe6a0 Refactor models and add IHasId/IdBasedEqualityComparer 2019-12-06 00:17:14 +02:00
Alexey Golub 23512dae64 Update dockerfile 2019-12-05 21:04:39 +02:00
Alexey Golub 8382ca725d Migrate to net core 3.1 2019-12-05 21:03:49 +02:00
Alexey Golub 457e14d0b6 Refactor DataService
In preparation for #177
2019-12-03 23:32:48 +02:00
Alexey Golub b830014a46 Make emoji parser less greedy
Fixes #230

Emoji parsing is a terrible endeavour. I'm leaning towards matching fewer emojis and then blaming Unicode for lack of proper classification, than matching a few too many characters.
2019-12-02 21:05:42 +02:00
boomanaiden154 35e1114318 [CSV] Use commas instead of semicolons (#236) 2019-12-01 18:10:24 +02:00
Alexey Golub 610bf4baa3 Fallback channel name to channel ID
Fixes #227
2019-12-01 18:09:23 +02:00
Alexey Golub d84222ea09 Use Polly instead of Failsafe 2019-12-01 17:53:10 +02:00
Alexey Golub cc1bb924e7 [HTML] Adjust embed color to theme 2019-12-01 17:50:10 +02:00
Alexey Golub 0d2ae8b5db Add support for announcement channels
Closes #222
2019-12-01 17:35:56 +02:00
Alexey Golub 632b9f953b Use shared props file 2019-12-01 16:00:45 +02:00
Alexey Golub 9a52c6a6f6 Update readme 2019-11-30 23:31:01 +02:00
Alexey Golub fc8c1c5914 Mention that user tokens are against TOS
Closes #211
2019-11-25 17:22:04 +02:00
Alexey Golub 279a6aee84 Update donation info 2019-11-19 21:17:05 +02:00
Alexey Golub 73ab983056 Mention wiki in readme 2019-11-16 21:00:56 +02:00
Alexey Golub e5a2852165 Use nullable 2019-11-13 19:19:36 +02:00
Alexey Golub 1bf9d9e2e2 Use GitHub actions 2019-11-06 21:04:16 +02:00
Alexey Golub 9299b95250 [CLI] Use Microsoft.Extensions.DependencyInjection instead of Stylet 2019-11-06 00:26:43 +02:00
Alexey Golub 5e0a2c6761 Update dockerfile 2019-11-06 00:09:25 +02:00
Alexey Golub ccbd11e526 Update readme 2019-11-04 15:30:13 +02:00
Alexey Golub 2c36f6a826 Target .NET Core 3 in libraries instead of .NET Std 2.1 2019-09-29 14:19:03 +03:00
Alexey Golub d6980cef8d Cleanup 2019-09-27 01:02:46 +03:00
Alexey Golub fa4bb3b693 [GUI] Move to Microsoft.Xaml.Behaviors.Wpf from System.Windows.Interactivity.WPF 2019-09-27 00:49:29 +03:00
Alexey Golub 05c551f80c Refactor 2019-09-26 20:44:28 +03:00
Alexey Golub 69cfe1845b Migrate to .NET Core 3 2019-09-26 20:06:35 +03:00
118 changed files with 2081 additions and 2092 deletions
+12
View File
@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"xamlstyler.console": {
"version": "3.2001.0",
"commands": [
"xstyler"
]
}
}
}
+1 -2
View File
@@ -1,4 +1,3 @@
github: Tyrrrz github: Tyrrrz
patreon: Tyrrrz patreon: Tyrrrz
open_collective: Tyrrrz custom: ['buymeacoffee.com/Tyrrrz', 'tyrrrz.me/donate']
custom: ['buymeacoffee.com/Tyrrrz']
+66
View File
@@ -0,0 +1,66 @@
name: CD
on:
push:
tags:
- '*'
jobs:
build:
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Install .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.100
- name: Build & publish (CLI)
run: dotnet publish DiscordChatExporter.Cli/ -o DiscordChatExporter.Cli/bin/Publish/ --configuration Release
- name: Build & publish (GUI)
run: dotnet publish DiscordChatExporter.Gui/ -o DiscordChatExporter.Gui/bin/Publish/ --configuration Release
- name: Pack (CLI)
run: Compress-Archive -Path DiscordChatExporter.Cli/bin/Publish/* -DestinationPath DiscordChatExporter.Cli/bin/Publish/Archive.zip -Force
shell: pwsh
- name: Pack (GUI)
run: Compress-Archive -Path DiscordChatExporter.Gui/bin/Publish/* -DestinationPath DiscordChatExporter.Gui/bin/Publish/Archive.zip -Force
shell: pwsh
- name: Create release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: ${{ github.ref }}
body: |
[Changelog](https://github.com/Tyrrrz/DiscordChatExporter/blob/master/Changelog.md)
draft: false
prerelease: false
- name: Upload release asset (CLI)
uses: actions/upload-release-asset@v1.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: DiscordChatExporter.Cli/bin/Publish/Archive.zip
asset_name: DiscordChatExporter.CLI.zip
asset_content_type: application/zip
- name: Upload release asset (GUI)
uses: actions/upload-release-asset@v1.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: DiscordChatExporter.Gui/bin/Publish/Archive.zip
asset_name: DiscordChatExporter.zip
asset_content_type: application/zip
+34
View File
@@ -0,0 +1,34 @@
name: CI
on: [push, pull_request]
jobs:
build:
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Install .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.100
- name: Build & publish (CLI)
run: dotnet publish DiscordChatExporter.Cli/ -o DiscordChatExporter.Cli/bin/Publish/ --configuration Release
- name: Build & publish (GUI)
run: dotnet publish DiscordChatExporter.Gui/ -o DiscordChatExporter.Gui/bin/Publish/ --configuration Release
- name: Upload build artifacts (CLI)
uses: actions/upload-artifact@master
with:
name: DiscordChatExporter.CLI
path: DiscordChatExporter.Cli/bin/Publish/
- name: Upload build artifact (GUI)
uses: actions/upload-artifact@master
with:
name: DiscordChatExporter
path: DiscordChatExporter.Gui/bin/Publish/
+23
View File
@@ -1,3 +1,26 @@
### v2.17 (12-Jan-2020)
- Fixed an issue where an empty file was produced when exporting a channel with no messages (for specified period). With the new behavior, no file will be created and instead a message will be shown to the user informing of the failure.
- [HTML/TXT] Added message count to the bottom of the file. This number shows how many messages were exported as part of the current file. Prior to 2.16 this number was present along with the rest of the metadata at the top, then removed due to changes in the exporter, and now brought back at the end of the file instead.
- [HTML] Fixed an issue where some emojis were not properly rendered.
- [GUI] Added a setting that lets you configure whether to remember the last used token. Previously it was the default behavior, but now you can disable it if you don't want the token persisted on your system.
### v2.16 (08-Dec-2019)
- Migrated from .NET Framework to .NET Core. To run this and future versions of DiscordChatExporter you will need [.NET Core runtime for desktop apps](https://dotnet.microsoft.com/download/dotnet-core/3.1/runtime) (for GUI/CLI version on Windows) or [.NET Core base runtime](https://dotnet.microsoft.com/download/dotnet-core/3.1) (for CLI version on Windows, Linux or macOS).
- Reworked the exporter engine to render output using streaming data source. This means that only a very small portion of messages are cached in memory when exporting. In other words, you are only limited by storage space and not how much RAM you have, so you can now export terabytes of chat logs without hitting out of memory exceptions. I still recommend using partitions if you're exporting very large chat logs (250k+ messages) to HTML, unless you want your computer to catch fire when you try to open the export in browser.
- Changed how partitioned files are named, due to the fact that total number of partitions is no longer known ahead of time.
- Added a warning about automating user accounts to the usage guide in both GUI and CLI versions.
- Added support for announcement (news) channels. You will now be able to see them in the list and export them.
- Fixed various issues that resulted in exceptions during export process.
- [HTML/TXT] Removed message count from the metadata. Due to the fact that the messages are exported as they are streamed, total number of messages is not known ahead of time.
- [HTML] Changed default color of the embed color pill to match the theme.
- [HTML] Changed emoji parser to be less greedy. As a result it should match fewer character sequences that look like emojis but really aren't, but on the other hand it might miss some actual emojis. This means that some standard emojis (i.e. not custom server emojis) may not look like in Discord. This is a compromise I'm willing to take because detecting emojis in text is really hard to get right and not worth it at all.
- [HTML] Some other minor styling adjustments.
- [CSV] Replaced semicolons (;) with (,) as separators.
Please note that CSV export is most likely not going to receive future updates and will remain only as an option for those who wish to use it for text analysis. In other words, it won't contain any of the rich media features like embeds. If you're currently using CSV as a structured output to feed into your program, JSON output will replace that in the future.
### v2.15 (15-Sep-2019) ### v2.15 (15-Sep-2019)
- Improved markdown parser and made it even faster for non-HTML formats. - Improved markdown parser and made it even faster for non-HTML formats.
-21
View File
@@ -1,21 +0,0 @@
New-Item "$PSScriptRoot\Portable\bin" -ItemType Directory -Force
# --- GUI ---
# Get files
$files = @()
$files += Get-Item -Path "$PSScriptRoot\..\License.txt"
$files += Get-ChildItem -Path "$PSScriptRoot\..\DiscordChatExporter.Gui\bin\Release\*" -Include "*.exe", "*.dll", "*.config"
# Pack into archive
$files | Compress-Archive -DestinationPath "$PSScriptRoot\Portable\bin\DiscordChatExporter.zip" -Force
# --- CLI ---
# Get files
$files = @()
$files += Get-Item -Path "$PSScriptRoot\..\License.txt"
$files += Get-ChildItem -Path "$PSScriptRoot\..\DiscordChatExporter.Cli\bin\Release\net46\*" -Include "*.exe", "*.dll", "*.config"
# Pack into archive
$files | Compress-Archive -DestinationPath "$PSScriptRoot\Portable\bin\DiscordChatExporter.CLI.zip" -Force
@@ -16,13 +16,6 @@ namespace DiscordChatExporter.Cli.Commands
{ {
} }
public override async Task ExecuteAsync(IConsole console) public override async Task ExecuteAsync(IConsole console) => await ExportAsync(console, ChannelId);
{
// Get channel
var channel = await DataService.GetChannelAsync(GetToken(), ChannelId);
// Export
await ExportChannelAsync(console, channel);
}
} }
} }
@@ -3,11 +3,9 @@ using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using CliFx.Attributes; using CliFx.Attributes;
using CliFx.Services; using CliFx.Services;
using DiscordChatExporter.Cli.Internal; using CliFx.Utilities;
using DiscordChatExporter.Core.Models; using DiscordChatExporter.Core.Models;
using DiscordChatExporter.Core.Services; using DiscordChatExporter.Core.Services;
using DiscordChatExporter.Core.Services.Helpers;
using Tyrrrz.Extensions;
namespace DiscordChatExporter.Cli.Commands namespace DiscordChatExporter.Cli.Commands
{ {
@@ -17,24 +15,23 @@ namespace DiscordChatExporter.Cli.Commands
protected ExportService ExportService { get; } protected ExportService ExportService { get; }
[CommandOption("format", 'f', Description = "Output file format.")] [CommandOption("format", 'f', Description = "Output file format.")]
public ExportFormat ExportFormat { get; set; } = ExportFormat.HtmlDark; public ExportFormat ExportFormat { get; set; } = ExportFormat.HtmlDark;
[CommandOption("output", 'o', Description = "Output file or directory path.")] [CommandOption("output", 'o', Description = "Output file or directory path.")]
public string OutputPath { get; set; } public string? OutputPath { get; set; }
[CommandOption("after",Description = "Limit to messages sent after this date.")] [CommandOption("after", Description = "Limit to messages sent after this date.")]
public DateTimeOffset? After { get; set; } public DateTimeOffset? After { get; set; }
[CommandOption("before",Description = "Limit to messages sent before this date.")] [CommandOption("before", Description = "Limit to messages sent before this date.")]
public DateTimeOffset? Before { get; set; } public DateTimeOffset? Before { get; set; }
[CommandOption("partition", 'p', Description = "Split output into partitions limited to this number of messages.")] [CommandOption("partition", 'p', Description = "Split output into partitions limited to this number of messages.")]
public int? PartitionLimit { get; set; } public int? PartitionLimit { get; set; }
[CommandOption("dateformat", Description = "Date format used in output.")] [CommandOption("dateformat", Description = "Date format used in output.")]
public string DateFormat { get; set; } public string? DateFormat { get; set; }
protected ExportCommandBase(SettingsService settingsService, DataService dataService, ExportService exportService) protected ExportCommandBase(SettingsService settingsService, DataService dataService, ExportService exportService)
: base(dataService) : base(dataService)
@@ -43,36 +40,32 @@ namespace DiscordChatExporter.Cli.Commands
ExportService = exportService; ExportService = exportService;
} }
protected async Task ExportChannelAsync(IConsole console, Channel channel) protected async Task ExportAsync(IConsole console, Guild guild, Channel channel)
{ {
// Configure settings if (!string.IsNullOrWhiteSpace(DateFormat))
if (!DateFormat.IsNullOrWhiteSpace())
SettingsService.DateFormat = DateFormat; SettingsService.DateFormat = DateFormat;
console.Output.Write($"Exporting channel [{channel.Name}]... "); console.Output.Write($"Exporting channel [{channel.Name}]... ");
using (var progress = new InlineProgress(console)) var progress = console.CreateProgressTicker();
{
// Get chat log
var chatLog = await DataService.GetChatLogAsync(GetToken(), channel, After, Before, progress);
// Generate file path if not set or is a directory var outputPath = OutputPath ?? Directory.GetCurrentDirectory();
var filePath = OutputPath; await ExportService.ExportChatLogAsync(GetToken(), guild, channel,
if (filePath.IsNullOrWhiteSpace() || ExportHelper.IsDirectoryPath(filePath)) outputPath, ExportFormat, PartitionLimit,
{ After, Before, progress);
// Generate default file name
var fileName = ExportHelper.GetDefaultExportFileName(ExportFormat, chatLog.Guild,
chatLog.Channel, After, Before);
// Combine paths console.Output.WriteLine();
filePath = Path.Combine(filePath ?? "", fileName); }
}
// Export protected async Task ExportAsync(IConsole console, Channel channel)
await ExportService.ExportChatLogAsync(chatLog, filePath, ExportFormat, PartitionLimit); {
var guild = await DataService.GetGuildAsync(GetToken(), channel.GuildId);
await ExportAsync(console, guild, channel);
}
// Report successful completion protected async Task ExportAsync(IConsole console, string channelId)
progress.ReportCompletion(); {
} var channel = await DataService.GetChannelAsync(GetToken(), channelId);
await ExportAsync(console, channel);
} }
} }
} }
@@ -3,6 +3,7 @@ using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using CliFx.Attributes; using CliFx.Attributes;
using CliFx.Services; using CliFx.Services;
using DiscordChatExporter.Core.Models.Exceptions;
using DiscordChatExporter.Core.Services; using DiscordChatExporter.Core.Services;
using DiscordChatExporter.Core.Services.Exceptions; using DiscordChatExporter.Core.Services.Exceptions;
@@ -29,7 +30,7 @@ namespace DiscordChatExporter.Cli.Commands
{ {
try try
{ {
await ExportChannelAsync(console, channel); await ExportAsync(console, channel);
} }
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden) catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden)
{ {
@@ -39,6 +40,10 @@ namespace DiscordChatExporter.Cli.Commands
{ {
console.Error.WriteLine("This channel doesn't exist."); console.Error.WriteLine("This channel doesn't exist.");
} }
catch (DomainException ex)
{
console.Error.WriteLine(ex.Message);
}
} }
} }
} }
@@ -4,6 +4,7 @@ using System.Threading.Tasks;
using CliFx.Attributes; using CliFx.Attributes;
using CliFx.Services; using CliFx.Services;
using DiscordChatExporter.Core.Models; using DiscordChatExporter.Core.Models;
using DiscordChatExporter.Core.Models.Exceptions;
using DiscordChatExporter.Core.Services; using DiscordChatExporter.Core.Services;
using DiscordChatExporter.Core.Services.Exceptions; using DiscordChatExporter.Core.Services.Exceptions;
@@ -26,14 +27,14 @@ namespace DiscordChatExporter.Cli.Commands
var channels = await DataService.GetGuildChannelsAsync(GetToken(), GuildId); var channels = await DataService.GetGuildChannelsAsync(GetToken(), GuildId);
// Filter and order channels // Filter and order channels
channels = channels.Where(c => c.Type == ChannelType.GuildTextChat).OrderBy(c => c.Name).ToArray(); channels = channels.Where(c => c.Type.IsExportable()).OrderBy(c => c.Name).ToArray();
// Loop through channels // Loop through channels
foreach (var channel in channels) foreach (var channel in channels)
{ {
try try
{ {
await ExportChannelAsync(console, channel); await ExportAsync(console, channel);
} }
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden) catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden)
{ {
@@ -43,6 +44,10 @@ namespace DiscordChatExporter.Cli.Commands
{ {
console.Error.WriteLine("This channel doesn't exist."); console.Error.WriteLine("This channel doesn't exist.");
} }
catch (DomainException ex)
{
console.Error.WriteLine(ex.Message);
}
} }
} }
} }
@@ -24,7 +24,7 @@ namespace DiscordChatExporter.Cli.Commands
var channels = await DataService.GetGuildChannelsAsync(GetToken(), GuildId); var channels = await DataService.GetGuildChannelsAsync(GetToken(), GuildId);
// Filter and order channels // Filter and order channels
channels = channels.Where(c => c.Type == ChannelType.GuildTextChat).OrderBy(c => c.Name).ToArray(); channels = channels.Where(c => c.Type.IsExportable()).OrderBy(c => c.Name).ToArray();
// Print result // Print result
foreach (var channel in channels) foreach (var channel in channels)
@@ -18,6 +18,7 @@ namespace DiscordChatExporter.Cli.Commands
console.Output.WriteLine(" 4. Select \"Local Storage\" > \"https://discordapp.com\" on the left"); console.Output.WriteLine(" 4. Select \"Local Storage\" > \"https://discordapp.com\" on the left");
console.Output.WriteLine(" 5. Press Ctrl+R to reload"); console.Output.WriteLine(" 5. Press Ctrl+R to reload");
console.Output.WriteLine(" 6. Find \"token\" at the bottom and copy the value"); console.Output.WriteLine(" 6. Find \"token\" at the bottom and copy the value");
console.Output.WriteLine(" * Automating user accounts is technically against TOS, use at your own risk.");
console.Output.WriteLine(); console.Output.WriteLine();
console.WithForegroundColor(ConsoleColor.White, () => console.Output.WriteLine("To get bot token:")); console.WithForegroundColor(ConsoleColor.White, () => console.Output.WriteLine("To get bot token:"));
@@ -1,20 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<Import Project="../DiscordChatExporter.props" />
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFrameworks>net46;netcoreapp2.1</TargetFrameworks>
<Version>2.15</Version>
<Company>Tyrrrz</Company>
<Copyright>Copyright (c) Alexey Golub</Copyright>
<ApplicationIcon>..\favicon.ico</ApplicationIcon> <ApplicationIcon>..\favicon.ico</ApplicationIcon>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<LangVersion>latest</LangVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="CliFx" Version="0.0.5" /> <PackageReference Include="CliFx" Version="0.0.8" />
<PackageReference Include="Stylet" Version="1.2.0" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.0" />
<PackageReference Include="Tyrrrz.Extensions" Version="1.6.2" /> <PackageReference Include="Tyrrrz.Extensions" Version="1.6.5" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -1,48 +0,0 @@
using System;
using CliFx.Services;
namespace DiscordChatExporter.Cli.Internal
{
internal class InlineProgress : IProgress<double>, IDisposable
{
private readonly IConsole _console;
private string _lastOutput = "";
private bool _isCompleted;
public InlineProgress(IConsole console)
{
_console = console;
}
private void ResetCursorPosition()
{
foreach (var c in _lastOutput)
_console.Output.Write('\b');
}
public void Report(double progress)
{
// If output is not redirected - reset cursor position and write progress
if (!_console.IsOutputRedirected)
{
ResetCursorPosition();
_console.Output.Write(_lastOutput = $"{progress:P1}");
}
}
public void ReportCompletion() => _isCompleted = true;
public void Dispose()
{
// If output is not redirected - reset cursor position
if (!_console.IsOutputRedirected)
{
ResetCursorPosition();
}
// Inform about completion
_console.Output.WriteLine(_isCompleted ? "Completed ✓" : "Failed X");
}
}
}
+21 -12
View File
@@ -1,33 +1,42 @@
using System.Threading.Tasks; using System;
using System.Threading.Tasks;
using CliFx; using CliFx;
using DiscordChatExporter.Cli.Commands;
using DiscordChatExporter.Core.Services; using DiscordChatExporter.Core.Services;
using StyletIoC; using Microsoft.Extensions.DependencyInjection;
namespace DiscordChatExporter.Cli namespace DiscordChatExporter.Cli
{ {
public static class Program public static class Program
{ {
private static IContainer BuildContainer() private static IServiceProvider ConfigureServices()
{ {
var builder = new StyletIoCBuilder(); var services = new ServiceCollection();
// Autobind the .Services assembly // Register services
builder.Autobind(typeof(DataService).Assembly); services.AddSingleton<DataService>();
services.AddSingleton<ExportService>();
services.AddSingleton<SettingsService>();
// Bind settings as singleton // Register commands
builder.Bind<SettingsService>().ToSelf().InSingletonScope(); services.AddTransient<ExportChannelCommand>();
services.AddTransient<ExportDirectMessagesCommand>();
services.AddTransient<ExportGuildCommand>();
services.AddTransient<GetChannelsCommand>();
services.AddTransient<GetDirectMessageChannelsCommand>();
services.AddTransient<GetGuildsCommand>();
services.AddTransient<GuideCommand>();
// Set instance return services.BuildServiceProvider();
return builder.BuildContainer();
} }
public static Task<int> Main(string[] args) public static Task<int> Main(string[] args)
{ {
var container = BuildContainer(); var serviceProvider = ConfigureServices();
return new CliApplicationBuilder() return new CliApplicationBuilder()
.AddCommandsFromThisAssembly() .AddCommandsFromThisAssembly()
.UseCommandFactory(schema => (ICommand) container.Get(schema.Type)) .UseCommandFactory(schema => (ICommand) serviceProvider.GetService(schema.Type))
.Build() .Build()
.RunAsync(args); .RunAsync(args);
} }
@@ -1,18 +1,16 @@
using Tyrrrz.Extensions; namespace DiscordChatExporter.Core.Markdown.Ast
namespace DiscordChatExporter.Core.Markdown.Nodes
{ {
public class EmojiNode : Node public class EmojiNode : Node
{ {
public string Id { get; } public string? Id { get; }
public string Name { get; } public string Name { get; }
public bool IsAnimated { get; } public bool IsAnimated { get; }
public bool IsCustomEmoji => !Id.IsNullOrWhiteSpace(); public bool IsCustomEmoji => !string.IsNullOrWhiteSpace(Id);
public EmojiNode(string id, string name, bool isAnimated) public EmojiNode(string? id, string name, bool isAnimated)
{ {
Id = id; Id = id;
Name = name; Name = name;
@@ -1,6 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
namespace DiscordChatExporter.Core.Markdown.Nodes namespace DiscordChatExporter.Core.Markdown.Ast
{ {
public class FormattedNode : Node public class FormattedNode : Node
{ {
@@ -1,4 +1,4 @@
namespace DiscordChatExporter.Core.Markdown.Nodes namespace DiscordChatExporter.Core.Markdown.Ast
{ {
public class InlineCodeBlockNode : Node public class InlineCodeBlockNode : Node
{ {
@@ -1,4 +1,4 @@
namespace DiscordChatExporter.Core.Markdown.Nodes namespace DiscordChatExporter.Core.Markdown.Ast
{ {
public class LinkNode : Node public class LinkNode : Node
{ {
@@ -1,4 +1,4 @@
namespace DiscordChatExporter.Core.Markdown.Nodes namespace DiscordChatExporter.Core.Markdown.Ast
{ {
public class MentionNode : Node public class MentionNode : Node
{ {
@@ -1,4 +1,4 @@
namespace DiscordChatExporter.Core.Markdown.Nodes namespace DiscordChatExporter.Core.Markdown.Ast
{ {
public enum MentionType public enum MentionType
{ {
@@ -1,4 +1,4 @@
namespace DiscordChatExporter.Core.Markdown.Nodes namespace DiscordChatExporter.Core.Markdown.Ast
{ {
public class MultiLineCodeBlockNode : Node public class MultiLineCodeBlockNode : Node
{ {
@@ -0,0 +1,6 @@
namespace DiscordChatExporter.Core.Markdown.Ast
{
public abstract class Node
{
}
}
@@ -1,4 +1,4 @@
namespace DiscordChatExporter.Core.Markdown.Nodes namespace DiscordChatExporter.Core.Markdown.Ast
{ {
public enum TextFormatting public enum TextFormatting
{ {
@@ -1,4 +1,4 @@
namespace DiscordChatExporter.Core.Markdown.Nodes namespace DiscordChatExporter.Core.Markdown.Ast
{ {
public class TextNode : Node public class TextNode : Node
{ {
@@ -1,12 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<Import Project="../DiscordChatExporter.props" />
<PropertyGroup>
<TargetFrameworks>net46;netstandard2.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Tyrrrz.Extensions" Version="1.6.2" /> <PackageReference Include="Tyrrrz.Extensions" Version="1.6.5" />
</ItemGroup> </ItemGroup>
</Project> </Project>
@@ -16,9 +16,9 @@ namespace DiscordChatExporter.Core.Markdown.Internal
{ {
} }
public ParsedMatch<T> Match(StringPart stringPart) public ParsedMatch<T>? Match(StringPart stringPart)
{ {
ParsedMatch<T> earliestMatch = null; ParsedMatch<T>? earliestMatch = null;
// Try to match the input with each matcher and get the match with the lowest start index // Try to match the input with each matcher and get the match with the lowest start index
foreach (var matcher in _matchers) foreach (var matcher in _matchers)
@@ -1,20 +1,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace DiscordChatExporter.Core.Markdown.Internal namespace DiscordChatExporter.Core.Markdown.Internal
{ {
internal static class Extensions internal static class Extensions
{ {
public static StringPart Shrink(this StringPart stringPart, int newStartIndex, int newLength) =>
new StringPart(stringPart.Target, newStartIndex, newLength);
public static StringPart Shrink(this StringPart stringPart, int newStartIndex) =>
stringPart.Shrink(newStartIndex, stringPart.EndIndex - newStartIndex);
public static StringPart Shrink(this StringPart stringPart, Capture capture) =>
stringPart.Shrink(capture.Index, capture.Length);
public static IEnumerable<ParsedMatch<T>> MatchAll<T>(this IMatcher<T> matcher, StringPart stringPart, public static IEnumerable<ParsedMatch<T>> MatchAll<T>(this IMatcher<T> matcher, StringPart stringPart,
Func<StringPart, T> fallbackTransform) Func<StringPart, T> fallbackTransform)
{ {
@@ -23,7 +13,7 @@ namespace DiscordChatExporter.Core.Markdown.Internal
while (currentIndex < stringPart.EndIndex) while (currentIndex < stringPart.EndIndex)
{ {
// Find a match within this segment // Find a match within this segment
var match = matcher.Match(stringPart.Shrink(currentIndex, stringPart.EndIndex - currentIndex)); var match = matcher.Match(stringPart.Slice(currentIndex, stringPart.EndIndex - currentIndex));
// If there's no match - break // If there's no match - break
if (match == null) if (match == null)
@@ -32,7 +22,7 @@ namespace DiscordChatExporter.Core.Markdown.Internal
// If this match doesn't start immediately at current index - transform and yield fallback first // If this match doesn't start immediately at current index - transform and yield fallback first
if (match.StringPart.StartIndex > currentIndex) if (match.StringPart.StartIndex > currentIndex)
{ {
var fallbackPart = stringPart.Shrink(currentIndex, match.StringPart.StartIndex - currentIndex); var fallbackPart = stringPart.Slice(currentIndex, match.StringPart.StartIndex - currentIndex);
yield return new ParsedMatch<T>(fallbackPart, fallbackTransform(fallbackPart)); yield return new ParsedMatch<T>(fallbackPart, fallbackTransform(fallbackPart));
} }
@@ -46,7 +36,7 @@ namespace DiscordChatExporter.Core.Markdown.Internal
// If EOL wasn't reached - transform and yield remaining part as fallback // If EOL wasn't reached - transform and yield remaining part as fallback
if (currentIndex < stringPart.EndIndex) if (currentIndex < stringPart.EndIndex)
{ {
var fallbackPart = stringPart.Shrink(currentIndex); var fallbackPart = stringPart.Slice(currentIndex);
yield return new ParsedMatch<T>(fallbackPart, fallbackTransform(fallbackPart)); yield return new ParsedMatch<T>(fallbackPart, fallbackTransform(fallbackPart));
} }
} }
@@ -2,6 +2,6 @@
{ {
internal interface IMatcher<T> internal interface IMatcher<T>
{ {
ParsedMatch<T> Match(StringPart stringPart); ParsedMatch<T>? Match(StringPart stringPart);
} }
} }
@@ -1,8 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace DiscordChatExporter.Core.Markdown.Internal namespace DiscordChatExporter.Core.Markdown.Internal
@@ -23,7 +19,7 @@ namespace DiscordChatExporter.Core.Markdown.Internal
{ {
} }
public ParsedMatch<T> Match(StringPart stringPart) public ParsedMatch<T>? Match(StringPart stringPart)
{ {
var match = _regex.Match(stringPart.Target, stringPart.StartIndex, stringPart.Length); var match = _regex.Match(stringPart.Target, stringPart.StartIndex, stringPart.Length);
if (!match.Success) if (!match.Success)
@@ -37,8 +33,8 @@ namespace DiscordChatExporter.Core.Markdown.Internal
if (!_regex.IsMatch(stringPart.Target.Substring(0, stringPart.EndIndex), stringPart.StartIndex)) if (!_regex.IsMatch(stringPart.Target.Substring(0, stringPart.EndIndex), stringPart.StartIndex))
return null; return null;
var stringPartShrunk = stringPart.Shrink(match.Index, match.Length); var stringPartMatch = stringPart.Slice(match.Index, match.Length);
return new ParsedMatch<T>(stringPartShrunk, _transform(stringPartShrunk, match)); return new ParsedMatch<T>(stringPartMatch, _transform(stringPartMatch, match));
} }
} }
} }
@@ -20,14 +20,14 @@ namespace DiscordChatExporter.Core.Markdown.Internal
{ {
} }
public ParsedMatch<T> Match(StringPart stringPart) public ParsedMatch<T>? Match(StringPart stringPart)
{ {
var index = stringPart.Target.IndexOf(_needle, stringPart.StartIndex, stringPart.Length, _comparison); var index = stringPart.Target.IndexOf(_needle, stringPart.StartIndex, stringPart.Length, _comparison);
if (index >= 0) if (index >= 0)
{ {
var stringPartShrunk = stringPart.Shrink(index, _needle.Length); var stringPartMatch = stringPart.Slice(index, _needle.Length);
return new ParsedMatch<T>(stringPartShrunk, _transform(stringPartShrunk)); return new ParsedMatch<T>(stringPartMatch, _transform(stringPartMatch));
} }
return null; return null;
@@ -1,6 +1,8 @@
namespace DiscordChatExporter.Core.Markdown.Internal using System.Text.RegularExpressions;
namespace DiscordChatExporter.Core.Markdown.Internal
{ {
internal class StringPart internal readonly struct StringPart
{ {
public string Target { get; } public string Target { get; }
@@ -23,6 +25,12 @@
{ {
} }
public StringPart Slice(int newStartIndex, int newLength) => new StringPart(Target, newStartIndex, newLength);
public StringPart Slice(int newStartIndex) => Slice(newStartIndex, EndIndex - newStartIndex);
public StringPart Slice(Capture capture) => Slice(capture.Index, capture.Length);
public override string ToString() => Target.Substring(StartIndex, Length); public override string ToString() => Target.Substring(StartIndex, Length);
} }
} }
@@ -1,9 +1,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using DiscordChatExporter.Core.Markdown.Ast;
using DiscordChatExporter.Core.Markdown.Internal; using DiscordChatExporter.Core.Markdown.Internal;
using DiscordChatExporter.Core.Markdown.Nodes;
using Tyrrrz.Extensions;
namespace DiscordChatExporter.Core.Markdown namespace DiscordChatExporter.Core.Markdown
{ {
@@ -17,57 +16,57 @@ namespace DiscordChatExporter.Core.Markdown
// Capture any character until the earliest double asterisk not followed by an asterisk // Capture any character until the earliest double asterisk not followed by an asterisk
private static readonly IMatcher<Node> BoldFormattedNodeMatcher = new RegexMatcher<Node>( private static readonly IMatcher<Node> BoldFormattedNodeMatcher = new RegexMatcher<Node>(
new Regex("\\*\\*(.+?)\\*\\*(?!\\*)", DefaultRegexOptions | RegexOptions.Singleline), new Regex("\\*\\*(.+?)\\*\\*(?!\\*)", DefaultRegexOptions | RegexOptions.Singleline),
(p, m) => new FormattedNode(TextFormatting.Bold, Parse(p.Shrink(m.Groups[1])))); (p, m) => new FormattedNode(TextFormatting.Bold, Parse(p.Slice(m.Groups[1]))));
// Capture any character until the earliest single asterisk not preceded or followed by an asterisk // Capture any character until the earliest single asterisk not preceded or followed by an asterisk
// Opening asterisk must not be followed by whitespace // Opening asterisk must not be followed by whitespace
// Closing asterisk must not be preceded by whitespace // Closing asterisk must not be preceded by whitespace
private static readonly IMatcher<Node> ItalicFormattedNodeMatcher = new RegexMatcher<Node>( private static readonly IMatcher<Node> ItalicFormattedNodeMatcher = new RegexMatcher<Node>(
new Regex("\\*(?!\\s)(.+?)(?<!\\s|\\*)\\*(?!\\*)", DefaultRegexOptions | RegexOptions.Singleline), new Regex("\\*(?!\\s)(.+?)(?<!\\s|\\*)\\*(?!\\*)", DefaultRegexOptions | RegexOptions.Singleline),
(p, m) => new FormattedNode(TextFormatting.Italic, Parse(p.Shrink(m.Groups[1])))); (p, m) => new FormattedNode(TextFormatting.Italic, Parse(p.Slice(m.Groups[1]))));
// Capture any character until the earliest triple asterisk not followed by an asterisk // Capture any character until the earliest triple asterisk not followed by an asterisk
private static readonly IMatcher<Node> ItalicBoldFormattedNodeMatcher = new RegexMatcher<Node>( private static readonly IMatcher<Node> ItalicBoldFormattedNodeMatcher = new RegexMatcher<Node>(
new Regex("\\*(\\*\\*.+?\\*\\*)\\*(?!\\*)", DefaultRegexOptions | RegexOptions.Singleline), new Regex("\\*(\\*\\*.+?\\*\\*)\\*(?!\\*)", DefaultRegexOptions | RegexOptions.Singleline),
(p, m) => new FormattedNode(TextFormatting.Italic, Parse(p.Shrink(m.Groups[1]), BoldFormattedNodeMatcher))); (p, m) => new FormattedNode(TextFormatting.Italic, Parse(p.Slice(m.Groups[1]), BoldFormattedNodeMatcher)));
// Capture any character except underscore until an underscore // Capture any character except underscore until an underscore
// Closing underscore must not be followed by a word character // Closing underscore must not be followed by a word character
private static readonly IMatcher<Node> ItalicAltFormattedNodeMatcher = new RegexMatcher<Node>( private static readonly IMatcher<Node> ItalicAltFormattedNodeMatcher = new RegexMatcher<Node>(
new Regex("_([^_]+)_(?!\\w)", DefaultRegexOptions | RegexOptions.Singleline), new Regex("_([^_]+)_(?!\\w)", DefaultRegexOptions | RegexOptions.Singleline),
(p, m) => new FormattedNode(TextFormatting.Italic, Parse(p.Shrink(m.Groups[1])))); (p, m) => new FormattedNode(TextFormatting.Italic, Parse(p.Slice(m.Groups[1]))));
// Capture any character until the earliest double underscore not followed by an underscore // Capture any character until the earliest double underscore not followed by an underscore
private static readonly IMatcher<Node> UnderlineFormattedNodeMatcher = new RegexMatcher<Node>( private static readonly IMatcher<Node> UnderlineFormattedNodeMatcher = new RegexMatcher<Node>(
new Regex("__(.+?)__(?!_)", DefaultRegexOptions | RegexOptions.Singleline), new Regex("__(.+?)__(?!_)", DefaultRegexOptions | RegexOptions.Singleline),
(p, m) => new FormattedNode(TextFormatting.Underline, Parse(p.Shrink(m.Groups[1])))); (p, m) => new FormattedNode(TextFormatting.Underline, Parse(p.Slice(m.Groups[1]))));
// Capture any character until the earliest triple underscore not followed by an underscore // Capture any character until the earliest triple underscore not followed by an underscore
private static readonly IMatcher<Node> ItalicUnderlineFormattedNodeMatcher = new RegexMatcher<Node>( private static readonly IMatcher<Node> ItalicUnderlineFormattedNodeMatcher = new RegexMatcher<Node>(
new Regex("_(__.+?__)_(?!_)", DefaultRegexOptions | RegexOptions.Singleline), new Regex("_(__.+?__)_(?!_)", DefaultRegexOptions | RegexOptions.Singleline),
(p, m) => new FormattedNode(TextFormatting.Italic, Parse(p.Shrink(m.Groups[1]), UnderlineFormattedNodeMatcher))); (p, m) => new FormattedNode(TextFormatting.Italic, Parse(p.Slice(m.Groups[1]), UnderlineFormattedNodeMatcher)));
// Capture any character until the earliest double tilde // Capture any character until the earliest double tilde
private static readonly IMatcher<Node> StrikethroughFormattedNodeMatcher = new RegexMatcher<Node>( private static readonly IMatcher<Node> StrikethroughFormattedNodeMatcher = new RegexMatcher<Node>(
new Regex("~~(.+?)~~", DefaultRegexOptions | RegexOptions.Singleline), new Regex("~~(.+?)~~", DefaultRegexOptions | RegexOptions.Singleline),
(p, m) => new FormattedNode(TextFormatting.Strikethrough, Parse(p.Shrink(m.Groups[1])))); (p, m) => new FormattedNode(TextFormatting.Strikethrough, Parse(p.Slice(m.Groups[1]))));
// Capture any character until the earliest double pipe // Capture any character until the earliest double pipe
private static readonly IMatcher<Node> SpoilerFormattedNodeMatcher = new RegexMatcher<Node>( private static readonly IMatcher<Node> SpoilerFormattedNodeMatcher = new RegexMatcher<Node>(
new Regex("\\|\\|(.+?)\\|\\|", DefaultRegexOptions | RegexOptions.Singleline), new Regex("\\|\\|(.+?)\\|\\|", DefaultRegexOptions | RegexOptions.Singleline),
(p, m) => new FormattedNode(TextFormatting.Spoiler, Parse(p.Shrink(m.Groups[1])))); (p, m) => new FormattedNode(TextFormatting.Spoiler, Parse(p.Slice(m.Groups[1]))));
// Capture any character until the end of the line // Capture any character until the end of the line
// Opening 'greater than' character must be followed by whitespace // Opening 'greater than' character must be followed by whitespace
private static readonly IMatcher<Node> SingleLineQuoteNodeMatcher = new RegexMatcher<Node>( private static readonly IMatcher<Node> SingleLineQuoteNodeMatcher = new RegexMatcher<Node>(
new Regex("^>\\s(.+)\r?\n?", DefaultRegexOptions), new Regex("^>\\s(.+)\r?\n?", DefaultRegexOptions),
(p, m) => new FormattedNode(TextFormatting.Quote, Parse(p.Shrink(m.Groups[1])))); (p, m) => new FormattedNode(TextFormatting.Quote, Parse(p.Slice(m.Groups[1]))));
// Capture any character until the end of the input // Capture any character until the end of the input
// Opening 'greater than' characters must be followed by whitespace // Opening 'greater than' characters must be followed by whitespace
private static readonly IMatcher<Node> MultiLineQuoteNodeMatcher = new RegexMatcher<Node>( private static readonly IMatcher<Node> MultiLineQuoteNodeMatcher = new RegexMatcher<Node>(
new Regex("^>>>\\s(.+)", DefaultRegexOptions | RegexOptions.Singleline), new Regex("^>>>\\s(.+)", DefaultRegexOptions | RegexOptions.Singleline),
(p, m) => new FormattedNode(TextFormatting.Quote, Parse(p.Shrink(m.Groups[1])))); (p, m) => new FormattedNode(TextFormatting.Quote, Parse(p.Slice(m.Groups[1]))));
/* Code blocks */ /* Code blocks */
@@ -114,18 +113,18 @@ namespace DiscordChatExporter.Core.Markdown
/* Emojis */ /* Emojis */
// Capture any country flag emoji (two regional indicator surrogate pairs) // Capture any country flag emoji (two regional indicator surrogate pairs)
// ... or "symbol/other" character // ... or "miscellaneous symbol" character
// ... or surrogate pair // ... or surrogate pair
// ... or digit followed by enclosing mark // ... or digit followed by enclosing mark
// (this does not match all emojis in Discord but it's reasonably accurate enough) // (this does not match all emojis in Discord but it's reasonably accurate enough)
private static readonly IMatcher<Node> StandardEmojiNodeMatcher = new RegexMatcher<Node>( private static readonly IMatcher<Node> StandardEmojiNodeMatcher = new RegexMatcher<Node>(
new Regex("((?:[\\uD83C][\\uDDE6-\\uDDFF]){2}|\\p{So}|\\p{Cs}{2}|\\d\\p{Me})", DefaultRegexOptions), new Regex("((?:[\\uD83C][\\uDDE6-\\uDDFF]){2}|[\\u2600-\\u26FF]|\\p{Cs}{2}|\\d\\p{Me})", DefaultRegexOptions),
m => new EmojiNode(m.Groups[1].Value)); m => new EmojiNode(m.Groups[1].Value));
// Capture <:lul:123456> or <a:lul:123456> // Capture <:lul:123456> or <a:lul:123456>
private static readonly IMatcher<Node> CustomEmojiNodeMatcher = new RegexMatcher<Node>( private static readonly IMatcher<Node> CustomEmojiNodeMatcher = new RegexMatcher<Node>(
new Regex("<(a)?:(.+?):(\\d+?)>", DefaultRegexOptions), new Regex("<(a)?:(.+?):(\\d+?)>", DefaultRegexOptions),
m => new EmojiNode(m.Groups[3].Value, m.Groups[2].Value, !m.Groups[1].Value.IsNullOrWhiteSpace())); m => new EmojiNode(m.Groups[3].Value, m.Groups[2].Value, !string.IsNullOrWhiteSpace(m.Groups[1].Value)));
/* Links */ /* Links */
@@ -211,6 +210,7 @@ namespace DiscordChatExporter.Core.Markdown
StandardEmojiNodeMatcher, StandardEmojiNodeMatcher,
CustomEmojiNodeMatcher); CustomEmojiNodeMatcher);
// Minimal set of matchers for non-multimedia formats (e.g. plain text)
private static readonly IMatcher<Node> MinimalAggregateNodeMatcher = new AggregateMatcher<Node>( private static readonly IMatcher<Node> MinimalAggregateNodeMatcher = new AggregateMatcher<Node>(
// Mentions // Mentions
EveryoneMentionNodeMatcher, EveryoneMentionNodeMatcher,
@@ -220,7 +220,6 @@ namespace DiscordChatExporter.Core.Markdown
RoleMentionNodeMatcher, RoleMentionNodeMatcher,
// Emoji // Emoji
StandardEmojiNodeMatcher,
CustomEmojiNodeMatcher); CustomEmojiNodeMatcher);
private static IReadOnlyList<Node> Parse(StringPart stringPart, IMatcher<Node> matcher) => private static IReadOnlyList<Node> Parse(StringPart stringPart, IMatcher<Node> matcher) =>
@@ -1,6 +0,0 @@
namespace DiscordChatExporter.Core.Markdown.Nodes
{
public abstract class Node
{
}
}
@@ -6,7 +6,7 @@ namespace DiscordChatExporter.Core.Models
{ {
// https://discordapp.com/developers/docs/resources/channel#attachment-object // https://discordapp.com/developers/docs/resources/channel#attachment-object
public partial class Attachment public partial class Attachment : IHasId
{ {
public string Id { get; } public string Id { get; }
+5 -5
View File
@@ -2,21 +2,21 @@
{ {
// https://discordapp.com/developers/docs/resources/channel#channel-object // https://discordapp.com/developers/docs/resources/channel#channel-object
public partial class Channel public partial class Channel : IHasId
{ {
public string Id { get; } public string Id { get; }
public string ParentId { get; } public string? ParentId { get; }
public string GuildId { get; } public string GuildId { get; }
public string Name { get; } public string Name { get; }
public string Topic { get; } public string? Topic { get; }
public ChannelType Type { get; } public ChannelType Type { get; }
public Channel(string id, string parentId, string guildId, string name, string topic, ChannelType type) public Channel(string id, string? parentId, string guildId, string name, string? topic, ChannelType type)
{ {
Id = id; Id = id;
ParentId = parentId; ParentId = parentId;
@@ -32,6 +32,6 @@
public partial class Channel public partial class Channel
{ {
public static Channel CreateDeletedChannel(string id) => public static Channel CreateDeletedChannel(string id) =>
new Channel(id, null, null, "deleted-channel", null, ChannelType.GuildTextChat); new Channel(id, null, "unknown-guild", "deleted-channel", null, ChannelType.GuildTextChat);
} }
} }
@@ -1,6 +1,7 @@
namespace DiscordChatExporter.Core.Models namespace DiscordChatExporter.Core.Models
{ {
// https://discordapp.com/developers/docs/resources/channel#channel-object-channel-types // https://discordapp.com/developers/docs/resources/channel#channel-object-channel-types
// Order of enum fields needs to match the order in the docs.
public enum ChannelType public enum ChannelType
{ {
@@ -8,6 +9,8 @@
DirectTextChat, DirectTextChat,
GuildVoiceChat, GuildVoiceChat,
DirectGroupTextChat, DirectGroupTextChat,
Category GuildCategory,
GuildNews,
GuildStore
} }
} }
@@ -1,33 +0,0 @@
using System;
using System.Collections.Generic;
namespace DiscordChatExporter.Core.Models
{
public class ChatLog
{
public Guild Guild { get; }
public Channel Channel { get; }
public DateTimeOffset? After { get; }
public DateTimeOffset? Before { get; }
public IReadOnlyList<Message> Messages { get; }
public Mentionables Mentionables { get; }
public ChatLog(Guild guild, Channel channel, DateTimeOffset? after, DateTimeOffset? before,
IReadOnlyList<Message> messages, Mentionables mentionables)
{
Guild = guild;
Channel = channel;
After = after;
Before = before;
Messages = messages;
Mentionables = mentionables;
}
public override string ToString() => $"{Guild.Name} | {Channel.Name}";
}
}
@@ -1,12 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<Import Project="../DiscordChatExporter.props" />
<PropertyGroup>
<TargetFrameworks>net46;netstandard2.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Tyrrrz.Extensions" Version="1.6.2" /> <PackageReference Include="Tyrrrz.Extensions" Version="1.6.5" />
</ItemGroup> </ItemGroup>
</Project> </Project>
+11 -11
View File
@@ -8,28 +8,28 @@ namespace DiscordChatExporter.Core.Models
public class Embed public class Embed
{ {
public string Title { get; } public string? Title { get; }
public string Url { get; } public string? Url { get; }
public DateTimeOffset? Timestamp { get; } public DateTimeOffset? Timestamp { get; }
public Color Color { get; } public Color? Color { get; }
public EmbedAuthor Author { get; } public EmbedAuthor? Author { get; }
public string Description { get; } public string? Description { get; }
public IReadOnlyList<EmbedField> Fields { get; } public IReadOnlyList<EmbedField> Fields { get; }
public EmbedImage Thumbnail { get; } public EmbedImage? Thumbnail { get; }
public EmbedImage Image { get; } public EmbedImage? Image { get; }
public EmbedFooter Footer { get; } public EmbedFooter? Footer { get; }
public Embed(string title, string url, DateTimeOffset? timestamp, Color color, EmbedAuthor author, string description, public Embed(string? title, string? url, DateTimeOffset? timestamp, Color? color, EmbedAuthor? author, string? description,
IReadOnlyList<EmbedField> fields, EmbedImage thumbnail, EmbedImage image, EmbedFooter footer) IReadOnlyList<EmbedField> fields, EmbedImage? thumbnail, EmbedImage? image, EmbedFooter? footer)
{ {
Title = title; Title = title;
Url = url; Url = url;
@@ -43,6 +43,6 @@ namespace DiscordChatExporter.Core.Models
Footer = footer; Footer = footer;
} }
public override string ToString() => Title; public override string ToString() => Title ?? "<untitled embed>";
} }
} }
@@ -4,19 +4,19 @@ namespace DiscordChatExporter.Core.Models
public class EmbedAuthor public class EmbedAuthor
{ {
public string Name { get; } public string? Name { get; }
public string Url { get; } public string? Url { get; }
public string IconUrl { get; } public string? IconUrl { get; }
public EmbedAuthor(string name, string url, string iconUrl) public EmbedAuthor(string? name, string? url, string? iconUrl)
{ {
Name = name; Name = name;
Url = url; Url = url;
IconUrl = iconUrl; IconUrl = iconUrl;
} }
public override string ToString() => Name; public override string ToString() => Name ?? "<unnamed author>";
} }
} }
@@ -6,9 +6,9 @@ namespace DiscordChatExporter.Core.Models
{ {
public string Text { get; } public string Text { get; }
public string IconUrl { get; } public string? IconUrl { get; }
public EmbedFooter(string text, string iconUrl) public EmbedFooter(string text, string? iconUrl)
{ {
Text = text; Text = text;
IconUrl = iconUrl; IconUrl = iconUrl;
@@ -4,13 +4,13 @@ namespace DiscordChatExporter.Core.Models
public class EmbedImage public class EmbedImage
{ {
public string Url { get; } public string? Url { get; }
public int? Width { get; } public int? Width { get; }
public int? Height { get; } public int? Height { get; }
public EmbedImage(string url, int? width, int? height) public EmbedImage(string? url, int? width, int? height)
{ {
Url = url; Url = url;
Height = height; Height = height;
+28 -12
View File
@@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using Tyrrrz.Extensions; using Tyrrrz.Extensions;
namespace DiscordChatExporter.Core.Models namespace DiscordChatExporter.Core.Models
@@ -8,7 +9,7 @@ namespace DiscordChatExporter.Core.Models
public partial class Emoji public partial class Emoji
{ {
public string Id { get; } public string? Id { get; }
public string Name { get; } public string Name { get; }
@@ -16,7 +17,7 @@ namespace DiscordChatExporter.Core.Models
public string ImageUrl { get; } public string ImageUrl { get; }
public Emoji(string id, string name, bool isAnimated) public Emoji(string? id, string name, bool isAnimated)
{ {
Id = id; Id = id;
Name = name; Name = name;
@@ -28,19 +29,26 @@ namespace DiscordChatExporter.Core.Models
public partial class Emoji public partial class Emoji
{ {
private static IEnumerable<int> GetCodePoints(string emoji) private static IEnumerable<Rune> GetRunes(string emoji)
{ {
for (var i = 0; i < emoji.Length; i += char.IsHighSurrogate(emoji[i]) ? 2 : 1) var lastIndex = 0;
yield return char.ConvertToUtf32(emoji, i); while (lastIndex < emoji.Length && Rune.TryGetRuneAt(emoji, lastIndex, out var rune))
{
// Skip variant selector rune
if (rune.Value != 0xfe0f)
yield return rune;
lastIndex += rune.Utf16SequenceLength;
}
} }
private static string GetTwemojiName(string emoji) => private static string GetTwemojiName(IEnumerable<Rune> runes) =>
GetCodePoints(emoji).Select(i => i.ToString("x")).JoinToString("-"); runes.Select(r => r.Value.ToString("x")).JoinToString("-");
public static string GetImageUrl(string id, string name, bool isAnimated) public static string GetImageUrl(string? id, string name, bool isAnimated)
{ {
// Custom emoji // Custom emoji
if (!id.IsNullOrWhiteSpace()) if (!string.IsNullOrWhiteSpace(id))
{ {
// Animated // Animated
if (isAnimated) if (isAnimated)
@@ -50,9 +58,17 @@ namespace DiscordChatExporter.Core.Models
return $"https://cdn.discordapp.com/emojis/{id}.png"; return $"https://cdn.discordapp.com/emojis/{id}.png";
} }
// Standard unicode emoji (via twemoji) // Standard unicode emoji
var twemojiName = GetTwemojiName(name); var emojiRunes = GetRunes(name).ToArray();
return $"https://twemoji.maxcdn.com/2/72x72/{twemojiName}.png"; if (emojiRunes.Any())
{
// Get corresponding Twemoji image
var twemojiName = GetTwemojiName(emojiRunes);
return $"https://twemoji.maxcdn.com/2/72x72/{twemojiName}.png";
}
// Fallback in case of failure
return name;
} }
} }
} }
@@ -0,0 +1,12 @@
using System;
namespace DiscordChatExporter.Core.Models.Exceptions
{
public class DomainException : Exception
{
public DomainException(string message)
: base(message)
{
}
}
}
+24 -25
View File
@@ -4,32 +4,31 @@ namespace DiscordChatExporter.Core.Models
{ {
public static class Extensions public static class Extensions
{ {
public static string GetFileExtension(this ExportFormat format) public static bool IsExportable(this ChannelType channelType) =>
{ channelType == ChannelType.GuildTextChat ||
if (format == ExportFormat.PlainText) channelType == ChannelType.DirectTextChat ||
return "txt"; channelType == ChannelType.DirectGroupTextChat ||
if (format == ExportFormat.HtmlDark) channelType == ChannelType.GuildNews ||
return "html"; channelType == ChannelType.GuildStore;
if (format == ExportFormat.HtmlLight)
return "html";
if (format == ExportFormat.Csv)
return "csv";
throw new ArgumentOutOfRangeException(nameof(format)); public static string GetFileExtension(this ExportFormat format) =>
} format switch
{
ExportFormat.PlainText => "txt",
ExportFormat.HtmlDark => "html",
ExportFormat.HtmlLight => "html",
ExportFormat.Csv => "csv",
_ => throw new ArgumentOutOfRangeException(nameof(format))
};
public static string GetDisplayName(this ExportFormat format) public static string GetDisplayName(this ExportFormat format) =>
{ format switch
if (format == ExportFormat.PlainText) {
return "Plain Text"; ExportFormat.PlainText => "Plain Text",
if (format == ExportFormat.HtmlDark) ExportFormat.HtmlDark => "HTML (Dark)",
return "HTML (Dark)"; ExportFormat.HtmlLight => "HTML (Light)",
if (format == ExportFormat.HtmlLight) ExportFormat.Csv => "Comma Separated Values (CSV)",
return "HTML (Light)"; _ => throw new ArgumentOutOfRangeException(nameof(format))
if (format == ExportFormat.Csv) };
return "Comma Seperated Values (CSV)";
throw new ArgumentOutOfRangeException(nameof(format));
}
} }
} }
@@ -30,7 +30,6 @@ namespace DiscordChatExporter.Core.Models
{ {
get get
{ {
// Absolute value is used to deal with negative values
if (Math.Abs(PetaBytes) >= 1) if (Math.Abs(PetaBytes) >= 1)
return PetaByteSymbol; return PetaByteSymbol;
@@ -54,7 +53,6 @@ namespace DiscordChatExporter.Core.Models
{ {
get get
{ {
// Absolute value is used to deal with negative values
if (Math.Abs(PetaBytes) >= 1) if (Math.Abs(PetaBytes) >= 1)
return PetaBytes; return PetaBytes;
+7 -9
View File
@@ -1,20 +1,18 @@
using Tyrrrz.Extensions; namespace DiscordChatExporter.Core.Models
namespace DiscordChatExporter.Core.Models
{ {
// https://discordapp.com/developers/docs/resources/guild#guild-object // https://discordapp.string.IsNullOrWhiteSpace(com/developers/docs/resources/guild#guild-object
public partial class Guild public partial class Guild : IHasId
{ {
public string Id { get; } public string Id { get; }
public string Name { get; } public string Name { get; }
public string IconHash { get; } public string? IconHash { get; }
public string IconUrl { get; } public string IconUrl { get; }
public Guild(string id, string name, string iconHash) public Guild(string id, string name, string? iconHash)
{ {
Id = id; Id = id;
Name = name; Name = name;
@@ -28,9 +26,9 @@ namespace DiscordChatExporter.Core.Models
public partial class Guild public partial class Guild
{ {
public static string GetIconUrl(string id, string iconHash) public static string GetIconUrl(string id, string? iconHash)
{ {
return !iconHash.IsNullOrWhiteSpace() return !string.IsNullOrWhiteSpace(iconHash)
? $"https://cdn.discordapp.com/icons/{id}/{iconHash}.png" ? $"https://cdn.discordapp.com/icons/{id}/{iconHash}.png"
: "https://cdn.discordapp.com/embed/avatars/0.png"; : "https://cdn.discordapp.com/embed/avatars/0.png";
} }
@@ -0,0 +1,7 @@
namespace DiscordChatExporter.Core.Models
{
public interface IHasId
{
string Id { get; }
}
}
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
namespace DiscordChatExporter.Core.Models
{
public partial class IdBasedEqualityComparer : IEqualityComparer<IHasId>
{
public bool Equals(IHasId? x, IHasId? y) => StringComparer.Ordinal.Equals(x?.Id, y?.Id);
public int GetHashCode(IHasId obj) => StringComparer.Ordinal.GetHashCode(obj.Id);
}
public partial class IdBasedEqualityComparer
{
public static IdBasedEqualityComparer Instance { get; } = new IdBasedEqualityComparer();
}
}
@@ -1,30 +0,0 @@
using System.Collections.Generic;
using System.Linq;
namespace DiscordChatExporter.Core.Models
{
public class Mentionables
{
public IReadOnlyList<User> Users { get; }
public IReadOnlyList<Channel> Channels { get; }
public IReadOnlyList<Role> Roles { get; }
public Mentionables(IReadOnlyList<User> users, IReadOnlyList<Channel> channels, IReadOnlyList<Role> roles)
{
Users = users;
Channels = channels;
Roles = roles;
}
public User GetUser(string id) =>
Users.FirstOrDefault(u => u.Id == id) ?? User.CreateUnknownUser(id);
public Channel GetChannel(string id) =>
Channels.FirstOrDefault(c => c.Id == id) ?? Channel.CreateDeletedChannel(id);
public Role GetRole(string id) =>
Roles.FirstOrDefault(r => r.Id == id) ?? Role.CreateDeletedRole(id);
}
}
+11 -10
View File
@@ -5,7 +5,7 @@ namespace DiscordChatExporter.Core.Models
{ {
// https://discordapp.com/developers/docs/resources/channel#message-object // https://discordapp.com/developers/docs/resources/channel#message-object
public class Message public class Message : IHasId
{ {
public string Id { get; } public string Id { get; }
@@ -19,7 +19,9 @@ namespace DiscordChatExporter.Core.Models
public DateTimeOffset? EditedTimestamp { get; } public DateTimeOffset? EditedTimestamp { get; }
public string Content { get; } public bool IsPinned { get; }
public string? Content { get; }
public IReadOnlyList<Attachment> Attachments { get; } public IReadOnlyList<Attachment> Attachments { get; }
@@ -29,12 +31,11 @@ namespace DiscordChatExporter.Core.Models
public IReadOnlyList<User> MentionedUsers { get; } public IReadOnlyList<User> MentionedUsers { get; }
public bool IsPinned { get; } public Message(string id, string channelId, MessageType type, User author,
DateTimeOffset timestamp, DateTimeOffset? editedTimestamp, bool isPinned,
public Message(string id, string channelId, MessageType type, User author, DateTimeOffset timestamp, string content,
DateTimeOffset? editedTimestamp, string content, IReadOnlyList<Attachment> attachments, IReadOnlyList<Attachment> attachments,IReadOnlyList<Embed> embeds, IReadOnlyList<Reaction> reactions,
IReadOnlyList<Embed> embeds, IReadOnlyList<Reaction> reactions, IReadOnlyList<User> mentionedUsers, IReadOnlyList<User> mentionedUsers)
bool isPinned)
{ {
Id = id; Id = id;
ChannelId = channelId; ChannelId = channelId;
@@ -42,14 +43,14 @@ namespace DiscordChatExporter.Core.Models
Author = author; Author = author;
Timestamp = timestamp; Timestamp = timestamp;
EditedTimestamp = editedTimestamp; EditedTimestamp = editedTimestamp;
IsPinned = isPinned;
Content = content; Content = content;
Attachments = attachments; Attachments = attachments;
Embeds = embeds; Embeds = embeds;
Reactions = reactions; Reactions = reactions;
MentionedUsers = mentionedUsers; MentionedUsers = mentionedUsers;
IsPinned = isPinned;
} }
public override string ToString() => Content; public override string ToString() => Content ?? "<message without content>";
} }
} }
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
namespace DiscordChatExporter.Core.Models
{
// Used for grouping contiguous messages in HTML export
public class MessageGroup
{
public User Author { get; }
public DateTimeOffset Timestamp { get; }
public IReadOnlyList<Message> Messages { get; }
public MessageGroup(User author, DateTimeOffset timestamp, IReadOnlyList<Message> messages)
{
Author = author;
Timestamp = timestamp;
Messages = messages;
}
}
}
+1 -1
View File
@@ -2,7 +2,7 @@
{ {
// https://discordapp.com/developers/docs/topics/permissions#role-object // https://discordapp.com/developers/docs/topics/permissions#role-object
public partial class Role public partial class Role : IHasId
{ {
public string Id { get; } public string Id { get; }
+5 -6
View File
@@ -1,11 +1,10 @@
using System; using System;
using Tyrrrz.Extensions;
namespace DiscordChatExporter.Core.Models namespace DiscordChatExporter.Core.Models
{ {
// https://discordapp.com/developers/docs/topics/permissions#role-object // https://discordapp.com/developers/docs/topics/permissions#role-object
public partial class User public partial class User : IHasId
{ {
public string Id { get; } public string Id { get; }
@@ -15,13 +14,13 @@ namespace DiscordChatExporter.Core.Models
public string FullName { get; } public string FullName { get; }
public string AvatarHash { get; } public string? AvatarHash { get; }
public string AvatarUrl { get; } public string AvatarUrl { get; }
public bool IsBot { get; } public bool IsBot { get; }
public User(string id, int discriminator, string name, string avatarHash, bool isBot) public User(string id, int discriminator, string name, string? avatarHash, bool isBot)
{ {
Id = id; Id = id;
Discriminator = discriminator; Discriminator = discriminator;
@@ -40,10 +39,10 @@ namespace DiscordChatExporter.Core.Models
{ {
public static string GetFullName(string name, int discriminator) => $"{name}#{discriminator:0000}"; public static string GetFullName(string name, int discriminator) => $"{name}#{discriminator:0000}";
public static string GetAvatarUrl(string id, int discriminator, string avatarHash) public static string GetAvatarUrl(string id, int discriminator, string? avatarHash)
{ {
// Custom avatar // Custom avatar
if (!avatarHash.IsNullOrWhiteSpace()) if (!string.IsNullOrWhiteSpace(avatarHash))
{ {
// Animated // Animated
if (avatarHash.StartsWith("a_", StringComparison.Ordinal)) if (avatarHash.StartsWith("a_", StringComparison.Ordinal))
@@ -1,123 +0,0 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using DiscordChatExporter.Core.Markdown;
using DiscordChatExporter.Core.Markdown.Nodes;
using DiscordChatExporter.Core.Models;
using Tyrrrz.Extensions;
namespace DiscordChatExporter.Core.Rendering
{
public class CsvChatLogRenderer : IChatLogRenderer
{
private readonly ChatLog _chatLog;
private readonly string _dateFormat;
public CsvChatLogRenderer(ChatLog chatLog, string dateFormat)
{
_chatLog = chatLog;
_dateFormat = dateFormat;
}
private string FormatDate(DateTimeOffset date) =>
date.ToLocalTime().ToString(_dateFormat, CultureInfo.InvariantCulture);
private string FormatMarkdown(Node node)
{
// Text node
if (node is TextNode textNode)
{
return textNode.Text;
}
// Mention node
if (node is MentionNode mentionNode)
{
// Meta mention node
if (mentionNode.Type == MentionType.Meta)
{
return mentionNode.Id;
}
// User mention node
if (mentionNode.Type == MentionType.User)
{
var user = _chatLog.Mentionables.GetUser(mentionNode.Id);
return $"@{user.Name}";
}
// Channel mention node
if (mentionNode.Type == MentionType.Channel)
{
var channel = _chatLog.Mentionables.GetChannel(mentionNode.Id);
return $"#{channel.Name}";
}
// Role mention node
if (mentionNode.Type == MentionType.Role)
{
var role = _chatLog.Mentionables.GetRole(mentionNode.Id);
return $"@{role.Name}";
}
}
// Emoji node
if (node is EmojiNode emojiNode)
{
return emojiNode.IsCustomEmoji ? $":{emojiNode.Name}:" : emojiNode.Name;
}
// Throw on unexpected nodes
throw new InvalidOperationException($"Unexpected node: [{node.GetType()}].");
}
private string FormatMarkdown(IEnumerable<Node> nodes) => nodes.Select(FormatMarkdown).JoinToString("");
private string FormatMarkdown(string markdown) => FormatMarkdown(MarkdownParser.ParseMinimal(markdown));
private async Task RenderFieldAsync(TextWriter writer, string value)
{
var encodedValue = value.Replace("\"", "\"\"");
await writer.WriteAsync($"\"{encodedValue}\";");
}
private async Task RenderMessageAsync(TextWriter writer, Message message)
{
// Author ID
await RenderFieldAsync(writer, message.Author.Id);
// Author
await RenderFieldAsync(writer, message.Author.FullName);
// Timestamp
await RenderFieldAsync(writer, FormatDate(message.Timestamp));
// Content
await RenderFieldAsync(writer, FormatMarkdown(message.Content));
// Attachments
var formattedAttachments = message.Attachments.Select(a => a.Url).JoinToString(",");
await RenderFieldAsync(writer, formattedAttachments);
// Reactions
var formattedReactions = message.Reactions.Select(r => $"{r.Emoji.Name} ({r.Count})").JoinToString(",");
await RenderFieldAsync(writer, formattedReactions);
// Line break
await writer.WriteLineAsync();
}
public async Task RenderAsync(TextWriter writer)
{
// Headers
await writer.WriteLineAsync("AuthorID;Author;Date;Content;Attachments;Reactions;");
// Log
foreach (var message in _chatLog.Messages)
await RenderMessageAsync(writer, message);
}
}
}
@@ -1,21 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<Import Project="../DiscordChatExporter.props" />
<PropertyGroup>
<TargetFrameworks>net46;netstandard2.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="Resources\HtmlCore.css" />
<EmbeddedResource Include="Resources\HtmlDark.css" /> <EmbeddedResource Include="Resources\HtmlDark.css" />
<EmbeddedResource Include="Resources\HtmlDark.html" />
<EmbeddedResource Include="Resources\HtmlLight.css" /> <EmbeddedResource Include="Resources\HtmlLight.css" />
<EmbeddedResource Include="Resources\HtmlLight.html" /> <EmbeddedResource Include="Resources\HtmlLayoutTemplate.html" />
<EmbeddedResource Include="Resources\HtmlShared.css" /> <EmbeddedResource Include="Resources\HtmlMessageGroupTemplate.html" />
<EmbeddedResource Include="Resources\HtmlShared.html" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Scriban" Version="2.1.0" /> <PackageReference Include="Scriban" Version="2.1.1" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -0,0 +1,25 @@
using System.IO;
using System.Threading.Tasks;
using DiscordChatExporter.Core.Models;
using DiscordChatExporter.Core.Rendering.Logic;
namespace DiscordChatExporter.Core.Rendering.Formatters
{
public class CsvMessageWriter : MessageWriterBase
{
public CsvMessageWriter(TextWriter writer, RenderContext context)
: base(writer, context)
{
}
public override async Task WritePreambleAsync()
{
await Writer.WriteLineAsync(CsvRenderingLogic.FormatHeader(Context));
}
public override async Task WriteMessageAsync(Message message)
{
await Writer.WriteLineAsync(CsvRenderingLogic.FormatMessage(Context, message));
}
}
}
@@ -0,0 +1,163 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using DiscordChatExporter.Core.Models;
using DiscordChatExporter.Core.Rendering.Logic;
using Scriban;
using Scriban.Runtime;
using Tyrrrz.Extensions;
namespace DiscordChatExporter.Core.Rendering.Formatters
{
public partial class HtmlMessageWriter : MessageWriterBase
{
private readonly string _themeName;
private readonly List<Message> _messageGroupBuffer = new List<Message>();
private readonly Template _preambleTemplate;
private readonly Template _messageGroupTemplate;
private readonly Template _postambleTemplate;
private long _messageCount;
public HtmlMessageWriter(TextWriter writer, RenderContext context, string themeName)
: base(writer, context)
{
_themeName = themeName;
_preambleTemplate = Template.Parse(GetPreambleTemplateCode());
_messageGroupTemplate = Template.Parse(GetMessageGroupTemplateCode());
_postambleTemplate = Template.Parse(GetPostambleTemplateCode());
}
private MessageGroup GetCurrentMessageGroup()
{
var firstMessage = _messageGroupBuffer.First();
return new MessageGroup(firstMessage.Author, firstMessage.Timestamp, _messageGroupBuffer);
}
private TemplateContext CreateTemplateContext(IReadOnlyDictionary<string, object>? constants = null)
{
// Template context
var templateContext = new TemplateContext
{
MemberRenamer = m => m.Name,
MemberFilter = m => true,
LoopLimit = int.MaxValue,
StrictVariables = true
};
// Model
var scriptObject = new ScriptObject();
// Constants
scriptObject.SetValue("Context", Context, true);
scriptObject.SetValue("CoreStyleSheet", GetCoreStyleSheetCode(), true);
scriptObject.SetValue("ThemeStyleSheet", GetThemeStyleSheetCode(_themeName), true);
scriptObject.SetValue("HighlightJsStyleName", $"solarized-{_themeName.ToLowerInvariant()}", true);
// Additional constants
if (constants != null)
{
foreach (var (member, value) in constants)
scriptObject.SetValue(member, value, true);
}
// Functions
scriptObject.Import("FormatDate",
new Func<DateTimeOffset, string>(d => SharedRenderingLogic.FormatDate(d, Context.DateFormat)));
scriptObject.Import("FormatMarkdown",
new Func<string, string>(m => HtmlRenderingLogic.FormatMarkdown(Context, m)));
// Push model
templateContext.PushGlobal(scriptObject);
// Push output
templateContext.PushOutput(new TextWriterOutput(Writer));
return templateContext;
}
private async Task RenderCurrentMessageGroupAsync()
{
var templateContext = CreateTemplateContext(new Dictionary<string, object>
{
["MessageGroup"] = GetCurrentMessageGroup()
});
await templateContext.EvaluateAsync(_messageGroupTemplate.Page);
}
public override async Task WritePreambleAsync()
{
var templateContext = CreateTemplateContext();
await templateContext.EvaluateAsync(_preambleTemplate.Page);
}
public override async Task WriteMessageAsync(Message message)
{
// If message group is empty or the given message can be grouped, buffer the given message
if (!_messageGroupBuffer.Any() || HtmlRenderingLogic.CanBeGrouped(_messageGroupBuffer.Last(), message))
{
_messageGroupBuffer.Add(message);
}
// Otherwise, flush the group and render messages
else
{
await RenderCurrentMessageGroupAsync();
_messageGroupBuffer.Clear();
_messageGroupBuffer.Add(message);
}
// Increment message count
_messageCount++;
}
public override async Task WritePostambleAsync()
{
// Flush current message group
if (_messageGroupBuffer.Any())
await RenderCurrentMessageGroupAsync();
var templateContext = CreateTemplateContext(new Dictionary<string, object>
{
["MessageCount"] = _messageCount
});
await templateContext.EvaluateAsync(_postambleTemplate.Page);
}
}
public partial class HtmlMessageWriter
{
private static readonly Assembly ResourcesAssembly = typeof(HtmlRenderingLogic).Assembly;
private static readonly string ResourcesNamespace = $"{ResourcesAssembly.GetName().Name}.Resources";
private static string GetCoreStyleSheetCode() =>
ResourcesAssembly
.GetManifestResourceString($"{ResourcesNamespace}.HtmlCore.css");
private static string GetThemeStyleSheetCode(string themeName) =>
ResourcesAssembly
.GetManifestResourceString($"{ResourcesNamespace}.Html{themeName}.css");
private static string GetPreambleTemplateCode() =>
ResourcesAssembly
.GetManifestResourceString($"{ResourcesNamespace}.HtmlLayoutTemplate.html")
.SubstringUntil("{{~ %SPLIT% ~}}");
private static string GetMessageGroupTemplateCode() =>
ResourcesAssembly
.GetManifestResourceString($"{ResourcesNamespace}.HtmlMessageGroupTemplate.html");
private static string GetPostambleTemplateCode() =>
ResourcesAssembly
.GetManifestResourceString($"{ResourcesNamespace}.HtmlLayoutTemplate.html")
.SubstringAfter("{{~ %SPLIT% ~}}");
}
}
@@ -0,0 +1,28 @@
using System;
using System.IO;
using System.Threading.Tasks;
using DiscordChatExporter.Core.Models;
namespace DiscordChatExporter.Core.Rendering.Formatters
{
public abstract class MessageWriterBase : IAsyncDisposable
{
protected TextWriter Writer { get; }
protected RenderContext Context { get; }
protected MessageWriterBase(TextWriter writer, RenderContext context)
{
Writer = writer;
Context = context;
}
public virtual Task WritePreambleAsync() => Task.CompletedTask;
public abstract Task WriteMessageAsync(Message message);
public virtual Task WritePostambleAsync() => Task.CompletedTask;
public async ValueTask DisposeAsync() => await Writer.DisposeAsync();
}
}
@@ -0,0 +1,36 @@
using System.IO;
using System.Threading.Tasks;
using DiscordChatExporter.Core.Models;
using DiscordChatExporter.Core.Rendering.Logic;
namespace DiscordChatExporter.Core.Rendering.Formatters
{
public class PlainTextMessageWriter : MessageWriterBase
{
private long _messageCount;
public PlainTextMessageWriter(TextWriter writer, RenderContext context)
: base(writer, context)
{
}
public override async Task WritePreambleAsync()
{
await Writer.WriteLineAsync(PlainTextRenderingLogic.FormatPreamble(Context));
}
public override async Task WriteMessageAsync(Message message)
{
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));
}
}
}
@@ -1,25 +0,0 @@
using System;
using System.Collections.Generic;
using DiscordChatExporter.Core.Models;
namespace DiscordChatExporter.Core.Rendering
{
public partial class HtmlChatLogRenderer
{
private class MessageGroup
{
public User Author { get; }
public DateTimeOffset Timestamp { get; }
public IReadOnlyList<Message> Messages { get; }
public MessageGroup(User author, DateTimeOffset timestamp, IReadOnlyList<Message> messages)
{
Author = author;
Timestamp = timestamp;
Messages = messages;
}
}
}
}
@@ -1,27 +0,0 @@
using System.Reflection;
using System.Threading.Tasks;
using Scriban;
using Scriban.Parsing;
using Scriban.Runtime;
using Tyrrrz.Extensions;
namespace DiscordChatExporter.Core.Rendering
{
public partial class HtmlChatLogRenderer
{
private class TemplateLoader : ITemplateLoader
{
private const string ResourceRootNamespace = "DiscordChatExporter.Core.Rendering.Resources";
public string Load(string templatePath) =>
Assembly.GetExecutingAssembly().GetManifestResourceString($"{ResourceRootNamespace}.{templatePath}");
public string GetPath(TemplateContext context, SourceSpan callerSpan, string templateName) => templateName;
public string Load(TemplateContext context, SourceSpan callerSpan, string templatePath) => Load(templatePath);
public ValueTask<string> LoadAsync(TemplateContext context, SourceSpan callerSpan, string templatePath) =>
new ValueTask<string>(Load(templatePath));
}
}
}
@@ -1,10 +0,0 @@
using System.IO;
using System.Threading.Tasks;
namespace DiscordChatExporter.Core.Rendering
{
public interface IChatLogRenderer
{
Task RenderAsync(TextWriter writer);
}
}
@@ -0,0 +1,21 @@
using System.Text;
namespace DiscordChatExporter.Core.Rendering.Internal
{
internal static class Extensions
{
public static StringBuilder AppendLineIfNotEmpty(this StringBuilder builder, string value) =>
!string.IsNullOrWhiteSpace(value) ? builder.AppendLine(value) : builder;
public static StringBuilder Trim(this StringBuilder builder)
{
while (builder.Length > 0 && char.IsWhiteSpace(builder[0]))
builder.Remove(0, 1);
while (builder.Length > 0 && char.IsWhiteSpace(builder[^1]))
builder.Remove(builder.Length - 1, 1);
return builder;
}
}
}
@@ -0,0 +1,39 @@
using System.Linq;
using System.Text;
using DiscordChatExporter.Core.Models;
using Tyrrrz.Extensions;
using static DiscordChatExporter.Core.Rendering.Logic.SharedRenderingLogic;
namespace DiscordChatExporter.Core.Rendering.Logic
{
public static class CsvRenderingLogic
{
// Header is always the same
public static string FormatHeader(RenderContext context) => "AuthorID,Author,Date,Content,Attachments,Reactions";
private static string EncodeValue(string value)
{
value = value.Replace("\"", "\"\"");
return $"\"{value}\"";
}
public static string FormatMarkdown(RenderContext context, string markdown) =>
PlainTextRenderingLogic.FormatMarkdown(context, markdown);
public static string FormatMessage(RenderContext context, Message message)
{
var buffer = new StringBuilder();
buffer
.Append(EncodeValue(message.Author.Id)).Append(',')
.Append(EncodeValue(message.Author.FullName)).Append(',')
.Append(EncodeValue(FormatDate(message.Timestamp, context.DateFormat))).Append(',')
.Append(EncodeValue(FormatMarkdown(context, message.Content ?? ""))).Append(',')
.Append(EncodeValue(message.Attachments.Select(a => a.Url).JoinToString(","))).Append(',')
.Append(EncodeValue(message.Reactions.Select(r => $"{r.Emoji.Name} ({r.Count})").JoinToString(",")));
return buffer.ToString();
}
}
}
@@ -1,53 +1,31 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks;
using DiscordChatExporter.Core.Markdown; using DiscordChatExporter.Core.Markdown;
using DiscordChatExporter.Core.Markdown.Nodes; using DiscordChatExporter.Core.Markdown.Ast;
using DiscordChatExporter.Core.Models; using DiscordChatExporter.Core.Models;
using Scriban;
using Scriban.Runtime;
using Tyrrrz.Extensions; using Tyrrrz.Extensions;
namespace DiscordChatExporter.Core.Rendering namespace DiscordChatExporter.Core.Rendering.Logic
{ {
public partial class HtmlChatLogRenderer : IChatLogRenderer internal static class HtmlRenderingLogic
{ {
private readonly ChatLog _chatLog; public static bool CanBeGrouped(Message message1, Message message2)
private readonly string _themeName;
private readonly string _dateFormat;
public HtmlChatLogRenderer(ChatLog chatLog, string themeName, string dateFormat)
{ {
_chatLog = chatLog; if (message1.Author.Id != message2.Author.Id)
_themeName = themeName; return false;
_dateFormat = dateFormat;
if ((message2.Timestamp - message1.Timestamp).Duration().TotalMinutes > 7)
return false;
return true;
} }
private string HtmlEncode(string s) => WebUtility.HtmlEncode(s); private static string HtmlEncode(string s) => WebUtility.HtmlEncode(s);
private string FormatDate(DateTimeOffset date) => private static string FormatMarkdownNode(RenderContext context, Node node, bool isJumbo)
date.ToLocalTime().ToString(_dateFormat, CultureInfo.InvariantCulture);
private IEnumerable<MessageGroup> GroupMessages(IEnumerable<Message> messages) =>
messages.GroupContiguous((buffer, message) =>
{
// Break group if the author changed
if (buffer.Last().Author.Id != message.Author.Id)
return false;
// Break group if last message was more than 7 minutes ago
if ((message.Timestamp - buffer.Last().Timestamp).TotalMinutes > 7)
return false;
return true;
}).Select(g => new MessageGroup(g.First().Author, g.First().Timestamp, g));
private string FormatMarkdown(Node node, bool isJumbo)
{ {
// Text node // Text node
if (node is TextNode textNode) if (node is TextNode textNode)
@@ -60,7 +38,7 @@ namespace DiscordChatExporter.Core.Rendering
if (node is FormattedNode formattedNode) if (node is FormattedNode formattedNode)
{ {
// Recursively get inner html // Recursively get inner html
var innerHtml = FormatMarkdown(formattedNode.Children, false); var innerHtml = FormatMarkdownNodes(context, formattedNode.Children, false);
// Bold // Bold
if (formattedNode.Formatting == TextFormatting.Bold) if (formattedNode.Formatting == TextFormatting.Bold)
@@ -97,7 +75,7 @@ namespace DiscordChatExporter.Core.Rendering
if (node is MultiLineCodeBlockNode multilineCodeBlockNode) if (node is MultiLineCodeBlockNode multilineCodeBlockNode)
{ {
// Set CSS class for syntax highlighting // Set CSS class for syntax highlighting
var highlightCssClass = !multilineCodeBlockNode.Language.IsNullOrWhiteSpace() var highlightCssClass = !string.IsNullOrWhiteSpace(multilineCodeBlockNode.Language)
? $"language-{multilineCodeBlockNode.Language}" ? $"language-{multilineCodeBlockNode.Language}"
: "nohighlight"; : "nohighlight";
@@ -116,21 +94,27 @@ namespace DiscordChatExporter.Core.Rendering
// User mention node // User mention node
if (mentionNode.Type == MentionType.User) if (mentionNode.Type == MentionType.User)
{ {
var user = _chatLog.Mentionables.GetUser(mentionNode.Id); var user = context.MentionableUsers.FirstOrDefault(u => u.Id == mentionNode.Id) ??
User.CreateUnknownUser(mentionNode.Id);
return $"<span class=\"mention\" title=\"{HtmlEncode(user.FullName)}\">@{HtmlEncode(user.Name)}</span>"; return $"<span class=\"mention\" title=\"{HtmlEncode(user.FullName)}\">@{HtmlEncode(user.Name)}</span>";
} }
// Channel mention node // Channel mention node
if (mentionNode.Type == MentionType.Channel) if (mentionNode.Type == MentionType.Channel)
{ {
var channel = _chatLog.Mentionables.GetChannel(mentionNode.Id); var channel = context.MentionableChannels.FirstOrDefault(c => c.Id == mentionNode.Id) ??
Channel.CreateDeletedChannel(mentionNode.Id);
return $"<span class=\"mention\">#{HtmlEncode(channel.Name)}</span>"; return $"<span class=\"mention\">#{HtmlEncode(channel.Name)}</span>";
} }
// Role mention node // Role mention node
if (mentionNode.Type == MentionType.Role) if (mentionNode.Type == MentionType.Role)
{ {
var role = _chatLog.Mentionables.GetRole(mentionNode.Id); var role = context.MentionableRoles.FirstOrDefault(r => r.Id == mentionNode.Id) ??
Role.CreateDeletedRole(mentionNode.Id);
return $"<span class=\"mention\">@{HtmlEncode(role.Name)}</span>"; return $"<span class=\"mention\">@{HtmlEncode(role.Name)}</span>";
} }
} }
@@ -153,58 +137,24 @@ namespace DiscordChatExporter.Core.Rendering
// Extract message ID if the link points to a Discord message // Extract message ID if the link points to a Discord message
var linkedMessageId = Regex.Match(linkNode.Url, "^https?://discordapp.com/channels/.*?/(\\d+)/?$").Groups[1].Value; var linkedMessageId = Regex.Match(linkNode.Url, "^https?://discordapp.com/channels/.*?/(\\d+)/?$").Groups[1].Value;
return linkedMessageId.IsNullOrWhiteSpace() return string.IsNullOrWhiteSpace(linkedMessageId)
? $"<a href=\"{Uri.EscapeUriString(linkNode.Url)}\">{HtmlEncode(linkNode.Title)}</a>" ? $"<a href=\"{Uri.EscapeUriString(linkNode.Url)}\">{HtmlEncode(linkNode.Title)}</a>"
: $"<a href=\"{Uri.EscapeUriString(linkNode.Url)}\" onclick=\"scrollToMessage(event, '{linkedMessageId}')\">{HtmlEncode(linkNode.Title)}</a>"; : $"<a href=\"{Uri.EscapeUriString(linkNode.Url)}\" onclick=\"scrollToMessage(event, '{linkedMessageId}')\">{HtmlEncode(linkNode.Title)}</a>";
} }
// Throw on unexpected nodes // Throw on unexpected nodes
throw new InvalidOperationException($"Unexpected node: [{node.GetType()}]."); throw new InvalidOperationException($"Unexpected node [{node.GetType()}].");
} }
private string FormatMarkdown(IReadOnlyList<Node> nodes, bool isTopLevel) private static string FormatMarkdownNodes(RenderContext context, IReadOnlyList<Node> nodes, bool isTopLevel)
{ {
// Emojis are jumbo if all top-level nodes are emoji nodes or whitespace text nodes // Emojis are jumbo if all top-level nodes are emoji nodes or whitespace text nodes
var isJumbo = isTopLevel && nodes.All(n => n is EmojiNode || n is TextNode textNode && textNode.Text.IsNullOrWhiteSpace()); var isJumbo = isTopLevel && nodes.All(n => n is EmojiNode || n is TextNode textNode && string.IsNullOrWhiteSpace(textNode.Text));
return nodes.Select(n => FormatMarkdown(n, isJumbo)).JoinToString(""); return nodes.Select(n => FormatMarkdownNode(context, n, isJumbo)).JoinToString("");
} }
private string FormatMarkdown(string markdown) => FormatMarkdown(MarkdownParser.Parse(markdown), true); public static string FormatMarkdown(RenderContext context, string markdown) =>
FormatMarkdownNodes(context, MarkdownParser.Parse(markdown), true);
public async Task RenderAsync(TextWriter writer)
{
// Create template loader
var loader = new TemplateLoader();
// Get template
var templateCode = loader.Load($"Html{_themeName}.html");
var template = Template.Parse(templateCode);
// Create template context
var context = new TemplateContext
{
TemplateLoader = loader,
MemberRenamer = m => m.Name,
MemberFilter = m => true,
LoopLimit = int.MaxValue,
StrictVariables = true
};
// Create template model
var model = new ScriptObject();
model.SetValue("Model", _chatLog, true);
model.Import(nameof(GroupMessages), new Func<IEnumerable<Message>, IEnumerable<MessageGroup>>(GroupMessages));
model.Import(nameof(FormatDate), new Func<DateTimeOffset, string>(FormatDate));
model.Import(nameof(FormatMarkdown), new Func<string, string>(FormatMarkdown));
context.PushGlobal(model);
// Configure output
context.PushOutput(new TextWriterOutput(writer));
// HACK: Render output in a separate thread
// (even though Scriban has async API, it still makes a lot of blocking CPU-bound calls)
await Task.Run(async () => await context.EvaluateAsync(template.Page));
}
} }
} }
@@ -0,0 +1,246 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DiscordChatExporter.Core.Markdown;
using DiscordChatExporter.Core.Markdown.Ast;
using DiscordChatExporter.Core.Models;
using DiscordChatExporter.Core.Rendering.Internal;
using Tyrrrz.Extensions;
using static DiscordChatExporter.Core.Rendering.Logic.SharedRenderingLogic;
namespace DiscordChatExporter.Core.Rendering.Logic
{
public static class PlainTextRenderingLogic
{
public static string FormatPreamble(RenderContext context)
{
var buffer = new StringBuilder();
buffer.Append('=', 62).AppendLine();
buffer.AppendLine($"Guild: {context.Guild.Name}");
buffer.AppendLine($"Channel: {context.Channel.Name}");
if (!string.IsNullOrWhiteSpace(context.Channel.Topic))
buffer.AppendLine($"Topic: {context.Channel.Topic}");
if (context.After != null)
buffer.AppendLine($"After: {FormatDate(context.After.Value, context.DateFormat)}");
if (context.Before != null)
buffer.AppendLine($"Before: {FormatDate(context.Before.Value, context.DateFormat)}");
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();
}
private static string FormatMarkdownNode(RenderContext context, Node node)
{
// Text node
if (node is TextNode textNode)
{
return textNode.Text;
}
// Mention node
if (node is MentionNode mentionNode)
{
// Meta mention node
if (mentionNode.Type == MentionType.Meta)
{
return $"@{mentionNode.Id}";
}
// User mention node
if (mentionNode.Type == MentionType.User)
{
var user = context.MentionableUsers.FirstOrDefault(u => u.Id == mentionNode.Id) ??
User.CreateUnknownUser(mentionNode.Id);
return $"@{user.Name}";
}
// Channel mention node
if (mentionNode.Type == MentionType.Channel)
{
var channel = context.MentionableChannels.FirstOrDefault(c => c.Id == mentionNode.Id) ??
Channel.CreateDeletedChannel(mentionNode.Id);
return $"#{channel.Name}";
}
// Role mention node
if (mentionNode.Type == MentionType.Role)
{
var role = context.MentionableRoles.FirstOrDefault(r => r.Id == mentionNode.Id) ??
Role.CreateDeletedRole(mentionNode.Id);
return $"@{role.Name}";
}
}
// Emoji node
if (node is EmojiNode emojiNode)
{
return emojiNode.IsCustomEmoji ? $":{emojiNode.Name}:" : emojiNode.Name;
}
// Throw on unexpected nodes
throw new InvalidOperationException($"Unexpected node [{node.GetType()}].");
}
public static string FormatMarkdown(RenderContext context, string markdown) =>
MarkdownParser.ParseMinimal(markdown).Select(n => FormatMarkdownNode(context, n)).JoinToString("");
public static string FormatMessageHeader(RenderContext context, Message message)
{
var buffer = new StringBuilder();
// Timestamp & author
buffer
.Append($"[{FormatDate(message.Timestamp, context.DateFormat)}]")
.Append(' ')
.Append($"{message.Author.FullName}");
// Whether the message is pinned
if (message.IsPinned)
{
buffer.Append(' ').Append("(pinned)");
}
return buffer.ToString();
}
public static string FormatMessageContent(RenderContext context, Message message)
{
if (string.IsNullOrWhiteSpace(message.Content))
return "";
return FormatMarkdown(context, message.Content);
}
public static string FormatAttachments(IReadOnlyList<Attachment> attachments)
{
if (!attachments.Any())
return "";
var buffer = new StringBuilder();
buffer
.AppendLine("{Attachments}")
.AppendJoin(Environment.NewLine, attachments.Select(a => a.Url))
.AppendLine();
return buffer.ToString();
}
public static string FormatEmbeds(RenderContext context, IReadOnlyList<Embed> embeds)
{
if (!embeds.Any())
return "";
var buffer = new StringBuilder();
foreach (var embed in embeds)
{
buffer.AppendLine("{Embed}");
// Author name
if (!string.IsNullOrWhiteSpace(embed.Author?.Name))
buffer.AppendLine(embed.Author.Name);
// URL
if (!string.IsNullOrWhiteSpace(embed.Url))
buffer.AppendLine(embed.Url);
// Title
if (!string.IsNullOrWhiteSpace(embed.Title))
buffer.AppendLine(FormatMarkdown(context, embed.Title));
// Description
if (!string.IsNullOrWhiteSpace(embed.Description))
buffer.AppendLine(FormatMarkdown(context, embed.Description));
// Fields
foreach (var field in embed.Fields)
{
// Name
if (!string.IsNullOrWhiteSpace(field.Name))
buffer.AppendLine(field.Name);
// Value
if (!string.IsNullOrWhiteSpace(field.Value))
buffer.AppendLine(field.Value);
}
// Thumbnail URL
if (!string.IsNullOrWhiteSpace(embed.Thumbnail?.Url))
buffer.AppendLine(embed.Thumbnail?.Url);
// Image URL
if (!string.IsNullOrWhiteSpace(embed.Image?.Url))
buffer.AppendLine(embed.Image?.Url);
// Footer text
if (!string.IsNullOrWhiteSpace(embed.Footer?.Text))
buffer.AppendLine(embed.Footer?.Text);
buffer.AppendLine();
}
return buffer.ToString();
}
public static string FormatReactions(IReadOnlyList<Reaction> reactions)
{
if (!reactions.Any())
return "";
var buffer = new StringBuilder();
buffer.AppendLine("{Reactions}");
foreach (var reaction in reactions)
{
buffer.Append(reaction.Emoji.Name);
if (reaction.Count > 1)
buffer.Append($" ({reaction.Count})");
buffer.Append(" ");
}
buffer.AppendLine();
return buffer.ToString();
}
public static string FormatMessage(RenderContext context, Message message)
{
var buffer = new StringBuilder();
buffer
.AppendLine(FormatMessageHeader(context, message))
.AppendLineIfNotEmpty(FormatMessageContent(context, message))
.AppendLine()
.AppendLineIfNotEmpty(FormatAttachments(message.Attachments))
.AppendLineIfNotEmpty(FormatEmbeds(context, message.Embeds))
.AppendLineIfNotEmpty(FormatReactions(message.Reactions));
return buffer.Trim().ToString();
}
}
}
@@ -0,0 +1,11 @@
using System;
using System.Globalization;
namespace DiscordChatExporter.Core.Rendering.Logic
{
public static class SharedRenderingLogic
{
public static string FormatDate(DateTimeOffset date, string dateFormat) =>
date.ToLocalTime().ToString(dateFormat, CultureInfo.InvariantCulture);
}
}
@@ -0,0 +1,121 @@
using System;
using System.IO;
using System.Threading.Tasks;
using DiscordChatExporter.Core.Models;
using DiscordChatExporter.Core.Rendering.Formatters;
namespace DiscordChatExporter.Core.Rendering
{
public partial class MessageRenderer : IAsyncDisposable
{
private readonly RenderOptions _options;
private readonly RenderContext _context;
private long _renderedMessageCount;
private int _partitionIndex;
private MessageWriterBase? _writer;
public MessageRenderer(RenderOptions options, RenderContext context)
{
_options = options;
_context = context;
}
private async Task InitializeWriterAsync()
{
// Get partition file path
var filePath = GetPartitionFilePath(_options.BaseFilePath, _partitionIndex);
// Create output directory
var dirPath = Path.GetDirectoryName(_options.BaseFilePath);
if (!string.IsNullOrWhiteSpace(dirPath))
Directory.CreateDirectory(dirPath);
// Create writer
_writer = CreateMessageWriter(filePath, _options.Format, _context);
// Write preamble
await _writer.WritePreambleAsync();
}
private async Task ResetWriterAsync()
{
if (_writer != null)
{
// Write postamble
await _writer.WritePostambleAsync();
// Flush
await _writer.DisposeAsync();
_writer = null;
}
}
public async Task RenderMessageAsync(Message message)
{
// Ensure underlying writer is initialized
if (_writer == null)
await InitializeWriterAsync();
// Render the actual message
await _writer!.WriteMessageAsync(message);
// Increment count
_renderedMessageCount++;
// Shift partition if necessary
if (_options.PartitionLimit != null &&
_options.PartitionLimit != 0 &&
_renderedMessageCount % _options.PartitionLimit == 0)
{
await ResetWriterAsync();
_partitionIndex++;
}
}
public async ValueTask DisposeAsync() => await ResetWriterAsync();
}
public partial class MessageRenderer
{
private static string GetPartitionFilePath(string baseFilePath, int partitionIndex)
{
// First partition - no changes
if (partitionIndex <= 0)
return baseFilePath;
// Inject partition index into file name
var fileNameWithoutExt = Path.GetFileNameWithoutExtension(baseFilePath);
var fileExt = Path.GetExtension(baseFilePath);
var fileName = $"{fileNameWithoutExt} [part {partitionIndex + 1}]{fileExt}";
// Generate new path
var dirPath = Path.GetDirectoryName(baseFilePath);
if (!string.IsNullOrWhiteSpace(dirPath))
return Path.Combine(dirPath, fileName);
return fileName;
}
private static MessageWriterBase CreateMessageWriter(string filePath, ExportFormat format, RenderContext context)
{
// Create inner writer (it will get disposed by the wrapper)
var writer = File.CreateText(filePath);
// Create formatter
if (format == ExportFormat.PlainText)
return new PlainTextMessageWriter(writer, context);
if (format == ExportFormat.Csv)
return new CsvMessageWriter(writer, context);
if (format == ExportFormat.HtmlDark)
return new HtmlMessageWriter(writer, context, "Dark");
if (format == ExportFormat.HtmlLight)
return new HtmlMessageWriter(writer, context, "Light");
throw new InvalidOperationException($"Unknown export format [{format}].");
}
}
}
@@ -1,236 +0,0 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using DiscordChatExporter.Core.Markdown;
using DiscordChatExporter.Core.Markdown.Nodes;
using DiscordChatExporter.Core.Models;
using Tyrrrz.Extensions;
namespace DiscordChatExporter.Core.Rendering
{
public class PlainTextChatLogRenderer : IChatLogRenderer
{
private readonly ChatLog _chatLog;
private readonly string _dateFormat;
public PlainTextChatLogRenderer(ChatLog chatLog, string dateFormat)
{
_chatLog = chatLog;
_dateFormat = dateFormat;
}
private string FormatDate(DateTimeOffset date) =>
date.ToLocalTime().ToString(_dateFormat, CultureInfo.InvariantCulture);
private string FormatDateRange(DateTimeOffset? after, DateTimeOffset? before)
{
// Both 'after' and 'before'
if (after != null && before != null)
return $"{FormatDate(after.Value)} to {FormatDate(before.Value)}";
// Just 'after'
if (after != null)
return $"after {FormatDate(after.Value)}";
// Just 'before'
if (before != null)
return $"before {FormatDate(before.Value)}";
// Neither
return null;
}
private string FormatMarkdown(Node node)
{
// Text node
if (node is TextNode textNode)
{
return textNode.Text;
}
// Mention node
if (node is MentionNode mentionNode)
{
// Meta mention node
if (mentionNode.Type == MentionType.Meta)
{
return mentionNode.Id;
}
// User mention node
if (mentionNode.Type == MentionType.User)
{
var user = _chatLog.Mentionables.GetUser(mentionNode.Id);
return $"@{user.Name}";
}
// Channel mention node
if (mentionNode.Type == MentionType.Channel)
{
var channel = _chatLog.Mentionables.GetChannel(mentionNode.Id);
return $"#{channel.Name}";
}
// Role mention node
if (mentionNode.Type == MentionType.Role)
{
var role = _chatLog.Mentionables.GetRole(mentionNode.Id);
return $"@{role.Name}";
}
}
// Emoji node
if (node is EmojiNode emojiNode)
{
return emojiNode.IsCustomEmoji ? $":{emojiNode.Name}:" : emojiNode.Name;
}
// Throw on unexpected nodes
throw new InvalidOperationException($"Unexpected node: [{node.GetType()}].");
}
private string FormatMarkdown(IEnumerable<Node> nodes) => nodes.Select(FormatMarkdown).JoinToString("");
private string FormatMarkdown(string markdown) => FormatMarkdown(MarkdownParser.ParseMinimal(markdown));
private async Task RenderMessageHeaderAsync(TextWriter writer, Message message)
{
// Timestamp
await writer.WriteAsync($"[{FormatDate(message.Timestamp)}]");
// Author
await writer.WriteAsync($" {message.Author.FullName}");
// Whether the message is pinned
if (message.IsPinned)
await writer.WriteAsync(" (pinned)");
await writer.WriteLineAsync();
}
private async Task RenderAttachmentsAsync(TextWriter writer, IReadOnlyList<Attachment> attachments)
{
if (attachments.Any())
{
await writer.WriteLineAsync("{Attachments}");
foreach (var attachment in attachments)
await writer.WriteLineAsync(attachment.Url);
await writer.WriteLineAsync();
}
}
private async Task RenderEmbedsAsync(TextWriter writer, IReadOnlyList<Embed> embeds)
{
foreach (var embed in embeds)
{
await writer.WriteLineAsync("{Embed}");
// Author name
if (!(embed.Author?.Name).IsNullOrWhiteSpace())
await writer.WriteLineAsync(embed.Author?.Name);
// URL
if (!embed.Url.IsNullOrWhiteSpace())
await writer.WriteLineAsync(embed.Url);
// Title
if (!embed.Title.IsNullOrWhiteSpace())
await writer.WriteLineAsync(FormatMarkdown(embed.Title));
// Description
if (!embed.Description.IsNullOrWhiteSpace())
await writer.WriteLineAsync(FormatMarkdown(embed.Description));
// Fields
foreach (var field in embed.Fields)
{
// Name
if (!field.Name.IsNullOrWhiteSpace())
await writer.WriteLineAsync(field.Name);
// Value
if (!field.Value.IsNullOrWhiteSpace())
await writer.WriteLineAsync(field.Value);
}
// Thumbnail URL
if (!(embed.Thumbnail?.Url).IsNullOrWhiteSpace())
await writer.WriteLineAsync(embed.Thumbnail?.Url);
// Image URL
if (!(embed.Image?.Url).IsNullOrWhiteSpace())
await writer.WriteLineAsync(embed.Image?.Url);
// Footer text
if (!(embed.Footer?.Text).IsNullOrWhiteSpace())
await writer.WriteLineAsync(embed.Footer?.Text);
await writer.WriteLineAsync();
}
}
private async Task RenderReactionsAsync(TextWriter writer, IReadOnlyList<Reaction> reactions)
{
if (reactions.Any())
{
await writer.WriteLineAsync("{Reactions}");
foreach (var reaction in reactions)
{
await writer.WriteAsync(reaction.Emoji.Name);
if (reaction.Count > 1)
await writer.WriteAsync($" ({reaction.Count})");
await writer.WriteAsync(" ");
}
await writer.WriteLineAsync();
await writer.WriteLineAsync();
}
}
private async Task RenderMessageAsync(TextWriter writer, Message message)
{
// Header
await RenderMessageHeaderAsync(writer, message);
// Content
await writer.WriteLineAsync(FormatMarkdown(message.Content));
// Separator
await writer.WriteLineAsync();
// Attachments
await RenderAttachmentsAsync(writer, message.Attachments);
// Embeds
await RenderEmbedsAsync(writer, message.Embeds);
// Reactions
await RenderReactionsAsync(writer, message.Reactions);
}
public async Task RenderAsync(TextWriter writer)
{
// Metadata
await writer.WriteLineAsync('='.Repeat(62));
await writer.WriteLineAsync($"Guild: {_chatLog.Guild.Name}");
await writer.WriteLineAsync($"Channel: {_chatLog.Channel.Name}");
await writer.WriteLineAsync($"Topic: {_chatLog.Channel.Topic}");
await writer.WriteLineAsync($"Messages: {_chatLog.Messages.Count:N0}");
await writer.WriteLineAsync($"Range: {FormatDateRange(_chatLog.After, _chatLog.Before)}");
await writer.WriteLineAsync('='.Repeat(62));
await writer.WriteLineAsync();
// Log
foreach (var message in _chatLog.Messages)
await RenderMessageAsync(writer, message);
}
}
}
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using DiscordChatExporter.Core.Models;
namespace DiscordChatExporter.Core.Rendering
{
public class RenderContext
{
public Guild Guild { get; }
public Channel Channel { get; }
public DateTimeOffset? After { get; }
public DateTimeOffset? Before { get; }
public string DateFormat { get; }
public IReadOnlyCollection<User> MentionableUsers { get; }
public IReadOnlyCollection<Channel> MentionableChannels { get; }
public IReadOnlyCollection<Role> MentionableRoles { get; }
public RenderContext(Guild guild, Channel channel, DateTimeOffset? after, DateTimeOffset? before, string dateFormat,
IReadOnlyCollection<User> mentionableUsers, IReadOnlyCollection<Channel> mentionableChannels, IReadOnlyCollection<Role> mentionableRoles)
{
Guild = guild;
Channel = channel;
After = after;
Before = before;
DateFormat = dateFormat;
MentionableUsers = mentionableUsers;
MentionableChannels = mentionableChannels;
MentionableRoles = mentionableRoles;
}
}
}
@@ -0,0 +1,20 @@
using DiscordChatExporter.Core.Models;
namespace DiscordChatExporter.Core.Rendering
{
public class RenderOptions
{
public string BaseFilePath { get; }
public ExportFormat Format { get; }
public int? PartitionLimit { get; }
public RenderOptions(string baseFilePath, ExportFormat format, int? partitionLimit)
{
BaseFilePath = baseFilePath;
Format = format;
PartitionLimit = partitionLimit;
}
}
}
@@ -1,4 +1,4 @@
/* === GENERAL === */ /* General */
@font-face { @font-face {
font-family: Whitney; font-family: Whitney;
@@ -58,10 +58,10 @@ img {
} }
.quote { .quote {
margin: 0.5em 0;
padding-left: 0.6em;
border-left: 4px solid; border-left: 4px solid;
border-radius: 3px; border-radius: 3px;
margin: 8px 0;
padding-left: 10px;
} }
.pre { .pre {
@@ -69,8 +69,8 @@ img {
} }
.pre--multiline { .pre--multiline {
margin-top: 4px; margin-top: 0.25em;
padding: 8px; padding: 0.5em;
border: 2px solid; border: 2px solid;
border-radius: 5px; border-radius: 5px;
} }
@@ -78,7 +78,7 @@ img {
.pre--inline { .pre--inline {
padding: 2px; padding: 2px;
border-radius: 3px; border-radius: 3px;
font-size: 85%; font-size: 0.85em;
} }
.mention { .mention {
@@ -86,80 +86,69 @@ img {
} }
.emoji { .emoji {
width: 1.45em; width: 1.25em;
height: 1.45em; height: 1.25em;
margin: 0 1px; margin: 0 0.06em;
vertical-align: -0.4em; vertical-align: -0.4em;
} }
.emoji--small { .emoji--small {
width: 1rem; width: 1em;
height: 1rem; height: 1em;
} }
.emoji--large { .emoji--large {
width: 2rem; width: 2.8em;
height: 2rem; height: 2.8em;
} }
/* === INFO === */ /* Preamble */
.info { .preamble {
display: flex; display: grid;
margin: 0 0.3em 0.6em 0.3em;
max-width: 100%; max-width: 100%;
margin: 0 5px 10px 5px; grid-template-columns: auto 1fr;
} }
.info__guild-icon-container { .preamble__guild-icon-container {
flex: 0; grid-column: 1;
} }
.info__guild-icon { .preamble__guild-icon {
max-width: 88px; max-width: 88px;
max-height: 88px; max-height: 88px;
} }
.info__metadata { .preamble__entries-container {
flex: 1; grid-column: 2;
margin-left: 10px; margin-left: 0.6em;
} }
.info__guild-name { .preamble__entry {
font-size: 1.4em; font-size: 1.4em;
} }
.info__channel-name { .preamble__entry--small {
font-size: 1.2em; font-size: 1em;
} }
.info__channel-topic { /* Chatlog */
margin-top: 2px;
}
.info__channel-message-count {
margin-top: 2px;
}
.info__channel-date-range {
margin-top: 2px;
}
/* === CHATLOG === */
.chatlog { .chatlog {
max-width: 100%; max-width: 100%;
margin-bottom: 24px;
} }
.chatlog__message-group { .chatlog__message-group {
display: flex; display: grid;
margin: 0 10px; margin: 0 0.6em;
padding: 15px 0; padding: 0.9em 0;
border-top: 1px solid; border-top: 1px solid;
grid-template-columns: auto 1fr;
} }
.chatlog__author-avatar-container { .chatlog__author-avatar-container {
flex: 0; grid-column: 1;
width: 40px; width: 40px;
height: 40px; height: 40px;
} }
@@ -171,55 +160,53 @@ img {
} }
.chatlog__messages { .chatlog__messages {
flex: 1; grid-column: 2;
margin-left: 1.2em;
min-width: 50%; min-width: 50%;
margin-left: 20px;
} }
.chatlog__author-name { .chatlog__author-name {
font-size: 1em;
font-weight: 500; font-weight: 500;
} }
.chatlog__timestamp { .chatlog__timestamp {
margin-left: 5px; margin-left: 0.3em;
font-size: .75em; font-size: 0.75em;
} }
.chatlog__message { .chatlog__message {
padding: 2px 5px; padding: 0.1em 0.3em;
margin-right: -5px; margin: 0 -0.3em;
margin-left: -5px;
background-color: transparent; background-color: transparent;
transition: background-color 1s ease; transition: background-color 1s ease;
} }
.chatlog__content { .chatlog__content {
font-size: .9375em; font-size: 0.95em;
word-wrap: break-word; word-wrap: break-word;
} }
.chatlog__edited-timestamp { .chatlog__edited-timestamp {
margin-left: 3px; margin-left: 0.15em;
font-size: .8em; font-size: 0.8em;
} }
.chatlog__attachment-thumbnail { .chatlog__attachment-thumbnail {
margin-top: 5px; margin-top: 0.3em;
max-width: 50%; max-width: 50%;
max-height: 500px; max-height: 500px;
border-radius: 3px; border-radius: 3px;
} }
.chatlog__embed { .chatlog__embed {
margin-top: 5px;
display: flex; display: flex;
margin-top: 0.3em;
max-width: 520px; max-width: 520px;
} }
.chatlog__embed-color-pill { .chatlog__embed-color-pill {
flex-shrink: 0; flex-shrink: 0;
width: 4px; width: 0.25em;
border-top-left-radius: 3px; border-top-left-radius: 3px;
border-bottom-left-radius: 3px; border-bottom-left-radius: 3px;
} }
@@ -227,15 +214,15 @@ img {
.chatlog__embed-content-container { .chatlog__embed-content-container {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
padding: 8px 10px; padding: 0.5em 0.6em;
border: 1px solid; border: 1px solid;
border-top-right-radius: 3px; border-top-right-radius: 3px;
border-bottom-right-radius: 3px; border-bottom-right-radius: 3px;
} }
.chatlog__embed-content { .chatlog__embed-content {
width: 100%;
display: flex; display: flex;
width: 100%;
} }
.chatlog__embed-text { .chatlog__embed-text {
@@ -244,31 +231,31 @@ img {
.chatlog__embed-author { .chatlog__embed-author {
display: flex; display: flex;
margin-bottom: 0.3em;
align-items: center; align-items: center;
margin-bottom: 5px;
} }
.chatlog__embed-author-icon { .chatlog__embed-author-icon {
margin-right: 0.5em;
width: 20px; width: 20px;
height: 20px; height: 20px;
margin-right: 9px;
border-radius: 50%; border-radius: 50%;
} }
.chatlog__embed-author-name { .chatlog__embed-author-name {
font-size: .875em; font-size: 0.875em;
font-weight: 600; font-weight: 600;
} }
.chatlog__embed-title { .chatlog__embed-title {
margin-bottom: 4px; margin-bottom: 0.2em;
font-size: .875em; font-size: 0.875em;
font-weight: 600; font-weight: 600;
} }
.chatlog__embed-description { .chatlog__embed-description {
font-weight: 500; font-weight: 500;
font-size: 14px; font-size: 0.85em;
} }
.chatlog__embed-fields { .chatlog__embed-fields {
@@ -280,7 +267,8 @@ img {
flex: 0; flex: 0;
min-width: 100%; min-width: 100%;
max-width: 506px; max-width: 506px;
padding-top: 10px; padding-top: 0.6em;
font-size: 0.875em;
} }
.chatlog__embed-field--inline { .chatlog__embed-field--inline {
@@ -290,26 +278,24 @@ img {
} }
.chatlog__embed-field-name { .chatlog__embed-field-name {
margin-bottom: 4px; margin-bottom: 0.2em;
font-size: .875em;
font-weight: 600; font-weight: 600;
} }
.chatlog__embed-field-value { .chatlog__embed-field-value {
font-size: .875em;
font-weight: 500; font-weight: 500;
} }
.chatlog__embed-thumbnail { .chatlog__embed-thumbnail {
flex: 0; flex: 0;
margin-left: 20px; margin-left: 1.2em;
max-width: 80px; max-width: 80px;
max-height: 80px; max-height: 80px;
border-radius: 3px; border-radius: 3px;
} }
.chatlog__embed-image-container { .chatlog__embed-image-container {
margin-top: 10px; margin-top: 0.6em;
} }
.chatlog__embed-image { .chatlog__embed-image {
@@ -319,11 +305,11 @@ img {
} }
.chatlog__embed-footer { .chatlog__embed-footer {
margin-top: 10px; margin-top: 0.6em;
} }
.chatlog__embed-footer-icon { .chatlog__embed-footer-icon {
margin-right: 4px; margin-right: 0.2em;
width: 20px; width: 20px;
height: 20px; height: 20px;
border-radius: 50%; border-radius: 50%;
@@ -331,8 +317,8 @@ img {
} }
.chatlog__embed-footer-text { .chatlog__embed-footer-text {
font-size: 0.75em;
font-weight: 500; font-weight: 500;
font-size: .75em;
} }
.chatlog__reactions { .chatlog__reactions {
@@ -342,26 +328,35 @@ img {
.chatlog__reaction { .chatlog__reaction {
display: flex; display: flex;
align-items: center; align-items: center;
margin: 6px 2px 2px 2px; margin: 0.35em 0.1em 0.1em 0.1em;
padding: 3px 6px; padding: 0.2em 0.35em;
border-radius: 3px; border-radius: 3px;
} }
.chatlog__reaction-count { .chatlog__reaction-count {
min-width: 9px; min-width: 9px;
margin-left: 6px; margin-left: 0.35em;
font-size: .875em; font-size: 0.875em;
} }
.chatlog__bot-tag { .chatlog__bot-tag {
position: relative;
top: -.2em;
margin-left: 0.3em; margin-left: 0.3em;
background: #7289da; padding: 0.05em 0.3em;
color: #ffffff;
font-size: 0.625em;
padding: 1px 2px;
border-radius: 3px; border-radius: 3px;
vertical-align: middle; vertical-align: middle;
line-height: 1.3; line-height: 1.3;
position: relative; background: #7289da;
top: -.2em; color: #ffffff;
font-size: 0.625em;
font-weight: 500;
}
/* Postamble */
.postamble {
margin: 1.4em 0.3em 0.6em 0.3em;
padding: 1em;
border-top: 1px solid;
} }
@@ -1,4 +1,4 @@
/* === GENERAL === */ /* General */
body { body {
background-color: #36393e; background-color: #36393e;
@@ -23,28 +23,20 @@ a {
.pre--multiline { .pre--multiline {
border-color: #282b30 !important; border-color: #282b30 !important;
color: #839496 !important; color: #b9bbbe !important;
} }
.mention { .mention {
color: #7289da; color: #7289da;
} }
/* === INFO === */ /* === Preamble === */
.info__guild-name { .preamble__entry {
color: #ffffff; color: #ffffff;
} }
.info__channel-name { /* Chatlog */
color: #ffffff;
}
.info__channel-topic {
color: #ffffff;
}
/* === CHATLOG === */
.chatlog__message-group { .chatlog__message-group {
border-color: rgba(255, 255, 255, 0.1); border-color: rgba(255, 255, 255, 0.1);
@@ -70,6 +62,10 @@ a {
color: rgba(255, 255, 255, 0.2); color: rgba(255, 255, 255, 0.2);
} }
.chatlog__embed-color-pill--default {
background-color: rgba(79, 84, 92, 1);
}
.chatlog__embed-content-container { .chatlog__embed-content-container {
background-color: rgba(46, 48, 54, 0.3); background-color: rgba(46, 48, 54, 0.3);
border-color: rgba(46, 48, 54, 0.6); border-color: rgba(46, 48, 54, 0.6);
@@ -110,3 +106,13 @@ a {
.chatlog__reaction-count { .chatlog__reaction-count {
color: rgba(255, 255, 255, 0.3); color: rgba(255, 255, 255, 0.3);
} }
/* Postamble */
.postamble {
border-color: rgba(255, 255, 255, 0.1);
}
.postamble__entry {
color: #ffffff;
}
@@ -1,3 +0,0 @@
{{~ ThemeStyleSheet = include "HtmlDark.css" ~}}
{{~ HighlightJsStyleName = "solarized-dark" ~}}
{{~ include "HtmlShared.html" ~}}
@@ -0,0 +1,89 @@
<!DOCTYPE html>
<html lang="en">
<head>
{{~ # Metadata ~}}
<title>{{ Context.Guild.Name | html.escape }} - {{ Context.Channel.Name | html.escape }}</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
{{~ # Styles ~}}
<style>
{{ CoreStyleSheet }}
</style>
<style>
{{ ThemeStyleSheet }}
</style>
{{~ # Syntax highlighting ~}}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/styles/{{HighlightJsStyleName}}.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/highlight.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('.pre--multiline').forEach(block => hljs.highlightBlock(block));
});
</script>
{{~ # Local scripts ~}}
<script>
function scrollToMessage(event, id) {
var element = document.getElementById('message-' + id);
if (element) {
event.preventDefault();
element.classList.add('chatlog__message--highlighted');
window.scrollTo({
top: element.getBoundingClientRect().top - document.body.getBoundingClientRect().top - (window.innerHeight / 2),
behavior: 'smooth'
});
window.setTimeout(function() {
element.classList.remove('chatlog__message--highlighted');
}, 2000);
}
}
</script>
</head>
<body>
{{~ # Preamble ~}}
<div class="preamble">
<div class="preamble__guild-icon-container">
<img class="preamble__guild-icon" src="{{ Context.Guild.IconUrl }}" alt="Guild icon" />
</div>
<div class="preamble__entries-container">
<div class="preamble__entry">{{ Context.Guild.Name | html.escape }}</div>
<div class="preamble__entry">{{ Context.Channel.Name | html.escape }}</div>
{{~ if Context.Channel.Topic ~}}
<div class="preamble__entry preamble__entry--small">{{ Context.Channel.Topic | html.escape }}</div>
{{~ end ~}}
{{~ if Context.After || Context.Before ~}}
<div class="preamble__entry preamble__entry--small">
{{~ if Context.After && Context.Before ~}}
Between {{ Context.After | FormatDate | html.escape }} and {{ Context.Before | FormatDate | html.escape }}
{{~ else if Context.After ~}}
After {{ Context.After | FormatDate | html.escape }}
{{~ else if Context.Before ~}}
Before {{ Context.Before | FormatDate | html.escape }}
{{~ end ~}}
</div>
{{~ end ~}}
</div>
</div>
{{~ # Log ~}}
<div class="chatlog">
{{~ %SPLIT% ~}}
</div>
{{~ # Postamble ~}}
<div class="postamble">
<div class="postamble__entry">Exported {{ MessageCount | object.format "N0" }} message(s)</div>
</div>
</body>
</html>
@@ -1,4 +1,4 @@
/* === GENERAL === */ /* General */
body { body {
background-color: #ffffff; background-color: #ffffff;
@@ -32,21 +32,13 @@ a {
color: #7289da; color: #7289da;
} }
/* === INFO === */ /* Preamble */
.info__guild-name { .preamble__entry {
color: #2f3136; color: #2f3136;
} }
.info__channel-name { /* Chatlog */
color: #2f3136;
}
.info__channel-topic {
color: #2f3136;
}
/* === CHATLOG === */
.chatlog__message-group { .chatlog__message-group {
border-color: #eceeef; border-color: #eceeef;
@@ -73,6 +65,10 @@ a {
color: #747f8d; color: #747f8d;
} }
.chatlog__embed-color-pill--default {
background-color: rgba(227, 229, 232, 1);
}
.chatlog__embed-content-container { .chatlog__embed-content-container {
background-color: rgba(249, 249, 249, 0.3); background-color: rgba(249, 249, 249, 0.3);
border-color: rgba(204, 204, 204, 0.3); border-color: rgba(204, 204, 204, 0.3);
@@ -113,3 +109,13 @@ a {
.chatlog__reaction-count { .chatlog__reaction-count {
color: #747f8d; color: #747f8d;
} }
/* Postamble */
.postamble {
border-color: #eceeef;
}
.postamble__entry {
color: #2f3136;
}
@@ -1,3 +0,0 @@
{{~ ThemeStyleSheet = include "HtmlLight.css" ~}}
{{~ HighlightJsStyleName = "solarized-light" ~}}
{{~ include "HtmlShared.html" ~}}
@@ -0,0 +1,170 @@
<div class="chatlog__message-group">
{{~ # Avatar ~}}
<div class="chatlog__author-avatar-container">
<img class="chatlog__author-avatar" src="{{ MessageGroup.Author.AvatarUrl }}" alt="Avatar" />
</div>
<div class="chatlog__messages">
{{~ # Author name and timestamp ~}}
<span class="chatlog__author-name" title="{{ MessageGroup.Author.FullName | html.escape }}" data-user-id="{{ MessageGroup.Author.Id | html.escape }}">{{ MessageGroup.Author.Name | html.escape }}</span>
{{~ # Bot tag ~}}
{{~ if MessageGroup.Author.IsBot ~}}
<span class="chatlog__bot-tag">BOT</span>
{{~ end ~}}
<span class="chatlog__timestamp">{{ MessageGroup.Timestamp | FormatDate | html.escape }}</span>
{{~ # Messages ~}}
{{~ for message in MessageGroup.Messages ~}}
<div class="chatlog__message {{if message.IsPinned }}chatlog__message--pinned{{ end }}" data-message-id="{{ message.Id }}" id="message-{{ message.Id }}">
{{~ # Content ~}}
{{~ if message.Content ~}}
<div class="chatlog__content">
<span class="markdown">{{ message.Content | FormatMarkdown }}</span>
{{~ # Edited timestamp ~}}
{{~ if message.EditedTimestamp ~}}
<span class="chatlog__edited-timestamp" title="{{ message.EditedTimestamp | FormatDate | html.escape }}">(edited)</span>
{{~ end ~}}
</div>
{{~ end ~}}
{{~ # Attachments ~}}
{{~ for attachment in message.Attachments ~}}
<div class="chatlog__attachment">
<a href="{{ attachment.Url }}">
{{ # Image }}
{{~ if attachment.IsImage ~}}
<img class="chatlog__attachment-thumbnail" src="{{ attachment.Url }}" alt="Attachment" />
{{~ # Non-image ~}}
{{~ else ~}}
Attachment: {{ attachment.FileName }} ({{ attachment.FileSize }})
{{~ end ~}}
</a>
</div>
{{~ end ~}}
{{~ # Embeds ~}}
{{~ for embed in message.Embeds ~}}
<div class="chatlog__embed">
{{~ if embed.Color ~}}
<div class="chatlog__embed-color-pill" style="background-color: rgba({{ embed.Color.R }},{{ embed.Color.G }},{{ embed.Color.B }},{{ embed.Color.A }})"></div>
{{~ else ~}}
<div class="chatlog__embed-color-pill chatlog__embed-color-pill--default"></div>
{{~ end ~}}
<div class="chatlog__embed-content-container">
<div class="chatlog__embed-content">
<div class="chatlog__embed-text">
{{~ # Author ~}}
{{~ if embed.Author ~}}
<div class="chatlog__embed-author">
{{~ if embed.Author.IconUrl ~}}
<img class="chatlog__embed-author-icon" src="{{ embed.Author.IconUrl }}" alt="Author icon" />
{{~ end ~}}
{{~ if embed.Author.Name ~}}
<span class="chatlog__embed-author-name">
{{~ if embed.Author.Url ~}}
<a class="chatlog__embed-author-name-link" href="{{ embed.Author.Url }}">{{ embed.Author.Name | html.escape }}</a>
{{~ else ~}}
{{ embed.Author.Name | html.escape }}
{{~ end ~}}
</span>
{{~ end ~}}
</div>
{{~ end ~}}
{{~ # Title ~}}
{{~ if embed.Title ~}}
<div class="chatlog__embed-title">
{{~ if embed.Url ~}}
<a class="chatlog__embed-title-link" href="{{ embed.Url }}"><span class="markdown">{{ embed.Title | FormatMarkdown }}</span></a>
{{~ else ~}}
<span class="markdown">{{ embed.Title | FormatMarkdown }}</span>
{{~ end ~}}
</div>
{{~ end ~}}
{{~ # Description ~}}
{{~ if embed.Description ~}}
<div class="chatlog__embed-description"><span class="markdown">{{ embed.Description | FormatMarkdown }}</span></div>
{{~ end ~}}
{{~ # Fields ~}}
{{~ if embed.Fields | array.size > 0 ~}}
<div class="chatlog__embed-fields">
{{~ for field in embed.Fields ~}}
<div class="chatlog__embed-field {{ if field.IsInline }} chatlog__embed-field--inline {{ end }}">
{{~ if field.Name ~}}
<div class="chatlog__embed-field-name"><span class="markdown">{{ field.Name | FormatMarkdown }}</span></div>
{{~ end ~}}
{{~ if field.Value ~}}
<div class="chatlog__embed-field-value"><span class="markdown">{{ field.Value | FormatMarkdown }}</span></div>
{{~ end ~}}
</div>
{{~ end ~}}
</div>
{{~ end ~}}
</div>
{{~ # Thumbnail ~}}
{{~ if embed.Thumbnail ~}}
<div class="chatlog__embed-thumbnail-container">
<a class="chatlog__embed-thumbnail-link" href="{{ embed.Thumbnail.Url }}">
<img class="chatlog__embed-thumbnail" src="{{ embed.Thumbnail.Url }}" alt="Thumbnail" />
</a>
</div>
{{~ end ~}}
</div>
{{~ # Image ~}}
{{~ if embed.Image ~}}
<div class="chatlog__embed-image-container">
<a class="chatlog__embed-image-link" href="{{ embed.Image.Url }}">
<img class="chatlog__embed-image" src="{{ embed.Image.Url }}" alt="Image" />
</a>
</div>
{{~ end ~}}
{{~ # Footer ~}}
{{~ if embed.Footer || embed.Timestamp ~}}
<div class="chatlog__embed-footer">
{{~ if embed.Footer ~}}
{{~ if embed.Footer.Text && embed.Footer.IconUrl ~}}
<img class="chatlog__embed-footer-icon" src="{{ embed.Footer.IconUrl }}" alt="Footer icon" />
{{~ end ~}}
{{~ end ~}}
<span class="chatlog__embed-footer-text">
{{~ if embed.Footer ~}}
{{~ if embed.Footer.Text ~}}
{{ embed.Footer.Text | html.escape }}
{{ if embed.Timestamp }} • {{ end }}
{{~ end ~}}
{{~ end ~}}
{{~ if embed.Timestamp ~}}
{{ embed.Timestamp | FormatDate | html.escape }}
{{~ end ~}}
</span>
</div>
{{~ end ~}}
</div>
</div>
{{~ end ~}}
{{~ # Reactions ~}}
{{~ if message.Reactions | array.size > 0 ~}}
<div class="chatlog__reactions">
{{~ for reaction in message.Reactions ~}}
<div class="chatlog__reaction">
<img class="emoji emoji--small" alt="{{ reaction.Emoji.Name }}" title="{{ reaction.Emoji.Name }}" src="{{ reaction.Emoji.ImageUrl }}" />
<span class="chatlog__reaction-count">{{ reaction.Count }}</span>
</div>
{{~ end ~}}
</div>
{{~ end ~}}
</div>
{{~ end ~}}
</div>
</div>
@@ -1,255 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
{{~ # Metadata ~}}
<title>{{ Model.Guild.Name | html.escape }} - {{ Model.Channel.Name | html.escape }}</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
{{~ # Styles ~}}
<style>
{{ include "HtmlShared.css" }}
</style>
<style>
{{ ThemeStyleSheet }}
</style>
{{~ # Local scripts ~}}
<script>
function scrollToMessage(event, id) {
var element = document.getElementById('message-' + id);
if (element !== null && element !== undefined) {
event.preventDefault();
element.classList.add('chatlog__message--highlighted');
window.scrollTo({
top: element.getBoundingClientRect().top - document.body.getBoundingClientRect().top - (window.innerHeight / 2),
behavior: 'smooth'
});
window.setTimeout(function() {
element.classList.remove('chatlog__message--highlighted');
}, 2000);
}
}
</script>
{{~ # Syntax highlighting ~}}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/styles/{{HighlightJsStyleName}}.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/highlight.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('.pre--multiline').forEach((block) => {
hljs.highlightBlock(block);
});
});
</script>
</head>
<body>
{{~ # Info ~}}
<div class="info">
<div class="info__guild-icon-container">
<img class="info__guild-icon" src="{{ Model.Guild.IconUrl }}" />
</div>
<div class="info__metadata">
<div class="info__guild-name">{{ Model.Guild.Name | html.escape }}</div>
<div class="info__channel-name">{{ Model.Channel.Name | html.escape }}</div>
{{~ if Model.Channel.Topic ~}}
<div class="info__channel-topic">{{ Model.Channel.Topic | html.escape }}</div>
{{~ end ~}}
<div class="info__channel-message-count">{{ Model.Messages | array.size | object.format "N0" }} messages</div>
{{~ if Model.After || Model.Before ~}}
<div class="info__channel-date-range">
{{~ if Model.After && Model.Before ~}}
Between {{ Model.After | FormatDate | html.escape }} and {{ Model.Before | FormatDate | html.escape }}
{{~ else if Model.After ~}}
After {{ Model.After | FormatDate | html.escape }}
{{~ else if Model.Before ~}}
Before {{ Model.Before | FormatDate | html.escape }}
{{~ end ~}}
</div>
{{~ end ~}}
</div>
</div>
{{~ # Log ~}}
<div class="chatlog">
{{~ for group in Model.Messages | GroupMessages ~}}
<div class="chatlog__message-group">
{{~ # Avatar ~}}
<div class="chatlog__author-avatar-container">
<img class="chatlog__author-avatar" src="{{ group.Author.AvatarUrl }}" />
</div>
<div class="chatlog__messages">
{{~ # Author name and timestamp ~}}
<span class="chatlog__author-name" title="{{ group.Author.FullName | html.escape }}" data-user-id="{{ group.Author.Id | html.escape }}">{{ group.Author.Name | html.escape }}</span>
{{~ # Bot tag ~}}
{{~ if group.Author.IsBot ~}}
<span class="chatlog__bot-tag">BOT</span>
{{~ end ~}}
<span class="chatlog__timestamp">{{ group.Timestamp | FormatDate | html.escape }}</span>
{{~ # Messages ~}}
{{~ for message in group.Messages ~}}
<div class="chatlog__message {{if message.IsPinned }}chatlog__message--pinned{{ end }}" data-message-id="{{ message.Id }}" id="message-{{ message.Id }}">
{{~ # Content ~}}
{{~ if message.Content ~}}
<div class="chatlog__content">
<span class="markdown">{{ message.Content | FormatMarkdown }}</span>
{{~ # Edited timestamp ~}}
{{~ if message.EditedTimestamp ~}}
<span class="chatlog__edited-timestamp" title="{{ message.EditedTimestamp | FormatDate | html.escape }}">(edited)</span>
{{~ end ~}}
</div>
{{~ end ~}}
{{~ # Attachments ~}}
{{~ for attachment in message.Attachments ~}}
<div class="chatlog__attachment">
<a href="{{ attachment.Url }}">
{{ # Image }}
{{~ if attachment.IsImage ~}}
<img class="chatlog__attachment-thumbnail" src="{{ attachment.Url }}" />
{{~ # Non-image ~}}
{{~ else ~}}
Attachment: {{ attachment.FileName }} ({{ attachment.FileSize }})
{{~ end ~}}
</a>
</div>
{{~ end ~}}
{{~ # Embeds ~}}
{{~ for embed in message.Embeds ~}}
<div class="chatlog__embed">
<div class="chatlog__embed-color-pill" style="background-color: rgba({{ embed.Color.R }},{{ embed.Color.G }},{{ embed.Color.B }},{{ embed.Color.A }})"></div>
<div class="chatlog__embed-content-container">
<div class="chatlog__embed-content">
<div class="chatlog__embed-text">
{{~ # Author ~}}
{{~ if embed.Author ~}}
<div class="chatlog__embed-author">
{{~ if embed.Author.IconUrl ~}}
<img class="chatlog__embed-author-icon" src="{{ embed.Author.IconUrl }}" />
{{~ end ~}}
{{~ if embed.Author.Name ~}}
<span class="chatlog__embed-author-name">
{{~ if embed.Author.Url ~}}
<a class="chatlog__embed-author-name-link" href="{{ embed.Author.Url }}">{{ embed.Author.Name | html.escape }}</a>
{{~ else ~}}
{{ embed.Author.Name | html.escape }}
{{~ end ~}}
</span>
{{~ end ~}}
</div>
{{~ end ~}}
{{~ # Title ~}}
{{~ if embed.Title ~}}
<div class="chatlog__embed-title">
{{~ if embed.Url ~}}
<a class="chatlog__embed-title-link" href="{{ embed.Url }}"><span class="markdown">{{ embed.Title | FormatMarkdown }}</span></a>
{{~ else ~}}
<span class="markdown">{{ embed.Title | FormatMarkdown }}</span>
{{~ end ~}}
</div>
{{~ end ~}}
{{~ # Description ~}}
{{~ if embed.Description ~}}
<div class="chatlog__embed-description"><span class="markdown">{{ embed.Description | FormatMarkdown }}</span></div>
{{~ end ~}}
{{~ # Fields ~}}
{{~ if embed.Fields | array.size > 0 ~}}
<div class="chatlog__embed-fields">
{{~ for field in embed.Fields ~}}
<div class="chatlog__embed-field {{ if field.IsInline }} chatlog__embed-field--inline {{ end }}">
{{~ if field.Name ~}}
<div class="chatlog__embed-field-name"><span class="markdown">{{ field.Name | FormatMarkdown }}</span></div>
{{~ end ~}}
{{~ if field.Value ~}}
<div class="chatlog__embed-field-value"><span class="markdown">{{ field.Value | FormatMarkdown }}</span></div>
{{~ end ~}}
</div>
{{~ end ~}}
</div>
{{~ end ~}}
</div>
{{~ # Thumbnail ~}}
{{~ if embed.Thumbnail ~}}
<div class="chatlog__embed-thumbnail-container">
<a class="chatlog__embed-thumbnail-link" href="{{ embed.Thumbnail.Url }}">
<img class="chatlog__embed-thumbnail" src="{{ embed.Thumbnail.Url }}" />
</a>
</div>
{{~ end ~}}
</div>
{{~ # Image ~}}
{{~ if embed.Image ~}}
<div class="chatlog__embed-image-container">
<a class="chatlog__embed-image-link" href="{{ embed.Image.Url }}">
<img class="chatlog__embed-image" src="{{ embed.Image.Url }}" />
</a>
</div>
{{~ end ~}}
{{~ # Footer ~}}
{{~ if embed.Footer || embed.Timestamp ~}}
<div class="chatlog__embed-footer">
{{~ if embed.Footer ~}}
{{~ if embed.Footer.Text && embed.Footer.IconUrl ~}}
<img class="chatlog__embed-footer-icon" src="{{ embed.Footer.IconUrl }}" />
{{~ end ~}}
{{~ end ~}}
<span class="chatlog__embed-footer-text">
{{~ if embed.Footer ~}}
{{~ if embed.Footer.Text ~}}
{{ embed.Footer.Text | html.escape }}
{{ if embed.Timestamp }} • {{ end }}
{{~ end ~}}
{{~ end ~}}
{{~ if embed.Timestamp ~}}
{{ embed.Timestamp | FormatDate | html.escape }}
{{~ end ~}}
</span>
</div>
{{~ end ~}}
</div>
</div>
{{~ end ~}}
{{~ # Reactions ~}}
{{~ if message.Reactions | array.size > 0 ~}}
<div class="chatlog__reactions">
{{~ for reaction in message.Reactions ~}}
<div class="chatlog__reaction">
<img class="emoji emoji--small" alt="{{ reaction.Emoji.Name }}" title="{{ reaction.Emoji.Name }}" src="{{ reaction.Emoji.ImageUrl }}" />
<span class="chatlog__reaction-count">{{ reaction.Count }}</span>
</div>
{{~ end ~}}
</div>
{{~ end ~}}
</div>
{{~ end ~}}
</div>
</div>
{{~ end ~}}
</div>
</body>
</html>
@@ -12,10 +12,10 @@ namespace DiscordChatExporter.Core.Services
{ {
private User ParseUser(JToken json) private User ParseUser(JToken json)
{ {
var id = json["id"].Value<string>(); var id = json["id"]!.Value<string>();
var discriminator = json["discriminator"].Value<int>(); var discriminator = json["discriminator"]!.Value<int>();
var name = json["username"].Value<string>(); var name = json["username"]!.Value<string>();
var avatarHash = json["avatar"].Value<string>(); var avatarHash = json["avatar"]!.Value<string>();
var isBot = json["bot"]?.Value<bool>() ?? false; var isBot = json["bot"]?.Value<bool>() ?? false;
return new User(id, discriminator, name, avatarHash, isBot); return new User(id, discriminator, name, avatarHash, isBot);
@@ -23,9 +23,9 @@ namespace DiscordChatExporter.Core.Services
private Guild ParseGuild(JToken json) private Guild ParseGuild(JToken json)
{ {
var id = json["id"].Value<string>(); var id = json["id"]!.Value<string>();
var name = json["name"].Value<string>(); var name = json["name"]!.Value<string>();
var iconHash = json["icon"].Value<string>(); var iconHash = json["icon"]!.Value<string>();
return new Guild(id, name, iconHash); return new Guild(id, name, iconHash);
} }
@@ -33,44 +33,49 @@ namespace DiscordChatExporter.Core.Services
private Channel ParseChannel(JToken json) private Channel ParseChannel(JToken json)
{ {
// Get basic data // Get basic data
var id = json["id"].Value<string>(); var id = json["id"]!.Value<string>();
var parentId = json["parent_id"]?.Value<string>(); var parentId = json["parent_id"]?.Value<string>();
var type = (ChannelType) json["type"].Value<int>(); var type = (ChannelType) json["type"]!.Value<int>();
var topic = json["topic"]?.Value<string>(); var topic = json["topic"]?.Value<string>();
// Try to extract guild ID // Try to extract guild ID
var guildId = json["guild_id"]?.Value<string>(); var guildId = json["guild_id"]?.Value<string>();
// If the guild ID is blank, it's direct messages // If the guild ID is blank, it's direct messages
if (guildId.IsNullOrWhiteSpace()) if (string.IsNullOrWhiteSpace(guildId))
guildId = Guild.DirectMessages.Id; guildId = Guild.DirectMessages.Id;
// Try to extract name // Try to extract name
var name = json["name"]?.Value<string>(); var name = json["name"]?.Value<string>();
// If the name is blank, it's direct messages // If the name is blank, it's direct messages
if (name.IsNullOrWhiteSpace()) if (string.IsNullOrWhiteSpace(name))
name = json["recipients"].Select(ParseUser).Select(u => u.Name).JoinToString(", "); name = json["recipients"]?.Select(ParseUser).Select(u => u.Name).JoinToString(", ");
// If the name is still blank for some reason, fallback to ID
// (blind fix to https://github.com/Tyrrrz/DiscordChatExporter/issues/227)
if (string.IsNullOrWhiteSpace(name))
name = id;
return new Channel(id, parentId, guildId, name, topic, type); return new Channel(id, parentId, guildId, name, topic, type);
} }
private Role ParseRole(JToken json) private Role ParseRole(JToken json)
{ {
var id = json["id"].Value<string>(); var id = json["id"]!.Value<string>();
var name = json["name"].Value<string>(); var name = json["name"]!.Value<string>();
return new Role(id, name); return new Role(id, name);
} }
private Attachment ParseAttachment(JToken json) private Attachment ParseAttachment(JToken json)
{ {
var id = json["id"].Value<string>(); var id = json["id"]!.Value<string>();
var url = json["url"].Value<string>(); var url = json["url"]!.Value<string>();
var width = json["width"]?.Value<int>(); var width = json["width"]?.Value<int>();
var height = json["height"]?.Value<int>(); var height = json["height"]?.Value<int>();
var fileName = json["filename"].Value<string>(); var fileName = json["filename"]!.Value<string>();
var fileSizeBytes = json["size"].Value<long>(); var fileSizeBytes = json["size"]!.Value<long>();
var fileSize = new FileSize(fileSizeBytes); var fileSize = new FileSize(fileSizeBytes);
@@ -88,8 +93,8 @@ namespace DiscordChatExporter.Core.Services
private EmbedField ParseEmbedField(JToken json) private EmbedField ParseEmbedField(JToken json)
{ {
var name = json["name"].Value<string>(); var name = json["name"]!.Value<string>();
var value = json["value"].Value<string>(); var value = json["value"]!.Value<string>();
var isInline = json["inline"]?.Value<bool>() ?? false; var isInline = json["inline"]?.Value<bool>() ?? false;
return new EmbedField(name, value, isInline); return new EmbedField(name, value, isInline);
@@ -106,7 +111,7 @@ namespace DiscordChatExporter.Core.Services
private EmbedFooter ParseEmbedFooter(JToken json) private EmbedFooter ParseEmbedFooter(JToken json)
{ {
var text = json["text"].Value<string>(); var text = json["text"]!.Value<string>();
var iconUrl = json["icon_url"]?.Value<string>(); var iconUrl = json["icon_url"]?.Value<string>();
return new EmbedFooter(text, iconUrl); return new EmbedFooter(text, iconUrl);
@@ -122,23 +127,23 @@ namespace DiscordChatExporter.Core.Services
// Get color // Get color
var color = json["color"] != null var color = json["color"] != null
? Color.FromArgb(json["color"].Value<int>()).ResetAlpha() ? Color.FromArgb(json["color"]!.Value<int>()).ResetAlpha()
: Color.FromArgb(79, 84, 92); // default color : default(Color?);
// Get author // Get author
var author = json["author"] != null ? ParseEmbedAuthor(json["author"]) : null; var author = json["author"] != null ? ParseEmbedAuthor(json["author"]!) : null;
// Get fields // Get fields
var fields = json["fields"].EmptyIfNull().Select(ParseEmbedField).ToArray(); var fields = (json["fields"] ?? Enumerable.Empty<JToken>()).Select(ParseEmbedField).ToArray();
// Get thumbnail // Get thumbnail
var thumbnail = json["thumbnail"] != null ? ParseEmbedImage(json["thumbnail"]) : null; var thumbnail = json["thumbnail"] != null ? ParseEmbedImage(json["thumbnail"]!) : null;
// Get image // Get image
var image = json["image"] != null ? ParseEmbedImage(json["image"]) : null; var image = json["image"] != null ? ParseEmbedImage(json["image"]!) : null;
// Get footer // Get footer
var footer = json["footer"] != null ? ParseEmbedFooter(json["footer"]) : null; var footer = json["footer"] != null ? ParseEmbedFooter(json["footer"]!) : null;
return new Embed(title, url, timestamp, color, author, description, fields, thumbnail, image, footer); return new Embed(title, url, timestamp, color, author, description, fields, thumbnail, image, footer);
} }
@@ -146,7 +151,7 @@ namespace DiscordChatExporter.Core.Services
private Emoji ParseEmoji(JToken json) private Emoji ParseEmoji(JToken json)
{ {
var id = json["id"]?.Value<string>(); var id = json["id"]?.Value<string>();
var name = json["name"]?.Value<string>(); var name = json["name"]!.Value<string>();
var isAnimated = json["animated"]?.Value<bool>() ?? false; var isAnimated = json["animated"]?.Value<bool>() ?? false;
return new Emoji(id, name, isAnimated); return new Emoji(id, name, isAnimated);
@@ -154,8 +159,8 @@ namespace DiscordChatExporter.Core.Services
private Reaction ParseReaction(JToken json) private Reaction ParseReaction(JToken json)
{ {
var count = json["count"].Value<int>(); var count = json["count"]!.Value<int>();
var emoji = ParseEmoji(json["emoji"]); var emoji = ParseEmoji(json["emoji"]!);
return new Reaction(count, emoji); return new Reaction(count, emoji);
} }
@@ -163,12 +168,12 @@ namespace DiscordChatExporter.Core.Services
private Message ParseMessage(JToken json) private Message ParseMessage(JToken json)
{ {
// Get basic data // Get basic data
var id = json["id"].Value<string>(); var id = json["id"]!.Value<string>();
var channelId = json["channel_id"].Value<string>(); var channelId = json["channel_id"]!.Value<string>();
var timestamp = json["timestamp"].Value<DateTime>().ToDateTimeOffset(); var timestamp = json["timestamp"]!.Value<DateTime>().ToDateTimeOffset();
var editedTimestamp = json["edited_timestamp"]?.Value<DateTime?>()?.ToDateTimeOffset(); var editedTimestamp = json["edited_timestamp"]?.Value<DateTime?>()?.ToDateTimeOffset();
var content = json["content"].Value<string>(); var content = json["content"]!.Value<string>();
var type = (MessageType) json["type"].Value<int>(); var type = (MessageType) json["type"]!.Value<int>();
// Workarounds for non-default types // Workarounds for non-default types
if (type == MessageType.RecipientAdd) if (type == MessageType.RecipientAdd)
@@ -187,25 +192,25 @@ namespace DiscordChatExporter.Core.Services
content = "Joined the server."; content = "Joined the server.";
// Get author // Get author
var author = ParseUser(json["author"]); var author = ParseUser(json["author"]!);
// Get attachments // Get attachments
var attachments = json["attachments"].EmptyIfNull().Select(ParseAttachment).ToArray(); var attachments = (json["attachments"] ?? Enumerable.Empty<JToken>()).Select(ParseAttachment).ToArray();
// Get embeds // Get embeds
var embeds = json["embeds"].EmptyIfNull().Select(ParseEmbed).ToArray(); var embeds = (json["embeds"] ?? Enumerable.Empty<JToken>()).Select(ParseEmbed).ToArray();
// Get reactions // Get reactions
var reactions = json["reactions"].EmptyIfNull().Select(ParseReaction).ToArray(); var reactions = (json["reactions"] ?? Enumerable.Empty<JToken>()).Select(ParseReaction).ToArray();
// Get mentioned users // Get mentions
var mentionedUsers = json["mentions"].EmptyIfNull().Select(ParseUser).ToArray(); var mentionedUsers = (json["mentions"] ?? Enumerable.Empty<JToken>()).Select(ParseUser).ToArray();
// Get whether this message is pinned // Get whether this message is pinned
var isPinned = json["pinned"].Value<bool>(); var isPinned = json["pinned"]!.Value<bool>();
return new Message(id, channelId, type, author, timestamp, editedTimestamp, content, attachments, embeds, return new Message(id, channelId, type, author, timestamp, editedTimestamp, isPinned, content, attachments, embeds,
reactions, mentionedUsers, isPinned); reactions, mentionedUsers);
} }
} }
} }
+105 -147
View File
@@ -1,73 +1,65 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Threading.Tasks; using System.Threading.Tasks;
using DiscordChatExporter.Core.Models; using DiscordChatExporter.Core.Models;
using DiscordChatExporter.Core.Services.Exceptions; using DiscordChatExporter.Core.Services.Exceptions;
using DiscordChatExporter.Core.Services.Internal; using DiscordChatExporter.Core.Services.Internal;
using Failsafe;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Tyrrrz.Extensions; using Polly;
namespace DiscordChatExporter.Core.Services namespace DiscordChatExporter.Core.Services
{ {
public partial class DataService : IDisposable public partial class DataService : IDisposable
{ {
private readonly HttpClient _httpClient = new HttpClient(); private readonly HttpClient _httpClient = new HttpClient();
private readonly IAsyncPolicy<HttpResponseMessage> _httpPolicy;
private async Task<JToken> GetApiResponseAsync(AuthToken token, string resource, string endpoint, public DataService()
params string[] parameters)
{ {
// Create retry policy _httpClient.BaseAddress = new Uri("https://discordapp.com/api/v6");
var retry = Retry.Create()
.Catch<HttpErrorStatusCodeException>(false, e => (int) e.StatusCode >= 500)
.Catch<HttpErrorStatusCodeException>(false, e => (int) e.StatusCode == 429)
.WithMaxTryCount(10)
.WithDelay(TimeSpan.FromSeconds(0.4));
// Send request // Discord seems to always respond 429 on our first request with unreasonable wait time (10+ minutes).
return await retry.ExecuteAsync(async () => // For that reason the policy will start respecting their retry-after header only after Nth failed response.
_httpPolicy = Policy
.HandleResult<HttpResponseMessage>(m => m.StatusCode == HttpStatusCode.TooManyRequests)
.OrResult(m => m.StatusCode >= HttpStatusCode.InternalServerError)
.WaitAndRetryAsync(6,
(i, result, ctx) =>
{
if (i <= 3)
return TimeSpan.FromSeconds(2 * i);
if (i <= 5)
return TimeSpan.FromSeconds(5 * i);
return result.Result.Headers.RetryAfter.Delta ?? TimeSpan.FromSeconds(10 * i);
},
(response, timespan, retryCount, context) => Task.CompletedTask);
}
private async Task<JToken> GetApiResponseAsync(AuthToken token, string route)
{
using var response = await _httpPolicy.ExecuteAsync(async () =>
{ {
// Create request using var request = new HttpRequestMessage(HttpMethod.Get, route);
const string apiRoot = "https://discordapp.com/api/v6";
using (var request = new HttpRequestMessage(HttpMethod.Get, $"{apiRoot}/{resource}/{endpoint}"))
{
// Set authorization header
request.Headers.Authorization = token.Type == AuthTokenType.Bot
? new AuthenticationHeaderValue("Bot", token.Value)
: new AuthenticationHeaderValue(token.Value);
// Add parameters request.Headers.Authorization = token.Type == AuthTokenType.Bot
foreach (var parameter in parameters) ? new AuthenticationHeaderValue("Bot", token.Value)
{ : new AuthenticationHeaderValue(token.Value);
var key = parameter.SubstringUntil("=");
var value = parameter.SubstringAfter("=");
// Skip empty values return await _httpClient.SendAsync(request);
if (value.IsNullOrWhiteSpace())
continue;
request.RequestUri = request.RequestUri.SetQueryParameter(key, value);
}
// Get response
using (var response = await _httpClient.SendAsync(request))
{
// Check status code
// We throw our own exception here because default one doesn't have status code
if (!response.IsSuccessStatusCode)
throw new HttpErrorStatusCodeException(response.StatusCode, response.ReasonPhrase);
// Get content
var raw = await response.Content.ReadAsStringAsync();
// Parse
return JToken.Parse(raw);
}
}
}); });
// We throw our own exception here because default one doesn't have status code
if (!response.IsSuccessStatusCode)
throw new HttpErrorStatusCodeException(response.StatusCode, response.ReasonPhrase);
var jsonRaw = await response.Content.ReadAsStringAsync();
return JToken.Parse(jsonRaw);
} }
public async Task<Guild> GetGuildAsync(AuthToken token, string guildId) public async Task<Guild> GetGuildAsync(AuthToken token, string guildId)
@@ -76,7 +68,7 @@ namespace DiscordChatExporter.Core.Services
if (guildId == Guild.DirectMessages.Id) if (guildId == Guild.DirectMessages.Id)
return Guild.DirectMessages; return Guild.DirectMessages;
var response = await GetApiResponseAsync(token, "guilds", guildId); var response = await GetApiResponseAsync(token, $"guilds/{guildId}");
var guild = ParseGuild(response); var guild = ParseGuild(response);
return guild; return guild;
@@ -84,23 +76,38 @@ namespace DiscordChatExporter.Core.Services
public async Task<Channel> GetChannelAsync(AuthToken token, string channelId) public async Task<Channel> GetChannelAsync(AuthToken token, string channelId)
{ {
var response = await GetApiResponseAsync(token, "channels", channelId); var response = await GetApiResponseAsync(token, $"channels/{channelId}");
var channel = ParseChannel(response); var channel = ParseChannel(response);
return channel; return channel;
} }
public async Task<IReadOnlyList<Guild>> GetUserGuildsAsync(AuthToken token) public async IAsyncEnumerable<Guild> GetUserGuildsAsync(AuthToken token)
{ {
var response = await GetApiResponseAsync(token, "users", "@me/guilds", "limit=100"); var afterId = "";
var guilds = response.Select(ParseGuild).ToArray();
return guilds; while (true)
{
var route = "users/@me/guilds?limit=100";
if (!string.IsNullOrWhiteSpace(afterId))
route += $"&after={afterId}";
var response = await GetApiResponseAsync(token, route);
if (!response.HasValues)
yield break;
foreach (var guild in response.Select(ParseGuild))
{
yield return guild;
afterId = guild.Id;
}
}
} }
public async Task<IReadOnlyList<Channel>> GetDirectMessageChannelsAsync(AuthToken token) public async Task<IReadOnlyList<Channel>> GetDirectMessageChannelsAsync(AuthToken token)
{ {
var response = await GetApiResponseAsync(token, "users", "@me/channels"); var response = await GetApiResponseAsync(token, "users/@me/channels");
var channels = response.Select(ParseChannel).ToArray(); var channels = response.Select(ParseChannel).ToArray();
return channels; return channels;
@@ -108,7 +115,11 @@ namespace DiscordChatExporter.Core.Services
public async Task<IReadOnlyList<Channel>> GetGuildChannelsAsync(AuthToken token, string guildId) public async Task<IReadOnlyList<Channel>> GetGuildChannelsAsync(AuthToken token, string guildId)
{ {
var response = await GetApiResponseAsync(token, "guilds", $"{guildId}/channels"); // Special case for direct messages pseudo-guild
if (guildId == Guild.DirectMessages.Id)
return Array.Empty<Channel>();
var response = await GetApiResponseAsync(token, $"guilds/{guildId}/channels");
var channels = response.Select(ParseChannel).ToArray(); var channels = response.Select(ParseChannel).ToArray();
return channels; return channels;
@@ -116,36 +127,48 @@ namespace DiscordChatExporter.Core.Services
public async Task<IReadOnlyList<Role>> GetGuildRolesAsync(AuthToken token, string guildId) public async Task<IReadOnlyList<Role>> GetGuildRolesAsync(AuthToken token, string guildId)
{ {
var response = await GetApiResponseAsync(token, "guilds", $"{guildId}/roles"); // Special case for direct messages pseudo-guild
if (guildId == Guild.DirectMessages.Id)
return Array.Empty<Role>();
var response = await GetApiResponseAsync(token, $"guilds/{guildId}/roles");
var roles = response.Select(ParseRole).ToArray(); var roles = response.Select(ParseRole).ToArray();
return roles; return roles;
} }
public async Task<IReadOnlyList<Message>> GetChannelMessagesAsync(AuthToken token, string channelId, private async Task<Message> GetLastMessageAsync(AuthToken token, string channelId, DateTimeOffset? before = null)
DateTimeOffset? after = null, DateTimeOffset? before = null, IProgress<double> progress = null)
{ {
var result = new List<Message>(); var route = $"channels/{channelId}/messages?limit=1";
if (before != null)
route += $"&before={before.Value.ToSnowflake()}";
var response = await GetApiResponseAsync(token, route);
return response.Select(ParseMessage).FirstOrDefault();
}
public async IAsyncEnumerable<Message> GetMessagesAsync(AuthToken token, string channelId,
DateTimeOffset? after = null, DateTimeOffset? before = null, IProgress<double>? progress = null)
{
// Get the last message // Get the last message
var response = await GetApiResponseAsync(token, "channels", $"{channelId}/messages", var lastMessage = await GetLastMessageAsync(token, channelId, before);
"limit=1", $"before={before?.ToSnowflake()}");
var lastMessage = response.Select(ParseMessage).FirstOrDefault();
// If the last message doesn't exist or it's outside of range - return // If the last message doesn't exist or it's outside of range - return
if (lastMessage == null || lastMessage.Timestamp < after) if (lastMessage == null || lastMessage.Timestamp < after)
{ {
progress?.Report(1); progress?.Report(1);
return result; yield break;
} }
// Get other messages // Get other messages
var offsetId = after?.ToSnowflake() ?? "0"; var firstMessage = default(Message);
var afterId = after?.ToSnowflake() ?? "0";
while (true) while (true)
{ {
// Get message batch // Get message batch
response = await GetApiResponseAsync(token, "channels", $"{channelId}/messages", var route = $"channels/{channelId}/messages?limit=100&after={afterId}";
"limit=100", $"after={offsetId}"); var response = await GetApiResponseAsync(token, route);
// Parse // Parse
var messages = response var messages = response
@@ -162,95 +185,30 @@ namespace DiscordChatExporter.Core.Services
.TakeWhile(m => m.Id != lastMessage.Id && m.Timestamp < lastMessage.Timestamp) .TakeWhile(m => m.Id != lastMessage.Id && m.Timestamp < lastMessage.Timestamp)
.ToArray(); .ToArray();
// Add to result // Yield messages
result.AddRange(messagesInRange); foreach (var message in messagesInRange)
{
// Set first message if it's not set
firstMessage ??= message;
// Report progress (based on the time range of parsed messages compared to total)
progress?.Report((message.Timestamp - firstMessage.Timestamp).TotalSeconds /
(lastMessage.Timestamp - firstMessage.Timestamp).TotalSeconds);
yield return message;
afterId = message.Id;
}
// Break if messages were trimmed (which means the last message was encountered) // Break if messages were trimmed (which means the last message was encountered)
if (messagesInRange.Length != messages.Length) if (messagesInRange.Length != messages.Length)
break; break;
// Report progress (based on the time range of parsed messages compared to total)
progress?.Report((result.Last().Timestamp - result.First().Timestamp).TotalSeconds /
(lastMessage.Timestamp - result.First().Timestamp).TotalSeconds);
// Move offset
offsetId = result.Last().Id;
} }
// Add last message // Yield last message
result.Add(lastMessage); yield return lastMessage;
// Report progress
progress?.Report(1); progress?.Report(1);
return result;
} }
public async Task<Mentionables> GetMentionablesAsync(AuthToken token, string guildId, public void Dispose() => _httpClient.Dispose();
IEnumerable<Message> messages)
{
// Get channels and roles
var channels = guildId != Guild.DirectMessages.Id
? await GetGuildChannelsAsync(token, guildId)
: Array.Empty<Channel>();
var roles = guildId != Guild.DirectMessages.Id
? await GetGuildRolesAsync(token, guildId)
: Array.Empty<Role>();
// Get users
var userMap = new Dictionary<string, User>();
foreach (var message in messages)
{
// Author
userMap[message.Author.Id] = message.Author;
// Mentioned users
foreach (var mentionedUser in message.MentionedUsers)
userMap[mentionedUser.Id] = mentionedUser;
}
var users = userMap.Values.ToArray();
return new Mentionables(users, channels, roles);
}
public async Task<ChatLog> GetChatLogAsync(AuthToken token, Guild guild, Channel channel,
DateTimeOffset? after = null, DateTimeOffset? before = null, IProgress<double> progress = null)
{
// Get messages
var messages = await GetChannelMessagesAsync(token, channel.Id, after, before, progress);
// Get mentionables
var mentionables = await GetMentionablesAsync(token, guild.Id, messages);
return new ChatLog(guild, channel, after, before, messages, mentionables);
}
public async Task<ChatLog> GetChatLogAsync(AuthToken token, Channel channel,
DateTimeOffset? after = null, DateTimeOffset? before = null, IProgress<double> progress = null)
{
// Get guild
var guild = channel.GuildId == Guild.DirectMessages.Id
? Guild.DirectMessages
: await GetGuildAsync(token, channel.GuildId);
// Get the chat log
return await GetChatLogAsync(token, guild, channel, after, before, progress);
}
public async Task<ChatLog> GetChatLogAsync(AuthToken token, string channelId,
DateTimeOffset? after = null, DateTimeOffset? before = null, IProgress<double> progress = null)
{
// Get channel
var channel = await GetChannelAsync(token, channelId);
// Get the chat log
return await GetChatLogAsync(token, channel, after, before, progress);
}
public void Dispose()
{
_httpClient.Dispose();
}
} }
} }
@@ -1,15 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<Import Project="../DiscordChatExporter.props" />
<PropertyGroup>
<TargetFrameworks>net46;netstandard2.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Failsafe" Version="1.1.0" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" /> <PackageReference Include="Onova" Version="2.5.2" />
<PackageReference Include="Onova" Version="2.4.5" /> <PackageReference Include="Polly" Version="7.2.0" />
<PackageReference Include="Tyrrrz.Extensions" Version="1.6.2" /> <PackageReference Include="Tyrrrz.Extensions" Version="1.6.5" />
<PackageReference Include="Tyrrrz.Settings" Version="1.3.4" /> <PackageReference Include="Tyrrrz.Settings" Version="1.3.4" />
</ItemGroup> </ItemGroup>
@@ -1,89 +1,83 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using DiscordChatExporter.Core.Models; using DiscordChatExporter.Core.Models;
using DiscordChatExporter.Core.Models.Exceptions;
using DiscordChatExporter.Core.Rendering; using DiscordChatExporter.Core.Rendering;
using DiscordChatExporter.Core.Services.Logic;
using Tyrrrz.Extensions; using Tyrrrz.Extensions;
namespace DiscordChatExporter.Core.Services namespace DiscordChatExporter.Core.Services
{ {
public class ExportService public partial class ExportService
{ {
private readonly SettingsService _settingsService; private readonly SettingsService _settingsService;
private readonly DataService _dataService;
public ExportService(SettingsService settingsService) public ExportService(SettingsService settingsService, DataService dataService)
{ {
_settingsService = settingsService; _settingsService = settingsService;
_dataService = dataService;
} }
private IChatLogRenderer CreateRenderer(ChatLog chatLog, ExportFormat format) public async Task ExportChatLogAsync(AuthToken token, Guild guild, Channel channel,
string outputPath, ExportFormat format, int? partitionLimit,
DateTimeOffset? after = null, DateTimeOffset? before = null, IProgress<double>? progress = null)
{ {
if (format == ExportFormat.PlainText) // Get base file path from output path
return new PlainTextChatLogRenderer(chatLog, _settingsService.DateFormat); var baseFilePath = GetFilePathFromOutputPath(outputPath, format, guild, channel, after, before);
if (format == ExportFormat.HtmlDark) // Create options
return new HtmlChatLogRenderer(chatLog, "Dark", _settingsService.DateFormat); var options = new RenderOptions(baseFilePath, format, partitionLimit);
if (format == ExportFormat.HtmlLight) // Create context
return new HtmlChatLogRenderer(chatLog, "Light", _settingsService.DateFormat); var mentionableUsers = new HashSet<User>(IdBasedEqualityComparer.Instance);
var mentionableChannels = await _dataService.GetGuildChannelsAsync(token, guild.Id);
var mentionableRoles = await _dataService.GetGuildRolesAsync(token, guild.Id);
if (format == ExportFormat.Csv) var context = new RenderContext
return new CsvChatLogRenderer(chatLog, _settingsService.DateFormat); (
guild, channel, after, before, _settingsService.DateFormat,
mentionableUsers, mentionableChannels, mentionableRoles
);
throw new ArgumentOutOfRangeException(nameof(format), $"Unknown format [{format}]."); // Create renderer
} await using var renderer = new MessageRenderer(options, context);
private async Task ExportChatLogAsync(ChatLog chatLog, string filePath, ExportFormat format) // Render messages
{ var renderedAnything = false;
// Create output directory await foreach (var message in _dataService.GetMessagesAsync(token, channel.Id, after, before, progress))
var dirPath = Path.GetDirectoryName(filePath);
if (!dirPath.IsNullOrWhiteSpace())
Directory.CreateDirectory(dirPath);
// Render chat log to output file
using (var writer = File.CreateText(filePath))
await CreateRenderer(chatLog, format).RenderAsync(writer);
}
public async Task ExportChatLogAsync(ChatLog chatLog, string filePath, ExportFormat format, int? partitionLimit)
{
// If partitioning is disabled or there are fewer messages in chat log than the limit - process it without partitioning
if (partitionLimit == null || partitionLimit <= 0 || chatLog.Messages.Count <= partitionLimit)
{ {
await ExportChatLogAsync(chatLog, filePath, format); // Add encountered users to the list of mentionable users
mentionableUsers.Add(message.Author);
mentionableUsers.AddRange(message.MentionedUsers);
// Render message
await renderer.RenderMessageAsync(message);
renderedAnything = true;
} }
// Otherwise split into partitions and export separately
else // Throw if no messages were rendered
if (!renderedAnything)
throw new DomainException($"Channel [{channel.Name}] contains no messages for specified period");
}
}
public partial class ExportService
{
private static string GetFilePathFromOutputPath(string outputPath, ExportFormat format, Guild guild, Channel channel,
DateTimeOffset? after, DateTimeOffset? before)
{
// Output is a directory
if (Directory.Exists(outputPath) || string.IsNullOrWhiteSpace(Path.GetExtension(outputPath)))
{ {
// Create partitions by grouping up to X contiguous messages into separate chat logs var fileName = ExportLogic.GetDefaultExportFileName(format, guild, channel, after, before);
var partitions = chatLog.Messages.GroupContiguous(g => g.Count < partitionLimit.Value) return Path.Combine(outputPath, fileName);
.Select(g => new ChatLog(chatLog.Guild, chatLog.Channel, chatLog.After, chatLog.Before, g, chatLog.Mentionables))
.ToArray();
// Split file path into components
var dirPath = Path.GetDirectoryName(filePath);
var fileNameWithoutExt = Path.GetFileNameWithoutExtension(filePath);
var fileExt = Path.GetExtension(filePath);
// Export each partition separately
var partitionNumber = 1;
foreach (var partition in partitions)
{
// Compose new file name
var partitionFilePath = $"{fileNameWithoutExt} [{partitionNumber} of {partitions.Length}]{fileExt}";
// Compose full file path
if (!dirPath.IsNullOrWhiteSpace())
partitionFilePath = Path.Combine(dirPath, partitionFilePath);
// Export
await ExportChatLogAsync(partition, partitionFilePath, format);
// Increment partition number
partitionNumber++;
}
} }
// Output is a file
return outputPath;
} }
} }
} }
@@ -0,0 +1,22 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace DiscordChatExporter.Core.Services
{
public static class Extensions
{
private static async ValueTask<IReadOnlyList<T>> AggregateAsync<T>(this IAsyncEnumerable<T> asyncEnumerable)
{
var list = new List<T>();
await foreach (var i in asyncEnumerable)
list.Add(i);
return list;
}
public static ValueTaskAwaiter<IReadOnlyList<T>> GetAwaiter<T>(this IAsyncEnumerable<T> asyncEnumerable) =>
asyncEnumerable.AggregateAsync().GetAwaiter();
}
}
@@ -1,59 +0,0 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using DiscordChatExporter.Core.Models;
using Tyrrrz.Extensions;
namespace DiscordChatExporter.Core.Services.Helpers
{
public static class ExportHelper
{
public static bool IsDirectoryPath(string path) =>
path.Last() == Path.DirectorySeparatorChar ||
path.Last() == Path.AltDirectorySeparatorChar ||
(Path.GetExtension(path).IsNullOrWhiteSpace() && !File.Exists(path));
public static string GetDefaultExportFileName(ExportFormat format, Guild guild, Channel channel,
DateTimeOffset? after = null, DateTimeOffset? before = null)
{
var result = new StringBuilder();
// Append guild and channel names
result.Append($"{guild.Name} - {channel.Name} [{channel.Id}]");
// Append date range
if (after != null || before != null)
{
result.Append(" (");
// Both 'after' and 'before' are set
if (after != null && before != null)
{
result.Append($"{after:yyyy-MM-dd} to {before:yyyy-MM-dd}");
}
// Only 'after' is set
else if (after != null)
{
result.Append($"after {after:yyyy-MM-dd}");
}
// Only 'before' is set
else
{
result.Append($"before {before:yyyy-MM-dd}");
}
result.Append(")");
}
// Append extension
result.Append($".{format.GetFileExtension()}");
// Replace invalid chars
foreach (var invalidChar in Path.GetInvalidFileNameChars())
result.Replace(invalidChar, '_');
return result.ToString();
}
}
}
@@ -0,0 +1,53 @@
using System;
using System.IO;
using System.Text;
using DiscordChatExporter.Core.Models;
namespace DiscordChatExporter.Core.Services.Logic
{
public static class ExportLogic
{
public static string GetDefaultExportFileName(ExportFormat format,
Guild guild, Channel channel,
DateTimeOffset? after = null, DateTimeOffset? before = null)
{
var buffer = new StringBuilder();
// Append guild and channel names
buffer.Append($"{guild.Name} - {channel.Name} [{channel.Id}]");
// Append date range
if (after != null || before != null)
{
buffer.Append(" (");
// Both 'after' and 'before' are set
if (after != null && before != null)
{
buffer.Append($"{after:yyyy-MM-dd} to {before:yyyy-MM-dd}");
}
// Only 'after' is set
else if (after != null)
{
buffer.Append($"after {after:yyyy-MM-dd}");
}
// Only 'before' is set
else
{
buffer.Append($"before {before:yyyy-MM-dd}");
}
buffer.Append(")");
}
// Append extension
buffer.Append($".{format.GetFileExtension()}");
// Replace invalid chars
foreach (var invalidChar in Path.GetInvalidFileNameChars())
buffer.Replace(invalidChar, '_');
return buffer.ToString();
}
}
}
@@ -5,12 +5,16 @@ namespace DiscordChatExporter.Core.Services
{ {
public class SettingsService : SettingsManager public class SettingsService : SettingsManager
{ {
public bool IsAutoUpdateEnabled { get; set; } = true;
public string DateFormat { get; set; } = "dd-MMM-yy hh:mm tt"; public string DateFormat { get; set; } = "dd-MMM-yy hh:mm tt";
public AuthToken LastToken { get; set; } public bool IsAutoUpdateEnabled { get; set; } = true;
public bool IsTokenPersisted { get; set; } = true;
public AuthToken? LastToken { get; set; }
public ExportFormat LastExportFormat { get; set; } = ExportFormat.HtmlDark; public ExportFormat LastExportFormat { get; set; } = ExportFormat.HtmlDark;
public int? LastPartitionLimit { get; set; } public int? LastPartitionLimit { get; set; }
public SettingsService() public SettingsService()
@@ -19,5 +23,7 @@ namespace DiscordChatExporter.Core.Services
Configuration.SubDirectoryPath = ""; Configuration.SubDirectoryPath = "";
Configuration.FileName = "Settings.dat"; Configuration.FileName = "Settings.dat";
} }
public bool ShouldSerializeLastToken() => IsTokenPersisted;
} }
} }
+1 -1
View File
@@ -1,4 +1,4 @@
<Application <Application
x:Class="DiscordChatExporter.Gui.App" x:Class="DiscordChatExporter.Gui.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+11 -1
View File
@@ -1,6 +1,16 @@
namespace DiscordChatExporter.Gui using System;
using System.Reflection;
namespace DiscordChatExporter.Gui
{ {
public partial class App public partial class App
{ {
private static readonly Assembly Assembly = typeof(App).Assembly;
public static string Name => Assembly.GetName().Name!;
public static Version Version => Assembly.GetName().Version!;
public static string VersionString => Version.ToString(3);
} }
} }
@@ -3,7 +3,7 @@ using System.Collections.Specialized;
using System.Linq; using System.Linq;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Interactivity; using Microsoft.Xaml.Behaviors;
namespace DiscordChatExporter.Gui.Behaviors namespace DiscordChatExporter.Gui.Behaviors
{ {
+6 -3
View File
@@ -1,11 +1,14 @@
using System.Windows; using DiscordChatExporter.Core.Services;
using System.Windows.Threading;
using DiscordChatExporter.Core.Services;
using DiscordChatExporter.Gui.ViewModels; using DiscordChatExporter.Gui.ViewModels;
using DiscordChatExporter.Gui.ViewModels.Framework; using DiscordChatExporter.Gui.ViewModels.Framework;
using Stylet; using Stylet;
using StyletIoC; using StyletIoC;
#if !DEBUG
using System.Windows;
using System.Windows.Threading;
#endif
namespace DiscordChatExporter.Gui namespace DiscordChatExporter.Gui
{ {
public class Bootstrapper : Bootstrapper<RootViewModel> public class Bootstrapper : Bootstrapper<RootViewModel>
@@ -4,25 +4,25 @@ using System.Windows.Data;
namespace DiscordChatExporter.Gui.Converters namespace DiscordChatExporter.Gui.Converters
{ {
[ValueConversion(typeof(DateTimeOffset), typeof(DateTime))] [ValueConversion(typeof(DateTimeOffset?), typeof(DateTime?))]
public class DateTimeOffsetToDateTimeConverter : IValueConverter public class DateTimeOffsetToDateTimeConverter : IValueConverter
{ {
public static DateTimeOffsetToDateTimeConverter Instance { get; } = new DateTimeOffsetToDateTimeConverter(); public static DateTimeOffsetToDateTimeConverter Instance { get; } = new DateTimeOffsetToDateTimeConverter();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) public object? Convert(object value, Type targetType, object parameter, CultureInfo culture)
{ {
if (value is DateTimeOffset dateTimeOffsetValue) if (value is DateTimeOffset dateTimeOffsetValue)
return dateTimeOffsetValue.DateTime; return dateTimeOffsetValue.DateTime;
return default; return default(DateTime?);
} }
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) public object? ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{ {
if (value is DateTime dateTimeValue) if (value is DateTime dateTimeValue)
return new DateTimeOffset(dateTimeValue); return new DateTimeOffset(dateTimeValue);
return default; return default(DateTimeOffset?);
} }
} }
} }
@@ -10,12 +10,12 @@ namespace DiscordChatExporter.Gui.Converters
{ {
public static ExportFormatToStringConverter Instance { get; } = new ExportFormatToStringConverter(); public static ExportFormatToStringConverter Instance { get; } = new ExportFormatToStringConverter();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) public object? Convert(object value, Type targetType, object parameter, CultureInfo culture)
{ {
if (value is ExportFormat exportFormatValue) if (value is ExportFormat exportFormatValue)
return exportFormatValue.GetDisplayName(); return exportFormatValue.GetDisplayName();
return default; return default(string);
} }
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
@@ -14,7 +14,7 @@ namespace DiscordChatExporter.Gui.Converters
if (value is bool boolValue) if (value is bool boolValue)
return !boolValue; return !boolValue;
return default; return default(bool);
} }
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
@@ -22,7 +22,7 @@ namespace DiscordChatExporter.Gui.Converters
if (value is bool boolValue) if (value is bool boolValue)
return !boolValue; return !boolValue;
return default; return default(bool);
} }
} }
} }
@@ -1,158 +1,38 @@
<?xml version="1.0" encoding="utf-8"?> <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Import Project="../DiscordChatExporter.props" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{732A67AF-93DE-49DF-B10F-FD74710B7863}</ProjectGuid>
<OutputType>WinExe</OutputType> <OutputType>WinExe</OutputType>
<RootNamespace>DiscordChatExporter.Gui</RootNamespace>
<AssemblyName>DiscordChatExporter</AssemblyName> <AssemblyName>DiscordChatExporter</AssemblyName>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion> <UseWPF>true</UseWPF>
<FileAlignment>512</FileAlignment> <ApplicationIcon>../favicon.ico</ApplicationIcon>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<LangVersion>latest</LangVersion>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>..\favicon.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Resource Include="../favicon.ico" />
<Reference Include="System.Core" />
<Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="App.xaml.cs"> <PackageReference Include="Gress" Version="1.1.1" />
<DependentUpon>App.xaml</DependentUpon> <PackageReference Include="MaterialDesignColors" Version="1.2.2" />
</Compile> <PackageReference Include="MaterialDesignThemes" Version="3.0.1" />
<Compile Include="Behaviors\ChannelViewModelMultiSelectionListBoxBehavior.cs" /> <PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.3" />
<Compile Include="Behaviors\MultiSelectionListBoxBehavior.cs" /> <PackageReference Include="Ookii.Dialogs.Wpf" Version="1.1.0" />
<Compile Include="Bootstrapper.cs" /> <PackageReference Include="Stylet" Version="1.3.0" />
<Compile Include="Converters\DateTimeOffsetToDateTimeConverter.cs" /> <PackageReference Include="Tyrrrz.Extensions" Version="1.6.5" />
<Compile Include="Converters\ExportFormatToStringConverter.cs" /> <PackageReference Include="PropertyChanged.Fody" Version="3.2.3" PrivateAssets="all" />
<Compile Include="Converters\InverseBoolConverter.cs" />
<Compile Include="Services\UpdateService.cs" />
<Compile Include="ViewModels\Components\ChannelViewModel.cs" />
<Compile Include="ViewModels\Components\GuildViewModel.cs" />
<Compile Include="ViewModels\Dialogs\ExportSetupViewModel.cs" />
<Compile Include="ViewModels\Framework\DialogManager.cs" />
<Compile Include="ViewModels\Framework\DialogScreen.cs" />
<Compile Include="ViewModels\Framework\Extensions.cs" />
<Compile Include="ViewModels\Framework\IViewModelFactory.cs" />
<Compile Include="ViewModels\Dialogs\SettingsViewModel.cs" />
<Compile Include="ViewModels\RootViewModel.cs" />
<Compile Include="Views\Dialogs\ExportSetupView.xaml.cs">
<DependentUpon>ExportSetupView.xaml</DependentUpon>
</Compile>
<Compile Include="Views\RootView.xaml.cs">
<DependentUpon>RootView.xaml</DependentUpon>
</Compile>
<Compile Include="Views\Dialogs\SettingsView.xaml.cs">
<DependentUpon>SettingsView.xaml</DependentUpon>
</Compile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs"> <ProjectReference Include="..\DiscordChatExporter.Core.Markdown\DiscordChatExporter.Core.Markdown.csproj" />
<SubType>Code</SubType> <ProjectReference Include="..\DiscordChatExporter.Core.Models\DiscordChatExporter.Core.Models.csproj" />
</Compile> <ProjectReference Include="..\DiscordChatExporter.Core.Rendering\DiscordChatExporter.Core.Rendering.csproj" />
<Compile Include="Properties\Resources.Designer.cs"> <ProjectReference Include="..\DiscordChatExporter.Core.Services\DiscordChatExporter.Core.Services.csproj" />
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Resource Include="..\favicon.ico" /> <Target Name="Format XAML" AfterTargets="BeforeBuild">
</ItemGroup> <Exec Command="dotnet tool restore" />
<ItemGroup> <Exec Command="dotnet xstyler -r -d &quot;$(MSBuildProjectDirectory)&quot;" />
<ProjectReference Include="..\DiscordChatExporter.Core.Models\DiscordChatExporter.Core.Models.csproj"> </Target>
<Project>{67a9d184-4656-4ce1-9d75-bddcbcafb200}</Project>
<Name>DiscordChatExporter.Core.Models</Name>
</ProjectReference>
<ProjectReference Include="..\DiscordChatExporter.Core.Services\DiscordChatExporter.Core.Services.csproj">
<Project>{707c0cd0-a7e0-4cab-8db9-07a45cb87377}</Project>
<Name>DiscordChatExporter.Core.Services</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Page Include="Views\Dialogs\ExportSetupView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\RootView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\Dialogs\SettingsView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Gress">
<Version>1.1.1</Version>
</PackageReference>
<PackageReference Include="MaterialDesignColors">
<Version>1.2.0</Version>
</PackageReference>
<PackageReference Include="MaterialDesignThemes">
<Version>2.6.0</Version>
</PackageReference>
<PackageReference Include="Ookii.Dialogs.Wpf">
<Version>1.1.0</Version>
</PackageReference>
<PackageReference Include="PropertyChanged.Fody">
<Version>2.6.1</Version>
</PackageReference>
<PackageReference Include="Stylet">
<Version>1.2.0</Version>
</PackageReference>
<PackageReference Include="System.Windows.Interactivity.WPF">
<Version>2.0.20525</Version>
</PackageReference>
<PackageReference Include="Tyrrrz.Extensions">
<Version>1.6.2</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>
@@ -1,7 +0,0 @@
using System.Reflection;
[assembly: AssemblyTitle("DiscordChatExporter")]
[assembly: AssemblyCompany("Tyrrrz")]
[assembly: AssemblyCopyright("Copyright (c) Alexey Golub")]
[assembly: AssemblyVersion("2.15")]
[assembly: AssemblyFileVersion("2.15")]
-63
View File
@@ -1,63 +0,0 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace DiscordChatExporter.Gui.Properties {
using System;
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources() {
}
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("DiscordChatExporter.Gui.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
}
}
@@ -1,117 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

Some files were not shown because too many files have changed in this diff Show More