mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-07-07 22:54:38 +02:00
Compare commits
101 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| eececb6701 | |||
| d3adf19500 | |||
| a01835efb6 | |||
| 1d39fe9c53 | |||
| 9f6090b3af | |||
| 9fa40dca00 | |||
| 8a4f306012 | |||
| 37be80e9e1 | |||
| 1475ffaf0e | |||
| aa50dd6dc7 | |||
| 539f57f455 | |||
| e1f83997aa | |||
| 26d713a17c | |||
| 5c2e725739 | |||
| bf56902134 | |||
| b4df267372 | |||
| 4ebc681327 | |||
| 5cb6adf19e | |||
| cec8409ac4 | |||
| 3af9421304 | |||
| cc6f930dc6 | |||
| e6aac3cdb9 | |||
| 8f1aec241b | |||
| 41c828eb3e | |||
| 964317dac0 | |||
| 2a223599f9 | |||
| fc38afe6a0 | |||
| 23512dae64 | |||
| 8382ca725d | |||
| 457e14d0b6 | |||
| b830014a46 | |||
| 35e1114318 | |||
| 610bf4baa3 | |||
| d84222ea09 | |||
| cc1bb924e7 | |||
| 0d2ae8b5db | |||
| 632b9f953b | |||
| 9a52c6a6f6 | |||
| fc8c1c5914 | |||
| 279a6aee84 | |||
| 73ab983056 | |||
| e5a2852165 | |||
| 1bf9d9e2e2 | |||
| 9299b95250 | |||
| 5e0a2c6761 | |||
| ccbd11e526 | |||
| 2c36f6a826 | |||
| d6980cef8d | |||
| fa4bb3b693 | |||
| 05c551f80c | |||
| 69cfe1845b | |||
| 3a0d351143 | |||
| f04719c4bd | |||
| d88cd9b228 | |||
| cd042e5368 | |||
| 533671c59f | |||
| 29c35f6754 | |||
| 7b32101517 | |||
| 9bb88128b0 | |||
| 12a5091d73 | |||
| 82af36c90d | |||
| a2beb9836b | |||
| 60f46bad29 | |||
| 2cdb230b1e | |||
| 9241b7d2d1 | |||
| 40f54d9a98 | |||
| f2faf823b9 | |||
| 5b9eaa57f5 | |||
| 58ba99e0c6 | |||
| 117d1b7e4a | |||
| 835910ee22 | |||
| 5630020d88 | |||
| 71e6a8de7a | |||
| 592b8b6294 | |||
| e6edcd27a4 | |||
| 557b5be844 | |||
| 02fe2a46e8 | |||
| 76c7f9b419 | |||
| 9a383d2bd4 | |||
| 2ed374ee5c | |||
| 0ced31ca4a | |||
| 3bb5638b56 | |||
| 3b5d752c95 | |||
| 2b1f599cbe | |||
| 7cb1c21897 | |||
| 37bcea86e3 | |||
| 559ef71d2f | |||
| ab0ce478db | |||
| afb9f52221 | |||
| d65ca409b1 | |||
| 7784383ea7 | |||
| 26322da800 | |||
| 1325edb135 | |||
| dec2c26d83 | |||
| 82b0b8cb0a | |||
| a7da90943f | |||
| 1edeaf9d3a | |||
| 9c043376f9 | |||
| 77366cc9b4 | |||
| ffdca7b799 | |||
| 30cba7959f |
@@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"version": 1,
|
||||||
|
"isRoot": true,
|
||||||
|
"tools": {
|
||||||
|
"xamlstyler.console": {
|
||||||
|
"version": "3.2001.0",
|
||||||
|
"commands": [
|
||||||
|
"xstyler"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
github: Tyrrrz
|
||||||
|
patreon: Tyrrrz
|
||||||
|
custom: ['buymeacoffee.com/Tyrrrz', 'tyrrrz.me/donate']
|
||||||
@@ -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
|
||||||
@@ -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/
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 51 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 105 KiB |
@@ -1,3 +1,70 @@
|
|||||||
|
### v2.18 (04-Feb-2020)
|
||||||
|
|
||||||
|
- Added JSON export format. It's a structured data format which is easy to parse. If you're using DiscordChatExporter to export chat logs for further ingestion by another tool, this is most likely the format you will want to use.
|
||||||
|
- [GUI] You can now quickly jump to a specific channel in a list. For example, if you want to jump to a channel named "General", you can simply press the 'g' key in the channel list view. You can also press 'g', 'e' and 'n' in quick succession which will jump to the next channel that starts with "gen". This mechanic is consistent with other Windows applications.
|
||||||
|
- [CLI] Improved help text screen.
|
||||||
|
|
||||||
|
### 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)
|
||||||
|
|
||||||
|
- Improved markdown parser and made it even faster for non-HTML formats.
|
||||||
|
- [HTML] Added support for block quotes.
|
||||||
|
- [HTML] Links pointing to a Discord message will now navigate to the linked message inside exported chat log if it's there.
|
||||||
|
- [HTML] Updated light theme to match how it looks in Discord after recent changes.
|
||||||
|
- [HTML] Added indication for when a message is pinned. Pinned messages now have a tinted background.
|
||||||
|
- [HTML] Fixed an issue where multiline code blocks sometimes had incorrect formatting applied to them.
|
||||||
|
- [TXT] Added indication for when a message is pinned. Pinned messages now have `(pinned)` next to timestamp and author.
|
||||||
|
- [CSV] Added message author's user ID to output.
|
||||||
|
- [GUI] Streamlined auto-update process a bit.
|
||||||
|
- [GUI] Added some tooltips.
|
||||||
|
|
||||||
|
### v2.14 (15-Jun-2019)
|
||||||
|
|
||||||
|
- [TXT] Added support for embeds.
|
||||||
|
- [TXT] Added support for reactions.
|
||||||
|
- [CSV] Added support for reactions.
|
||||||
|
- [TXT] Changed how attachments are rendered.
|
||||||
|
|
||||||
|
### v2.13.1 (06-Jun-2019)
|
||||||
|
|
||||||
|
- Fixed an issue where the app sometimes crashed when exporting due to `System.InvalidCastException`.
|
||||||
|
- [HTML] Added `data-user-id` attribute to `span.chatlog__author-name`. The value of this attribute is author's Discord user ID.
|
||||||
|
|
||||||
|
### v2.13 (15-May-2019)
|
||||||
|
|
||||||
|
- Updated usage instructions.
|
||||||
|
- [HTML] Added BOT tags to messages posted by bots.
|
||||||
|
- [GUI] UI will now be disabled when exporting.
|
||||||
|
- [GUI] Notification informing that the export has completed will now appear after all exports have completed, instead of each of them separately.
|
||||||
|
|
||||||
|
### v2.12.1 (12-Apr-2019)
|
||||||
|
|
||||||
|
- [GUI] Fixed an issue where the app crashed when trying to export multiple channels.
|
||||||
|
- [HTML] Fixed an issue where some of the emojis were rendered via Twemoji while Discord renders them as plain text.
|
||||||
|
- [HTML] Fixed an issue where emojis weren't jumbo if there were more than one of them.
|
||||||
|
|
||||||
### v2.12 (11-Apr-2019)
|
### v2.12 (11-Apr-2019)
|
||||||
|
|
||||||
- Improved markdown parsing performance which speeds up the final stage of exporting by around 4.5 times.
|
- Improved markdown parsing performance which speeds up the final stage of exporting by around 4.5 times.
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
|
|
||||||
<metadata>
|
|
||||||
<id>discordchatexporter</id>
|
|
||||||
<title>DiscordChatExporter</title>
|
|
||||||
<version>2.12</version>
|
|
||||||
<owners>Tyrrrz</owners>
|
|
||||||
<authors>Tyrrrz</authors>
|
|
||||||
<copyright>Copyright (C) Alexey Golub</copyright>
|
|
||||||
<summary>Exports Discord chat logs to a file</summary>
|
|
||||||
<description>DiscordChatExporter can be used to export message history from a Discord channel to a file. It works for both direct message chats and guild chats, supports markdown, message grouping, and attachments. The tool also lets you select from/to dates to limit the exported messages. There are options to configure the output, such as date format, color theme, message grouping limit, etc.</description>
|
|
||||||
<tags>discordchatexporter discord export chat logs archive gui</tags>
|
|
||||||
<projectSourceUrl>https://github.com/Tyrrrz/DiscordChatExporter</projectSourceUrl>
|
|
||||||
<packageSourceUrl>https://github.com/Tyrrrz/DiscordChatExporter</packageSourceUrl>
|
|
||||||
<projectUrl>http://tyrrrz.me/Projects/DiscordChatExporter</projectUrl>
|
|
||||||
<releaseNotes>https://github.com/Tyrrrz/DiscordChatExporter/blob/master/Changelog.md</releaseNotes>
|
|
||||||
<iconUrl>https://raw.githubusercontent.com/Tyrrrz/DiscordChatExporter/master/favicon.png</iconUrl>
|
|
||||||
<licenseUrl>https://raw.githubusercontent.com/Tyrrrz/DiscordChatExporter/master/License.txt</licenseUrl>
|
|
||||||
<requireLicenseAcceptance>true</requireLicenseAcceptance>
|
|
||||||
<bugTrackerUrl>https://github.com/Tyrrrz/DiscordChatExporter/issues</bugTrackerUrl>
|
|
||||||
</metadata>
|
|
||||||
<files>
|
|
||||||
<file src="tools\**" target="tools" />
|
|
||||||
</files>
|
|
||||||
</package>
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
$ErrorActionPreference = 'Stop'
|
|
||||||
$packageName = $env:ChocolateyPackageName
|
|
||||||
$installDirPath = "$(Split-Path -parent $MyInvocation.MyCommand.Definition)"
|
|
||||||
|
|
||||||
# Install package (GUI)
|
|
||||||
|
|
||||||
$packageArgs = @{
|
|
||||||
packageName = $packageName
|
|
||||||
unzipLocation = $installDirPath
|
|
||||||
url = 'https://github.com/Tyrrrz/DiscordChatExporter/releases/download/2.12/DiscordChatExporter.zip'
|
|
||||||
}
|
|
||||||
Install-ChocolateyZipPackage @packageArgs
|
|
||||||
|
|
||||||
# Mark the executable as GUI
|
|
||||||
New-Item (Join-Path $installDirPath "DiscordChatExporter.exe.gui") -ItemType File -Force
|
|
||||||
|
|
||||||
# Install package (CLI)
|
|
||||||
|
|
||||||
$packageArgs = @{
|
|
||||||
packageName = $packageName
|
|
||||||
unzipLocation = $installDirPath
|
|
||||||
url = 'https://github.com/Tyrrrz/DiscordChatExporter/releases/download/2.12/DiscordChatExporter.CLI.zip'
|
|
||||||
}
|
|
||||||
Install-ChocolateyZipPackage @packageArgs
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
# --- PORTABLE / 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
|
|
||||||
New-Item "$PSScriptRoot\Portable\GUI\bin" -ItemType Directory -Force
|
|
||||||
$files | Compress-Archive -DestinationPath "$PSScriptRoot\Portable\GUI\bin\DiscordChatExporter.zip" -Force
|
|
||||||
|
|
||||||
# --- PORTABLE / 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
|
|
||||||
New-Item "$PSScriptRoot\Portable\CLI\bin" -ItemType Directory -Force
|
|
||||||
$files | Compress-Archive -DestinationPath "$PSScriptRoot\Portable\CLI\bin\DiscordChatExporter.CLI.zip" -Force
|
|
||||||
|
|
||||||
# --- CHOCOLATEY ---
|
|
||||||
|
|
||||||
# Create package
|
|
||||||
New-Item "$PSScriptRoot\Choco\bin\" -ItemType Directory -Force
|
|
||||||
choco pack $PSScriptRoot\Choco\discordchatexporter.nuspec --out $PSScriptRoot\Choco\bin\
|
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
using DiscordChatExporter.Core.Services;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Commands
|
||||||
|
{
|
||||||
|
[Command("export", Description = "Export a channel.")]
|
||||||
|
public class ExportChannelCommand : ExportCommandBase
|
||||||
|
{
|
||||||
|
[CommandOption("channel", 'c', IsRequired = true, Description = "Channel ID.")]
|
||||||
|
public string ChannelId { get; set; } = "";
|
||||||
|
|
||||||
|
public ExportChannelCommand(SettingsService settingsService, DataService dataService, ExportService exportService)
|
||||||
|
: base(settingsService, dataService, exportService)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async ValueTask ExecuteAsync(IConsole console) => await ExportAsync(console, ChannelId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
using CliFx.Utilities;
|
||||||
|
using DiscordChatExporter.Core.Models;
|
||||||
|
using DiscordChatExporter.Core.Services;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Commands
|
||||||
|
{
|
||||||
|
public abstract class ExportCommandBase : TokenCommandBase
|
||||||
|
{
|
||||||
|
protected SettingsService SettingsService { get; }
|
||||||
|
|
||||||
|
protected ExportService ExportService { get; }
|
||||||
|
|
||||||
|
[CommandOption("format", 'f', Description = "Output file format.")]
|
||||||
|
public ExportFormat ExportFormat { get; set; } = ExportFormat.HtmlDark;
|
||||||
|
|
||||||
|
[CommandOption("output", 'o', Description = "Output file or directory path.")]
|
||||||
|
public string OutputPath { get; set; } = Directory.GetCurrentDirectory();
|
||||||
|
|
||||||
|
[CommandOption("after", Description = "Limit to messages sent after this date.")]
|
||||||
|
public DateTimeOffset? After { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("before", Description = "Limit to messages sent before this date.")]
|
||||||
|
public DateTimeOffset? Before { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("partition", 'p', Description = "Split output into partitions limited to this number of messages.")]
|
||||||
|
public int? PartitionLimit { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("dateformat", Description = "Date format used in output.")]
|
||||||
|
public string? DateFormat { get; set; }
|
||||||
|
|
||||||
|
protected ExportCommandBase(SettingsService settingsService, DataService dataService, ExportService exportService)
|
||||||
|
: base(dataService)
|
||||||
|
{
|
||||||
|
SettingsService = settingsService;
|
||||||
|
ExportService = exportService;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async ValueTask ExportAsync(IConsole console, Guild guild, Channel channel)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrWhiteSpace(DateFormat))
|
||||||
|
SettingsService.DateFormat = DateFormat;
|
||||||
|
|
||||||
|
console.Output.Write($"Exporting channel [{channel.Name}]... ");
|
||||||
|
var progress = console.CreateProgressTicker();
|
||||||
|
|
||||||
|
await ExportService.ExportChatLogAsync(Token, guild, channel,
|
||||||
|
OutputPath, ExportFormat, PartitionLimit,
|
||||||
|
After, Before, progress);
|
||||||
|
|
||||||
|
console.Output.WriteLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async ValueTask ExportAsync(IConsole console, Channel channel)
|
||||||
|
{
|
||||||
|
var guild = await DataService.GetGuildAsync(Token, channel.GuildId);
|
||||||
|
await ExportAsync(console, guild, channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async ValueTask ExportAsync(IConsole console, string channelId)
|
||||||
|
{
|
||||||
|
var channel = await DataService.GetChannelAsync(Token, channelId);
|
||||||
|
await ExportAsync(console, channel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
using DiscordChatExporter.Core.Models.Exceptions;
|
||||||
|
using DiscordChatExporter.Core.Services;
|
||||||
|
using DiscordChatExporter.Core.Services.Exceptions;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Commands
|
||||||
|
{
|
||||||
|
[Command("exportdm", Description = "Export all direct message channels.")]
|
||||||
|
public class ExportDirectMessagesCommand : ExportCommandBase
|
||||||
|
{
|
||||||
|
public ExportDirectMessagesCommand(SettingsService settingsService, DataService dataService, ExportService exportService)
|
||||||
|
: base(settingsService, dataService, exportService)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
// Get channels
|
||||||
|
var channels = await DataService.GetDirectMessageChannelsAsync(Token);
|
||||||
|
|
||||||
|
// Order channels
|
||||||
|
channels = channels.OrderBy(c => c.Name).ToArray();
|
||||||
|
|
||||||
|
// Loop through channels
|
||||||
|
foreach (var channel in channels)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await ExportAsync(console, channel);
|
||||||
|
}
|
||||||
|
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden)
|
||||||
|
{
|
||||||
|
console.Error.WriteLine("You don't have access to this channel.");
|
||||||
|
}
|
||||||
|
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
|
||||||
|
{
|
||||||
|
console.Error.WriteLine("This channel doesn't exist.");
|
||||||
|
}
|
||||||
|
catch (DomainException ex)
|
||||||
|
{
|
||||||
|
console.Error.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
using DiscordChatExporter.Core.Models;
|
||||||
|
using DiscordChatExporter.Core.Models.Exceptions;
|
||||||
|
using DiscordChatExporter.Core.Services;
|
||||||
|
using DiscordChatExporter.Core.Services.Exceptions;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Commands
|
||||||
|
{
|
||||||
|
[Command("exportguild", Description = "Export all channels within specified guild.")]
|
||||||
|
public class ExportGuildCommand : ExportCommandBase
|
||||||
|
{
|
||||||
|
[CommandOption("guild", 'g', IsRequired = true, Description = "Guild ID.")]
|
||||||
|
public string GuildId { get; set; } = "";
|
||||||
|
|
||||||
|
public ExportGuildCommand(SettingsService settingsService, DataService dataService, ExportService exportService)
|
||||||
|
: base(settingsService, dataService, exportService)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
// Get channels
|
||||||
|
var channels = await DataService.GetGuildChannelsAsync(Token, GuildId);
|
||||||
|
|
||||||
|
// Filter and order channels
|
||||||
|
channels = channels.Where(c => c.Type.IsExportable()).OrderBy(c => c.Name).ToArray();
|
||||||
|
|
||||||
|
// Loop through channels
|
||||||
|
foreach (var channel in channels)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await ExportAsync(console, channel);
|
||||||
|
}
|
||||||
|
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden)
|
||||||
|
{
|
||||||
|
console.Error.WriteLine("You don't have access to this channel.");
|
||||||
|
}
|
||||||
|
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
|
||||||
|
{
|
||||||
|
console.Error.WriteLine("This channel doesn't exist.");
|
||||||
|
}
|
||||||
|
catch (DomainException ex)
|
||||||
|
{
|
||||||
|
console.Error.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
using DiscordChatExporter.Core.Models;
|
||||||
|
using DiscordChatExporter.Core.Services;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Commands
|
||||||
|
{
|
||||||
|
[Command("channels", Description = "Get the list of channels in specified guild.")]
|
||||||
|
public class GetChannelsCommand : TokenCommandBase
|
||||||
|
{
|
||||||
|
[CommandOption("guild", 'g', IsRequired = true, Description = "Guild ID.")]
|
||||||
|
public string GuildId { get; set; } = "";
|
||||||
|
|
||||||
|
public GetChannelsCommand(DataService dataService)
|
||||||
|
: base(dataService)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
// Get channels
|
||||||
|
var channels = await DataService.GetGuildChannelsAsync(Token, GuildId);
|
||||||
|
|
||||||
|
// Filter and order channels
|
||||||
|
channels = channels.Where(c => c.Type.IsExportable()).OrderBy(c => c.Name).ToArray();
|
||||||
|
|
||||||
|
// Print result
|
||||||
|
foreach (var channel in channels)
|
||||||
|
console.Output.WriteLine($"{channel.Id} | {channel.Name}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
using DiscordChatExporter.Core.Services;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Commands
|
||||||
|
{
|
||||||
|
[Command("dm", Description = "Get the list of direct message channels.")]
|
||||||
|
public class GetDirectMessageChannelsCommand : TokenCommandBase
|
||||||
|
{
|
||||||
|
public GetDirectMessageChannelsCommand(DataService dataService)
|
||||||
|
: base(dataService)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
// Get channels
|
||||||
|
var channels = await DataService.GetDirectMessageChannelsAsync(Token);
|
||||||
|
|
||||||
|
// Order channels
|
||||||
|
channels = channels.OrderBy(c => c.Name).ToArray();
|
||||||
|
|
||||||
|
// Print result
|
||||||
|
foreach (var channel in channels)
|
||||||
|
console.Output.WriteLine($"{channel.Id} | {channel.Name}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
using DiscordChatExporter.Core.Services;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Commands
|
||||||
|
{
|
||||||
|
[Command("guilds", Description = "Get the list of accessible guilds.")]
|
||||||
|
public class GetGuildsCommand : TokenCommandBase
|
||||||
|
{
|
||||||
|
public GetGuildsCommand(DataService dataService)
|
||||||
|
: base(dataService)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
// Get guilds
|
||||||
|
var guilds = await DataService.GetUserGuildsAsync(Token);
|
||||||
|
|
||||||
|
// Order guilds
|
||||||
|
guilds = guilds.OrderBy(g => g.Name).ToArray();
|
||||||
|
|
||||||
|
// Print result
|
||||||
|
foreach (var guild in guilds)
|
||||||
|
console.Output.WriteLine($"{guild.Id} | {guild.Name}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Commands
|
||||||
|
{
|
||||||
|
[Command("guide", Description = "Explains how to obtain token, guild or channel ID.")]
|
||||||
|
public class GuideCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
console.WithForegroundColor(ConsoleColor.White, () => console.Output.WriteLine("To get user token:"));
|
||||||
|
console.Output.WriteLine(" 1. Open Discord");
|
||||||
|
console.Output.WriteLine(" 2. Press Ctrl+Shift+I to show developer tools");
|
||||||
|
console.Output.WriteLine(" 3. Navigate to the Application tab");
|
||||||
|
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(" 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.WithForegroundColor(ConsoleColor.White, () => console.Output.WriteLine("To get bot token:"));
|
||||||
|
console.Output.WriteLine(" 1. Go to Discord developer portal");
|
||||||
|
console.Output.WriteLine(" 2. Open your application's settings");
|
||||||
|
console.Output.WriteLine(" 3. Navigate to the Bot section on the left");
|
||||||
|
console.Output.WriteLine(" 4. Under Token click Copy");
|
||||||
|
console.Output.WriteLine();
|
||||||
|
|
||||||
|
console.WithForegroundColor(ConsoleColor.White, () => console.Output.WriteLine("To get guild ID or guild channel ID:"));
|
||||||
|
console.Output.WriteLine(" 1. Open Discord");
|
||||||
|
console.Output.WriteLine(" 2. Open Settings");
|
||||||
|
console.Output.WriteLine(" 3. Go to Appearance section");
|
||||||
|
console.Output.WriteLine(" 4. Enable Developer Mode");
|
||||||
|
console.Output.WriteLine(" 5. Right click on the desired guild or channel and click Copy ID");
|
||||||
|
console.Output.WriteLine();
|
||||||
|
|
||||||
|
console.WithForegroundColor(ConsoleColor.White, () => console.Output.WriteLine("To get direct message channel ID:"));
|
||||||
|
console.Output.WriteLine(" 1. Open Discord");
|
||||||
|
console.Output.WriteLine(" 2. Open the desired direct message channel");
|
||||||
|
console.Output.WriteLine(" 3. Press Ctrl+Shift+I to show developer tools");
|
||||||
|
console.Output.WriteLine(" 4. Navigate to the Console tab");
|
||||||
|
console.Output.WriteLine(" 5. Type \"window.location.href\" and press Enter");
|
||||||
|
console.Output.WriteLine(" 6. Copy the first long sequence of numbers inside the URL");
|
||||||
|
console.Output.WriteLine();
|
||||||
|
|
||||||
|
console.Output.WriteLine("If you still have unanswered questions, check out the wiki:");
|
||||||
|
console.Output.WriteLine("https://github.com/Tyrrrz/DiscordChatExporter/wiki");
|
||||||
|
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
using DiscordChatExporter.Core.Models;
|
||||||
|
using DiscordChatExporter.Core.Services;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Cli.Commands
|
||||||
|
{
|
||||||
|
public abstract class TokenCommandBase : ICommand
|
||||||
|
{
|
||||||
|
protected DataService DataService { get; }
|
||||||
|
|
||||||
|
[CommandOption("token", 't', IsRequired = true, Description = "Authorization token.")]
|
||||||
|
public string TokenValue { get; set; } = "";
|
||||||
|
|
||||||
|
[CommandOption("bot", 'b', Description = "Whether this authorization token belongs to a bot.")]
|
||||||
|
public bool IsBotToken { get; set; }
|
||||||
|
|
||||||
|
protected AuthToken Token => new AuthToken(IsBotToken ? AuthTokenType.Bot : AuthTokenType.User, TokenValue);
|
||||||
|
|
||||||
|
protected TokenCommandBase(DataService dataService)
|
||||||
|
{
|
||||||
|
DataService = dataService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract ValueTask ExecuteAsync(IConsole console);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
using DiscordChatExporter.Core.Services;
|
|
||||||
using StyletIoC;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli
|
|
||||||
{
|
|
||||||
public static class Container
|
|
||||||
{
|
|
||||||
public static IContainer Instance { get; }
|
|
||||||
|
|
||||||
static Container()
|
|
||||||
{
|
|
||||||
var builder = new StyletIoCBuilder();
|
|
||||||
|
|
||||||
// Autobind the .Services assembly
|
|
||||||
builder.Autobind(typeof(DataService).Assembly);
|
|
||||||
|
|
||||||
// Bind settings as singleton
|
|
||||||
builder.Bind<SettingsService>().ToSelf().InSingletonScope();
|
|
||||||
|
|
||||||
// Set instance
|
|
||||||
Instance = builder.BuildContainer();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,19 +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.12</Version>
|
|
||||||
<Company>Tyrrrz</Company>
|
|
||||||
<Copyright>Copyright (c) Alexey Golub</Copyright>
|
|
||||||
<ApplicationIcon>..\favicon.ico</ApplicationIcon>
|
<ApplicationIcon>..\favicon.ico</ApplicationIcon>
|
||||||
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="CommandLineParser" Version="2.3.0" />
|
<PackageReference Include="CliFx" Version="1.0.0" />
|
||||||
<PackageReference Include="Stylet" Version="1.1.22" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.1" />
|
||||||
<PackageReference Include="Tyrrrz.Extensions" Version="1.6.0" />
|
<PackageReference Include="Tyrrrz.Extensions" Version="1.6.5" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -1,47 +0,0 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Internal
|
|
||||||
{
|
|
||||||
internal class InlineProgress : IProgress<double>, IDisposable
|
|
||||||
{
|
|
||||||
private readonly int _posX;
|
|
||||||
private readonly int _posY;
|
|
||||||
|
|
||||||
private bool _isCompleted;
|
|
||||||
|
|
||||||
public InlineProgress()
|
|
||||||
{
|
|
||||||
// If output is not redirected - save initial cursor position
|
|
||||||
if (!Console.IsOutputRedirected)
|
|
||||||
{
|
|
||||||
_posX = Console.CursorLeft;
|
|
||||||
_posY = Console.CursorTop;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Report(double progress)
|
|
||||||
{
|
|
||||||
// If output is not redirected - reset cursor position and write progress
|
|
||||||
if (!Console.IsOutputRedirected)
|
|
||||||
{
|
|
||||||
Console.SetCursorPosition(_posX, _posY);
|
|
||||||
Console.WriteLine($"{progress:P1}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ReportCompletion() => _isCompleted = true;
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
// If output is not redirected - reset cursor position
|
|
||||||
if (!Console.IsOutputRedirected)
|
|
||||||
Console.SetCursorPosition(_posX, _posY);
|
|
||||||
|
|
||||||
// Inform about completion
|
|
||||||
if (_isCompleted)
|
|
||||||
Console.WriteLine("Completed ✓");
|
|
||||||
else
|
|
||||||
Console.WriteLine("Failed X");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,72 +1,44 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Threading.Tasks;
|
||||||
using CommandLine;
|
using CliFx;
|
||||||
using DiscordChatExporter.Cli.Verbs;
|
using DiscordChatExporter.Cli.Commands;
|
||||||
using DiscordChatExporter.Cli.Verbs.Options;
|
using DiscordChatExporter.Core.Services;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli
|
namespace DiscordChatExporter.Cli
|
||||||
{
|
{
|
||||||
public static class Program
|
public static class Program
|
||||||
{
|
{
|
||||||
private static void PrintTokenHelp()
|
private static IServiceProvider ConfigureServices()
|
||||||
{
|
{
|
||||||
Console.WriteLine("# To get user token:");
|
var services = new ServiceCollection();
|
||||||
Console.WriteLine(" 1. Open Discord");
|
|
||||||
Console.WriteLine(" 2. Press Ctrl+Shift+I to show developer tools");
|
// Register services
|
||||||
Console.WriteLine(" 3. Press Ctrl+R to reload");
|
services.AddSingleton<DataService>();
|
||||||
Console.WriteLine(" 4. Navigate to the Application tab");
|
services.AddSingleton<ExportService>();
|
||||||
Console.WriteLine(" 5. Select \"Local Storage\" > \"https://discordapp.com\" on the left");
|
services.AddSingleton<SettingsService>();
|
||||||
Console.WriteLine(" 6. Find \"token\" under key and copy the value");
|
|
||||||
Console.WriteLine();
|
// Register commands
|
||||||
Console.WriteLine("# To get bot token:");
|
services.AddTransient<ExportChannelCommand>();
|
||||||
Console.WriteLine(" 1. Go to Discord developer portal");
|
services.AddTransient<ExportDirectMessagesCommand>();
|
||||||
Console.WriteLine(" 2. Open your application's settings");
|
services.AddTransient<ExportGuildCommand>();
|
||||||
Console.WriteLine(" 3. Navigate to the Bot section on the left");
|
services.AddTransient<GetChannelsCommand>();
|
||||||
Console.WriteLine(" 4. Under Token click Copy");
|
services.AddTransient<GetDirectMessageChannelsCommand>();
|
||||||
Console.WriteLine();
|
services.AddTransient<GetGuildsCommand>();
|
||||||
Console.WriteLine("# To get guild or channel ID:");
|
services.AddTransient<GuideCommand>();
|
||||||
Console.WriteLine(" 1. Open Discord");
|
|
||||||
Console.WriteLine(" 2. Open Settings");
|
return services.BuildServiceProvider();
|
||||||
Console.WriteLine(" 3. Go to Appearance section");
|
|
||||||
Console.WriteLine(" 4. Enable Developer Mode");
|
|
||||||
Console.WriteLine(" 5. Right click on the desired guild or channel and click Copy ID");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Main(string[] args)
|
public static async Task<int> Main(string[] args)
|
||||||
{
|
{
|
||||||
// Get all verb types
|
var serviceProvider = ConfigureServices();
|
||||||
var verbTypes = new[]
|
|
||||||
{
|
|
||||||
typeof(ExportChannelOptions),
|
|
||||||
typeof(ExportDirectMessagesOptions),
|
|
||||||
typeof(ExportGuildOptions),
|
|
||||||
typeof(GetChannelsOptions),
|
|
||||||
typeof(GetDirectMessageChannelsOptions),
|
|
||||||
typeof(GetGuildsOptions)
|
|
||||||
};
|
|
||||||
|
|
||||||
// Parse command line arguments
|
return await new CliApplicationBuilder()
|
||||||
var parsedArgs = Parser.Default.ParseArguments(args, verbTypes);
|
.AddCommandsFromThisAssembly()
|
||||||
|
.UseTypeActivator(serviceProvider.GetService)
|
||||||
// Execute commands
|
.Build()
|
||||||
parsedArgs.WithParsed<ExportChannelOptions>(o => new ExportChannelVerb(o).Execute());
|
.RunAsync(args);
|
||||||
parsedArgs.WithParsed<ExportDirectMessagesOptions>(o => new ExportDirectMessagesVerb(o).Execute());
|
|
||||||
parsedArgs.WithParsed<ExportGuildOptions>(o => new ExportGuildVerb(o).Execute());
|
|
||||||
parsedArgs.WithParsed<GetChannelsOptions>(o => new GetChannelsVerb(o).Execute());
|
|
||||||
parsedArgs.WithParsed<GetDirectMessageChannelsOptions>(o => new GetDirectMessageChannelsVerb(o).Execute());
|
|
||||||
parsedArgs.WithParsed<GetGuildsOptions>(o => new GetGuildsVerb(o).Execute());
|
|
||||||
|
|
||||||
// Show token help if help requested or no verb specified
|
|
||||||
parsedArgs.WithNotParsed(errs =>
|
|
||||||
{
|
|
||||||
var err = errs.First();
|
|
||||||
|
|
||||||
if (err.Tag == ErrorType.NoVerbSelectedError)
|
|
||||||
PrintTokenHelp();
|
|
||||||
|
|
||||||
if (err.Tag == ErrorType.HelpVerbRequestedError && args.Length == 1)
|
|
||||||
PrintTokenHelp();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using DiscordChatExporter.Cli.Internal;
|
|
||||||
using DiscordChatExporter.Cli.Verbs.Options;
|
|
||||||
using DiscordChatExporter.Core.Services;
|
|
||||||
using DiscordChatExporter.Core.Services.Helpers;
|
|
||||||
using Tyrrrz.Extensions;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Verbs
|
|
||||||
{
|
|
||||||
public class ExportChannelVerb : Verb<ExportChannelOptions>
|
|
||||||
{
|
|
||||||
public ExportChannelVerb(ExportChannelOptions options)
|
|
||||||
: base(options)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public override async Task ExecuteAsync()
|
|
||||||
{
|
|
||||||
// Get services
|
|
||||||
var settingsService = Container.Instance.Get<SettingsService>();
|
|
||||||
var dataService = Container.Instance.Get<DataService>();
|
|
||||||
var exportService = Container.Instance.Get<ExportService>();
|
|
||||||
|
|
||||||
// Configure settings
|
|
||||||
if (!Options.DateFormat.EmptyIfNull().IsWhiteSpace())
|
|
||||||
settingsService.DateFormat = Options.DateFormat;
|
|
||||||
|
|
||||||
// Track progress
|
|
||||||
Console.Write($"Exporting channel [{Options.ChannelId}]... ");
|
|
||||||
using (var progress = new InlineProgress())
|
|
||||||
{
|
|
||||||
// Get chat log
|
|
||||||
var chatLog = await dataService.GetChatLogAsync(Options.GetToken(), Options.ChannelId,
|
|
||||||
Options.After, Options.Before, progress);
|
|
||||||
|
|
||||||
// Generate file path if not set or is a directory
|
|
||||||
var filePath = Options.OutputPath;
|
|
||||||
if (filePath.EmptyIfNull().IsWhiteSpace() || ExportHelper.IsDirectoryPath(filePath))
|
|
||||||
{
|
|
||||||
// Generate default file name
|
|
||||||
var fileName = ExportHelper.GetDefaultExportFileName(Options.ExportFormat, chatLog.Guild,
|
|
||||||
chatLog.Channel, Options.After, Options.Before);
|
|
||||||
|
|
||||||
// Combine paths
|
|
||||||
filePath = Path.Combine(filePath ?? "", fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Export
|
|
||||||
await exportService.ExportChatLogAsync(chatLog, filePath, Options.ExportFormat, Options.PartitionLimit);
|
|
||||||
|
|
||||||
// Report successful completion
|
|
||||||
progress.ReportCompletion();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,78 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using DiscordChatExporter.Cli.Internal;
|
|
||||||
using DiscordChatExporter.Cli.Verbs.Options;
|
|
||||||
using DiscordChatExporter.Core.Services;
|
|
||||||
using DiscordChatExporter.Core.Services.Exceptions;
|
|
||||||
using DiscordChatExporter.Core.Services.Helpers;
|
|
||||||
using Tyrrrz.Extensions;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Verbs
|
|
||||||
{
|
|
||||||
public class ExportDirectMessagesVerb : Verb<ExportDirectMessagesOptions>
|
|
||||||
{
|
|
||||||
public ExportDirectMessagesVerb(ExportDirectMessagesOptions options)
|
|
||||||
: base(options)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public override async Task ExecuteAsync()
|
|
||||||
{
|
|
||||||
// Get services
|
|
||||||
var settingsService = Container.Instance.Get<SettingsService>();
|
|
||||||
var dataService = Container.Instance.Get<DataService>();
|
|
||||||
var exportService = Container.Instance.Get<ExportService>();
|
|
||||||
|
|
||||||
// Configure settings
|
|
||||||
if (!Options.DateFormat.EmptyIfNull().IsWhiteSpace())
|
|
||||||
settingsService.DateFormat = Options.DateFormat;
|
|
||||||
|
|
||||||
// Get channels
|
|
||||||
var channels = await dataService.GetDirectMessageChannelsAsync(Options.GetToken());
|
|
||||||
|
|
||||||
// Order channels
|
|
||||||
channels = channels.OrderBy(c => c.Name).ToArray();
|
|
||||||
|
|
||||||
// Loop through channels
|
|
||||||
foreach (var channel in channels)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// Track progress
|
|
||||||
Console.Write($"Exporting channel [{channel.Name}]... ");
|
|
||||||
using (var progress = new InlineProgress())
|
|
||||||
{
|
|
||||||
// Get chat log
|
|
||||||
var chatLog = await dataService.GetChatLogAsync(Options.GetToken(), channel,
|
|
||||||
Options.After, Options.Before, progress);
|
|
||||||
|
|
||||||
// Generate default file name
|
|
||||||
var fileName = ExportHelper.GetDefaultExportFileName(Options.ExportFormat, chatLog.Guild,
|
|
||||||
chatLog.Channel, Options.After, Options.Before);
|
|
||||||
|
|
||||||
// Generate file path
|
|
||||||
var filePath = Path.Combine(Options.OutputPath ?? "", fileName);
|
|
||||||
|
|
||||||
// Export
|
|
||||||
await exportService.ExportChatLogAsync(chatLog, filePath, Options.ExportFormat,
|
|
||||||
Options.PartitionLimit);
|
|
||||||
|
|
||||||
// Report successful completion
|
|
||||||
progress.ReportCompletion();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden)
|
|
||||||
{
|
|
||||||
Console.Error.WriteLine("You don't have access to this channel");
|
|
||||||
}
|
|
||||||
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
|
|
||||||
{
|
|
||||||
Console.Error.WriteLine("This channel doesn't exist");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,79 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using DiscordChatExporter.Cli.Internal;
|
|
||||||
using DiscordChatExporter.Cli.Verbs.Options;
|
|
||||||
using DiscordChatExporter.Core.Models;
|
|
||||||
using DiscordChatExporter.Core.Services;
|
|
||||||
using DiscordChatExporter.Core.Services.Exceptions;
|
|
||||||
using DiscordChatExporter.Core.Services.Helpers;
|
|
||||||
using Tyrrrz.Extensions;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Verbs
|
|
||||||
{
|
|
||||||
public class ExportGuildVerb : Verb<ExportGuildOptions>
|
|
||||||
{
|
|
||||||
public ExportGuildVerb(ExportGuildOptions options)
|
|
||||||
: base(options)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public override async Task ExecuteAsync()
|
|
||||||
{
|
|
||||||
// Get services
|
|
||||||
var settingsService = Container.Instance.Get<SettingsService>();
|
|
||||||
var dataService = Container.Instance.Get<DataService>();
|
|
||||||
var exportService = Container.Instance.Get<ExportService>();
|
|
||||||
|
|
||||||
// Configure settings
|
|
||||||
if (!Options.DateFormat.EmptyIfNull().IsWhiteSpace())
|
|
||||||
settingsService.DateFormat = Options.DateFormat;
|
|
||||||
|
|
||||||
// Get channels
|
|
||||||
var channels = await dataService.GetGuildChannelsAsync(Options.GetToken(), Options.GuildId);
|
|
||||||
|
|
||||||
// Filter and order channels
|
|
||||||
channels = channels.Where(c => c.Type == ChannelType.GuildTextChat).OrderBy(c => c.Name).ToArray();
|
|
||||||
|
|
||||||
// Loop through channels
|
|
||||||
foreach (var channel in channels)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// Track progress
|
|
||||||
Console.Write($"Exporting channel [{channel.Name}]... ");
|
|
||||||
using (var progress = new InlineProgress())
|
|
||||||
{
|
|
||||||
// Get chat log
|
|
||||||
var chatLog = await dataService.GetChatLogAsync(Options.GetToken(), channel,
|
|
||||||
Options.After, Options.Before, progress);
|
|
||||||
|
|
||||||
// Generate default file name
|
|
||||||
var fileName = ExportHelper.GetDefaultExportFileName(Options.ExportFormat, chatLog.Guild,
|
|
||||||
chatLog.Channel, Options.After, Options.Before);
|
|
||||||
|
|
||||||
// Generate file path
|
|
||||||
var filePath = Path.Combine(Options.OutputPath ?? "", fileName);
|
|
||||||
|
|
||||||
// Export
|
|
||||||
await exportService.ExportChatLogAsync(chatLog, filePath, Options.ExportFormat,
|
|
||||||
Options.PartitionLimit);
|
|
||||||
|
|
||||||
// Report successful completion
|
|
||||||
progress.ReportCompletion();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden)
|
|
||||||
{
|
|
||||||
Console.Error.WriteLine("You don't have access to this channel");
|
|
||||||
}
|
|
||||||
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
|
|
||||||
{
|
|
||||||
Console.Error.WriteLine("This channel doesn't exist");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using DiscordChatExporter.Cli.Verbs.Options;
|
|
||||||
using DiscordChatExporter.Core.Models;
|
|
||||||
using DiscordChatExporter.Core.Services;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Verbs
|
|
||||||
{
|
|
||||||
public class GetChannelsVerb : Verb<GetChannelsOptions>
|
|
||||||
{
|
|
||||||
public GetChannelsVerb(GetChannelsOptions options)
|
|
||||||
: base(options)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public override async Task ExecuteAsync()
|
|
||||||
{
|
|
||||||
// Get data service
|
|
||||||
var dataService = Container.Instance.Get<DataService>();
|
|
||||||
|
|
||||||
// Get channels
|
|
||||||
var channels = await dataService.GetGuildChannelsAsync(Options.GetToken(), Options.GuildId);
|
|
||||||
|
|
||||||
// Filter and order channels
|
|
||||||
channels = channels.Where(c => c.Type == ChannelType.GuildTextChat).OrderBy(c => c.Name).ToArray();
|
|
||||||
|
|
||||||
// Print result
|
|
||||||
foreach (var channel in channels)
|
|
||||||
Console.WriteLine($"{channel.Id} | {channel.Name}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using DiscordChatExporter.Cli.Verbs.Options;
|
|
||||||
using DiscordChatExporter.Core.Services;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Verbs
|
|
||||||
{
|
|
||||||
public class GetDirectMessageChannelsVerb : Verb<GetDirectMessageChannelsOptions>
|
|
||||||
{
|
|
||||||
public GetDirectMessageChannelsVerb(GetDirectMessageChannelsOptions options)
|
|
||||||
: base(options)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public override async Task ExecuteAsync()
|
|
||||||
{
|
|
||||||
// Get data service
|
|
||||||
var dataService = Container.Instance.Get<DataService>();
|
|
||||||
|
|
||||||
// Get channels
|
|
||||||
var channels = await dataService.GetDirectMessageChannelsAsync(Options.GetToken());
|
|
||||||
|
|
||||||
// Order channels
|
|
||||||
channels = channels.OrderBy(c => c.Name).ToArray();
|
|
||||||
|
|
||||||
// Print result
|
|
||||||
foreach (var channel in channels)
|
|
||||||
Console.WriteLine($"{channel.Id} | {channel.Name}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using DiscordChatExporter.Cli.Verbs.Options;
|
|
||||||
using DiscordChatExporter.Core.Services;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Verbs
|
|
||||||
{
|
|
||||||
public class GetGuildsVerb : Verb<GetGuildsOptions>
|
|
||||||
{
|
|
||||||
public GetGuildsVerb(GetGuildsOptions options)
|
|
||||||
: base(options)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public override async Task ExecuteAsync()
|
|
||||||
{
|
|
||||||
// Get data service
|
|
||||||
var dataService = Container.Instance.Get<DataService>();
|
|
||||||
|
|
||||||
// Get guilds
|
|
||||||
var guilds = await dataService.GetUserGuildsAsync(Options.GetToken());
|
|
||||||
|
|
||||||
// Order guilds
|
|
||||||
guilds = guilds.OrderBy(g => g.Name).ToArray();
|
|
||||||
|
|
||||||
// Print result
|
|
||||||
foreach (var guild in guilds)
|
|
||||||
Console.WriteLine($"{guild.Id} | {guild.Name}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
using CommandLine;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Verbs.Options
|
|
||||||
{
|
|
||||||
[Verb("export", HelpText = "Export channel.")]
|
|
||||||
public class ExportChannelOptions : ExportOptions
|
|
||||||
{
|
|
||||||
[Option('c', "channel", Required = true, HelpText = "Channel ID.")]
|
|
||||||
public string ChannelId { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
using CommandLine;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Verbs.Options
|
|
||||||
{
|
|
||||||
[Verb("exportdm", HelpText = "Export all direct message channels.")]
|
|
||||||
public class ExportDirectMessagesOptions : ExportOptions
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
using CommandLine;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Verbs.Options
|
|
||||||
{
|
|
||||||
[Verb("exportguild", HelpText = "Export all channels within a given guild.")]
|
|
||||||
public class ExportGuildOptions : ExportOptions
|
|
||||||
{
|
|
||||||
[Option('g', "guild", Required = true, HelpText = "Guild ID.")]
|
|
||||||
public string GuildId { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
using System;
|
|
||||||
using CommandLine;
|
|
||||||
using DiscordChatExporter.Core.Models;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Verbs.Options
|
|
||||||
{
|
|
||||||
public abstract class ExportOptions : TokenOptions
|
|
||||||
{
|
|
||||||
[Option('f', "format", Default = ExportFormat.HtmlDark, HelpText = "Output file format.")]
|
|
||||||
public ExportFormat ExportFormat { get; set; }
|
|
||||||
|
|
||||||
[Option('o', "output", Default = null, HelpText = "Output file or directory path.")]
|
|
||||||
public string OutputPath { get; set; }
|
|
||||||
|
|
||||||
// HACK: CommandLineParser doesn't support DateTimeOffset
|
|
||||||
[Option("after", Default = null, HelpText = "Limit to messages sent after this date.")]
|
|
||||||
public DateTime? After { get; set; }
|
|
||||||
|
|
||||||
// HACK: CommandLineParser doesn't support DateTimeOffset
|
|
||||||
[Option("before", Default = null, HelpText = "Limit to messages sent before this date.")]
|
|
||||||
public DateTime? Before { get; set; }
|
|
||||||
|
|
||||||
[Option('p', "partition", Default = null, HelpText = "Split output into partitions limited to this number of messages.")]
|
|
||||||
public int? PartitionLimit { get; set; }
|
|
||||||
|
|
||||||
[Option("dateformat", Default = null, HelpText = "Date format used in output.")]
|
|
||||||
public string DateFormat { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
using CommandLine;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Verbs.Options
|
|
||||||
{
|
|
||||||
[Verb("channels", HelpText = "Get the list of channels in the given guild.")]
|
|
||||||
public class GetChannelsOptions : TokenOptions
|
|
||||||
{
|
|
||||||
[Option('g', "guild", Required = true, HelpText = "Guild ID.")]
|
|
||||||
public string GuildId { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
using CommandLine;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Verbs.Options
|
|
||||||
{
|
|
||||||
[Verb("dm", HelpText = "Get the list of direct message channels.")]
|
|
||||||
public class GetDirectMessageChannelsOptions : TokenOptions
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
using CommandLine;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Verbs.Options
|
|
||||||
{
|
|
||||||
[Verb("guilds", HelpText = "Get the list of accessible guilds.")]
|
|
||||||
public class GetGuildsOptions : TokenOptions
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
using CommandLine;
|
|
||||||
using DiscordChatExporter.Core.Models;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Verbs.Options
|
|
||||||
{
|
|
||||||
public abstract class TokenOptions
|
|
||||||
{
|
|
||||||
[Option('t', "token", Required = true, HelpText = "Authorization token.")]
|
|
||||||
public string TokenValue { get; set; }
|
|
||||||
|
|
||||||
[Option('b', "bot", Default = false, HelpText = "Whether this authorization token belongs to a bot.")]
|
|
||||||
public bool IsBotToken { get; set; }
|
|
||||||
|
|
||||||
public AuthToken GetToken() => new AuthToken(IsBotToken ? AuthTokenType.Bot : AuthTokenType.User, TokenValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Cli.Verbs
|
|
||||||
{
|
|
||||||
public abstract class Verb<TOptions>
|
|
||||||
{
|
|
||||||
protected TOptions Options { get; }
|
|
||||||
|
|
||||||
protected Verb(TOptions options)
|
|
||||||
{
|
|
||||||
Options = options;
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract Task ExecuteAsync();
|
|
||||||
|
|
||||||
public virtual void Execute() => ExecuteAsync().GetAwaiter().GetResult();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
namespace DiscordChatExporter.Core.Markdown.Ast
|
||||||
|
{
|
||||||
|
public class EmojiNode : Node
|
||||||
|
{
|
||||||
|
public string? Id { get; }
|
||||||
|
|
||||||
|
public string Name { get; }
|
||||||
|
|
||||||
|
public bool IsAnimated { get; }
|
||||||
|
|
||||||
|
public bool IsCustomEmoji => !string.IsNullOrWhiteSpace(Id);
|
||||||
|
|
||||||
|
public EmojiNode(string? id, string name, bool isAnimated)
|
||||||
|
{
|
||||||
|
Id = id;
|
||||||
|
Name = name;
|
||||||
|
IsAnimated = isAnimated;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EmojiNode(string name)
|
||||||
|
: this(null, name, false)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString() => $"<Emoji> {Name}";
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-6
@@ -1,19 +1,15 @@
|
|||||||
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
|
||||||
{
|
{
|
||||||
public string Token { get; }
|
|
||||||
|
|
||||||
public TextFormatting Formatting { get; }
|
public TextFormatting Formatting { get; }
|
||||||
|
|
||||||
public IReadOnlyList<Node> Children { get; }
|
public IReadOnlyList<Node> Children { get; }
|
||||||
|
|
||||||
public FormattedNode(string source, string token, TextFormatting formatting, IReadOnlyList<Node> children)
|
public FormattedNode(TextFormatting formatting, IReadOnlyList<Node> children)
|
||||||
: base(source)
|
|
||||||
{
|
{
|
||||||
Token = token;
|
|
||||||
Formatting = formatting;
|
Formatting = formatting;
|
||||||
Children = children;
|
Children = children;
|
||||||
}
|
}
|
||||||
+2
-3
@@ -1,11 +1,10 @@
|
|||||||
namespace DiscordChatExporter.Core.Markdown.Nodes
|
namespace DiscordChatExporter.Core.Markdown.Ast
|
||||||
{
|
{
|
||||||
public class InlineCodeBlockNode : Node
|
public class InlineCodeBlockNode : Node
|
||||||
{
|
{
|
||||||
public string Code { get; }
|
public string Code { get; }
|
||||||
|
|
||||||
public InlineCodeBlockNode(string source, string code)
|
public InlineCodeBlockNode(string code)
|
||||||
: base(source)
|
|
||||||
{
|
{
|
||||||
Code = code;
|
Code = code;
|
||||||
}
|
}
|
||||||
+4
-4
@@ -1,4 +1,4 @@
|
|||||||
namespace DiscordChatExporter.Core.Markdown.Nodes
|
namespace DiscordChatExporter.Core.Markdown.Ast
|
||||||
{
|
{
|
||||||
public class LinkNode : Node
|
public class LinkNode : Node
|
||||||
{
|
{
|
||||||
@@ -6,14 +6,14 @@
|
|||||||
|
|
||||||
public string Title { get; }
|
public string Title { get; }
|
||||||
|
|
||||||
public LinkNode(string source, string url, string title)
|
public LinkNode(string url, string title)
|
||||||
: base(source)
|
|
||||||
{
|
{
|
||||||
Url = url;
|
Url = url;
|
||||||
Title = title;
|
Title = title;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LinkNode(string source, string url) : this(source, url, url)
|
public LinkNode(string url)
|
||||||
|
: this(url, url)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
+2
-3
@@ -1,4 +1,4 @@
|
|||||||
namespace DiscordChatExporter.Core.Markdown.Nodes
|
namespace DiscordChatExporter.Core.Markdown.Ast
|
||||||
{
|
{
|
||||||
public class MentionNode : Node
|
public class MentionNode : Node
|
||||||
{
|
{
|
||||||
@@ -6,8 +6,7 @@
|
|||||||
|
|
||||||
public MentionType Type { get; }
|
public MentionType Type { get; }
|
||||||
|
|
||||||
public MentionNode(string source, string id, MentionType type)
|
public MentionNode(string id, MentionType type)
|
||||||
: base(source)
|
|
||||||
{
|
{
|
||||||
Id = id;
|
Id = id;
|
||||||
Type = type;
|
Type = type;
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
namespace DiscordChatExporter.Core.Markdown.Nodes
|
namespace DiscordChatExporter.Core.Markdown.Ast
|
||||||
{
|
{
|
||||||
public enum MentionType
|
public enum MentionType
|
||||||
{
|
{
|
||||||
+3
-4
@@ -1,13 +1,12 @@
|
|||||||
namespace DiscordChatExporter.Core.Markdown.Nodes
|
namespace DiscordChatExporter.Core.Markdown.Ast
|
||||||
{
|
{
|
||||||
public class MultilineCodeBlockNode : Node
|
public class MultiLineCodeBlockNode : Node
|
||||||
{
|
{
|
||||||
public string Language { get; }
|
public string Language { get; }
|
||||||
|
|
||||||
public string Code { get; }
|
public string Code { get; }
|
||||||
|
|
||||||
public MultilineCodeBlockNode(string source, string language, string code)
|
public MultiLineCodeBlockNode(string language, string code)
|
||||||
: base(source)
|
|
||||||
{
|
{
|
||||||
Language = language;
|
Language = language;
|
||||||
Code = code;
|
Code = code;
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace DiscordChatExporter.Core.Markdown.Ast
|
||||||
|
{
|
||||||
|
public abstract class Node
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
+3
-2
@@ -1,4 +1,4 @@
|
|||||||
namespace DiscordChatExporter.Core.Markdown.Nodes
|
namespace DiscordChatExporter.Core.Markdown.Ast
|
||||||
{
|
{
|
||||||
public enum TextFormatting
|
public enum TextFormatting
|
||||||
{
|
{
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
Italic,
|
Italic,
|
||||||
Underline,
|
Underline,
|
||||||
Strikethrough,
|
Strikethrough,
|
||||||
Spoiler
|
Spoiler,
|
||||||
|
Quote
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
namespace DiscordChatExporter.Core.Markdown.Ast
|
||||||
|
{
|
||||||
|
public class TextNode : Node
|
||||||
|
{
|
||||||
|
public string Text { get; }
|
||||||
|
|
||||||
|
public TextNode(string text)
|
||||||
|
{
|
||||||
|
Text = text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString() => Text;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +1,8 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<Import Project="../DiscordChatExporter.props" />
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFrameworks>net46;netstandard2.0</TargetFrameworks>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Tyrrrz.Extensions" Version="1.6.0" />
|
<PackageReference Include="Tyrrrz.Extensions" Version="1.6.5" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
@@ -16,27 +16,27 @@ namespace DiscordChatExporter.Core.Markdown.Internal
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public ParsedMatch<T> Match(string input, int startIndex, int length)
|
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)
|
||||||
{
|
{
|
||||||
// Try to match
|
// Try to match
|
||||||
var match = matcher.Match(input, startIndex, length);
|
var match = matcher.Match(stringPart);
|
||||||
|
|
||||||
// If there's no match - continue
|
// If there's no match - continue
|
||||||
if (match == null)
|
if (match == null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// If this match is earlier than previous earliest - replace
|
// If this match is earlier than previous earliest - replace
|
||||||
if (earliestMatch == null || match.StartIndex < earliestMatch.StartIndex)
|
if (earliestMatch == null || match.StringPart.StartIndex < earliestMatch.StringPart.StartIndex)
|
||||||
earliestMatch = match;
|
earliestMatch = match;
|
||||||
|
|
||||||
// If the earliest match starts at the very beginning - break,
|
// If the earliest match starts at the very beginning - break,
|
||||||
// because it's impossible to find a match earlier than that
|
// because it's impossible to find a match earlier than that
|
||||||
if (earliestMatch.StartIndex == startIndex)
|
if (earliestMatch.StringPart.StartIndex == stringPart.StartIndex)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,46 +5,40 @@ namespace DiscordChatExporter.Core.Markdown.Internal
|
|||||||
{
|
{
|
||||||
internal static class Extensions
|
internal static class Extensions
|
||||||
{
|
{
|
||||||
public static IEnumerable<ParsedMatch<T>> MatchAll<T>(this IMatcher<T> matcher, string input,
|
public static IEnumerable<ParsedMatch<T>> MatchAll<T>(this IMatcher<T> matcher, StringPart stringPart,
|
||||||
int startIndex, int length, Func<string, T> fallbackTransform)
|
Func<StringPart, T> fallbackTransform)
|
||||||
{
|
{
|
||||||
// Get end index for simplicity
|
|
||||||
var endIndex = startIndex + length;
|
|
||||||
|
|
||||||
// Loop through segments divided by individual matches
|
// Loop through segments divided by individual matches
|
||||||
var currentIndex = startIndex;
|
var currentIndex = stringPart.StartIndex;
|
||||||
while (currentIndex < endIndex)
|
while (currentIndex < stringPart.EndIndex)
|
||||||
{
|
{
|
||||||
// Find a match within this segment
|
// Find a match within this segment
|
||||||
var match = matcher.Match(input, currentIndex, 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)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// 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.StartIndex > currentIndex)
|
if (match.StringPart.StartIndex > currentIndex)
|
||||||
{
|
{
|
||||||
var fallback = input.Substring(currentIndex, match.StartIndex - currentIndex);
|
var fallbackPart = stringPart.Slice(currentIndex, match.StringPart.StartIndex - currentIndex);
|
||||||
yield return new ParsedMatch<T>(currentIndex, fallback.Length, fallbackTransform(fallback));
|
yield return new ParsedMatch<T>(fallbackPart, fallbackTransform(fallbackPart));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Yield match
|
// Yield match
|
||||||
yield return match;
|
yield return match;
|
||||||
|
|
||||||
// Shift current index to the end of the match
|
// Shift current index to the end of the match
|
||||||
currentIndex = match.StartIndex + match.Length;
|
currentIndex = match.StringPart.StartIndex + match.StringPart.Length;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 < endIndex)
|
if (currentIndex < stringPart.EndIndex)
|
||||||
{
|
{
|
||||||
var fallback = input.Substring(currentIndex);
|
var fallbackPart = stringPart.Slice(currentIndex);
|
||||||
yield return new ParsedMatch<T>(currentIndex, fallback.Length, fallbackTransform(fallback));
|
yield return new ParsedMatch<T>(fallbackPart, fallbackTransform(fallbackPart));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IEnumerable<ParsedMatch<T>> MatchAll<T>(this IMatcher<T> matcher, string input,
|
|
||||||
Func<string, T> fallbackTransform) => matcher.MatchAll(input, 0, input.Length, fallbackTransform);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,6 @@
|
|||||||
{
|
{
|
||||||
internal interface IMatcher<T>
|
internal interface IMatcher<T>
|
||||||
{
|
{
|
||||||
ParsedMatch<T> Match(string input, int startIndex, int length);
|
ParsedMatch<T>? Match(StringPart stringPart);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,17 +1,14 @@
|
|||||||
namespace DiscordChatExporter.Core.Markdown.Internal
|
namespace DiscordChatExporter.Core.Markdown.Internal
|
||||||
{
|
{
|
||||||
internal partial class ParsedMatch<T>
|
internal class ParsedMatch<T>
|
||||||
{
|
{
|
||||||
public int StartIndex { get; }
|
public StringPart StringPart { get; }
|
||||||
|
|
||||||
public int Length { get; }
|
|
||||||
|
|
||||||
public T Value { get; }
|
public T Value { get; }
|
||||||
|
|
||||||
public ParsedMatch(int startIndex, int length, T value)
|
public ParsedMatch(StringPart stringPart, T value)
|
||||||
{
|
{
|
||||||
StartIndex = startIndex;
|
StringPart = stringPart;
|
||||||
Length = length;
|
|
||||||
Value = value;
|
Value = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,18 +6,35 @@ namespace DiscordChatExporter.Core.Markdown.Internal
|
|||||||
internal class RegexMatcher<T> : IMatcher<T>
|
internal class RegexMatcher<T> : IMatcher<T>
|
||||||
{
|
{
|
||||||
private readonly Regex _regex;
|
private readonly Regex _regex;
|
||||||
private readonly Func<Match, T> _transform;
|
private readonly Func<StringPart, Match, T> _transform;
|
||||||
|
|
||||||
public RegexMatcher(Regex regex, Func<Match, T> transform)
|
public RegexMatcher(Regex regex, Func<StringPart, Match, T> transform)
|
||||||
{
|
{
|
||||||
_regex = regex;
|
_regex = regex;
|
||||||
_transform = transform;
|
_transform = transform;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ParsedMatch<T> Match(string input, int startIndex, int length)
|
public RegexMatcher(Regex regex, Func<Match, T> transform)
|
||||||
|
: this(regex, (p, m) => transform(m))
|
||||||
{
|
{
|
||||||
var match = _regex.Match(input, startIndex, length);
|
}
|
||||||
return match.Success ? new ParsedMatch<T>(match.Index, match.Length, _transform(match)) : null;
|
|
||||||
|
public ParsedMatch<T>? Match(StringPart stringPart)
|
||||||
|
{
|
||||||
|
var match = _regex.Match(stringPart.Target, stringPart.StartIndex, stringPart.Length);
|
||||||
|
if (!match.Success)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
// Overload regex.Match(string, int, int) doesn't take the whole string into account,
|
||||||
|
// it effectively functions as a match check on a substring.
|
||||||
|
// Which is super weird because regex.Match(string, int) takes the whole input in context.
|
||||||
|
// So in order to properly account for ^/$ regex tokens, we need to make sure that
|
||||||
|
// the expression also matches on the bigger part of the input.
|
||||||
|
if (!_regex.IsMatch(stringPart.Target.Substring(0, stringPart.EndIndex), stringPart.StartIndex))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var stringPartMatch = stringPart.Slice(match.Index, match.Length);
|
||||||
|
return new ParsedMatch<T>(stringPartMatch, _transform(stringPartMatch, match));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,24 +6,31 @@ namespace DiscordChatExporter.Core.Markdown.Internal
|
|||||||
{
|
{
|
||||||
private readonly string _needle;
|
private readonly string _needle;
|
||||||
private readonly StringComparison _comparison;
|
private readonly StringComparison _comparison;
|
||||||
private readonly Func<string, T> _transform;
|
private readonly Func<StringPart, T> _transform;
|
||||||
|
|
||||||
public StringMatcher(string needle, StringComparison comparison, Func<string, T> transform)
|
public StringMatcher(string needle, StringComparison comparison, Func<StringPart, T> transform)
|
||||||
{
|
{
|
||||||
_needle = needle;
|
_needle = needle;
|
||||||
_comparison = comparison;
|
_comparison = comparison;
|
||||||
_transform = transform;
|
_transform = transform;
|
||||||
}
|
}
|
||||||
|
|
||||||
public StringMatcher(string needle, Func<string, T> transform)
|
public StringMatcher(string needle, Func<StringPart, T> transform)
|
||||||
: this(needle, StringComparison.Ordinal, transform)
|
: this(needle, StringComparison.Ordinal, transform)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public ParsedMatch<T> Match(string input, int startIndex, int length)
|
public ParsedMatch<T>? Match(StringPart stringPart)
|
||||||
{
|
{
|
||||||
var index = input.IndexOf(_needle, startIndex, length, _comparison);
|
var index = stringPart.Target.IndexOf(_needle, stringPart.StartIndex, stringPart.Length, _comparison);
|
||||||
return index >= 0 ? new ParsedMatch<T>(index, _needle.Length, _transform(_needle)) : null;
|
|
||||||
|
if (index >= 0)
|
||||||
|
{
|
||||||
|
var stringPartMatch = stringPart.Slice(index, _needle.Length);
|
||||||
|
return new ParsedMatch<T>(stringPartMatch, _transform(stringPartMatch));
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Core.Markdown.Internal
|
||||||
|
{
|
||||||
|
internal readonly struct StringPart
|
||||||
|
{
|
||||||
|
public string Target { get; }
|
||||||
|
|
||||||
|
public int StartIndex { get; }
|
||||||
|
|
||||||
|
public int Length { get; }
|
||||||
|
|
||||||
|
public int EndIndex { get; }
|
||||||
|
|
||||||
|
public StringPart(string target, int startIndex, int length)
|
||||||
|
{
|
||||||
|
Target = target;
|
||||||
|
StartIndex = startIndex;
|
||||||
|
Length = length;
|
||||||
|
EndIndex = startIndex + length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StringPart(string target)
|
||||||
|
: this(target, 0, target.Length)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,136 +1,147 @@
|
|||||||
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
|
||||||
{
|
{
|
||||||
// The following parsing logic is meant to replicate Discord's markdown grammar as close as possible
|
// The following parsing logic is meant to replicate Discord's markdown grammar as close as possible
|
||||||
public static class MarkdownParser
|
public static class MarkdownParser
|
||||||
{
|
{
|
||||||
private const RegexOptions DefaultRegexOptions = RegexOptions.Compiled | RegexOptions.CultureInvariant;
|
private const RegexOptions DefaultRegexOptions = RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.Multiline;
|
||||||
|
|
||||||
/* Formatting */
|
/* Formatting */
|
||||||
|
|
||||||
// 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),
|
||||||
m => new FormattedNode(m.Value, "**", TextFormatting.Bold, Parse(m.Groups[1].Value)));
|
(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 preceeded 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),
|
||||||
m => new FormattedNode(m.Value, "*", TextFormatting.Italic, Parse(m.Groups[1].Value)));
|
(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),
|
||||||
m => new FormattedNode(m.Value, "*", TextFormatting.Italic, Parse(m.Groups[1].Value, 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),
|
||||||
m => new FormattedNode(m.Value, "_", TextFormatting.Italic, Parse(m.Groups[1].Value)));
|
(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),
|
||||||
m => new FormattedNode(m.Value, "__", TextFormatting.Underline, Parse(m.Groups[1].Value)));
|
(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),
|
||||||
m => new FormattedNode(m.Value, "_", TextFormatting.Italic, Parse(m.Groups[1].Value, 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),
|
||||||
m => new FormattedNode(m.Value, "~~", TextFormatting.Strikethrough, Parse(m.Groups[1].Value)));
|
(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),
|
||||||
m => new FormattedNode(m.Value, "||", TextFormatting.Spoiler, Parse(m.Groups[1].Value)));
|
(p, m) => new FormattedNode(TextFormatting.Spoiler, Parse(p.Slice(m.Groups[1]))));
|
||||||
|
|
||||||
|
// Capture any character until the end of the line
|
||||||
|
// Opening 'greater than' character must be followed by whitespace
|
||||||
|
private static readonly IMatcher<Node> SingleLineQuoteNodeMatcher = new RegexMatcher<Node>(
|
||||||
|
new Regex("^>\\s(.+)\r?\n?", DefaultRegexOptions),
|
||||||
|
(p, m) => new FormattedNode(TextFormatting.Quote, Parse(p.Slice(m.Groups[1]))));
|
||||||
|
|
||||||
|
// Capture any character until the end of the input
|
||||||
|
// Opening 'greater than' characters must be followed by whitespace
|
||||||
|
private static readonly IMatcher<Node> MultiLineQuoteNodeMatcher = new RegexMatcher<Node>(
|
||||||
|
new Regex("^>>>\\s(.+)", DefaultRegexOptions | RegexOptions.Singleline),
|
||||||
|
(p, m) => new FormattedNode(TextFormatting.Quote, Parse(p.Slice(m.Groups[1]))));
|
||||||
|
|
||||||
/* Code blocks */
|
/* Code blocks */
|
||||||
|
|
||||||
// Capture any character except backtick until a backtick
|
// Capture any character except backtick until a backtick
|
||||||
// Whitespace surrounding content inside backticks is trimmed
|
// Blank lines at the beginning and end of content are trimmed
|
||||||
private static readonly IMatcher<Node> InlineCodeBlockNodeMatcher = new RegexMatcher<Node>(
|
private static readonly IMatcher<Node> InlineCodeBlockNodeMatcher = new RegexMatcher<Node>(
|
||||||
new Regex("`([^`]+)`", DefaultRegexOptions | RegexOptions.Singleline),
|
new Regex("`([^`]+)`", DefaultRegexOptions | RegexOptions.Singleline),
|
||||||
m => new InlineCodeBlockNode(m.Value, m.Groups[1].Value.Trim()));
|
m => new InlineCodeBlockNode(m.Groups[1].Value.Trim('\r', '\n')));
|
||||||
|
|
||||||
// Capture language identifier and then any character until the earliest triple backtick
|
// Capture language identifier and then any character until the earliest triple backtick
|
||||||
// Languge identifier is one word immediately after opening backticks, followed immediately by newline
|
// Language identifier is one word immediately after opening backticks, followed immediately by newline
|
||||||
// Whitespace surrounding content inside backticks is trimmed
|
// Blank lines at the beginning and end of content are trimmed
|
||||||
private static readonly IMatcher<Node> MultilineCodeBlockNodeMatcher = new RegexMatcher<Node>(
|
private static readonly IMatcher<Node> MultiLineCodeBlockNodeMatcher = new RegexMatcher<Node>(
|
||||||
new Regex("```(?:(\\w*)\\n)?(.+?)```", DefaultRegexOptions | RegexOptions.Singleline),
|
new Regex("```(?:(\\w*)\\n)?(.+?)```", DefaultRegexOptions | RegexOptions.Singleline),
|
||||||
m => new MultilineCodeBlockNode(m.Value, m.Groups[1].Value, m.Groups[2].Value.Trim()));
|
m => new MultiLineCodeBlockNode(m.Groups[1].Value, m.Groups[2].Value.Trim('\r', '\n')));
|
||||||
|
|
||||||
/* Mentions */
|
/* Mentions */
|
||||||
|
|
||||||
// Capture @everyone
|
// Capture @everyone
|
||||||
private static readonly IMatcher<Node> EveryoneMentionNodeMatcher = new StringMatcher<Node>(
|
private static readonly IMatcher<Node> EveryoneMentionNodeMatcher = new StringMatcher<Node>(
|
||||||
"@everyone",
|
"@everyone",
|
||||||
s => new MentionNode(s, "everyone", MentionType.Meta));
|
p => new MentionNode("everyone", MentionType.Meta));
|
||||||
|
|
||||||
// Capture @here
|
// Capture @here
|
||||||
private static readonly IMatcher<Node> HereMentionNodeMatcher = new StringMatcher<Node>(
|
private static readonly IMatcher<Node> HereMentionNodeMatcher = new StringMatcher<Node>(
|
||||||
"@here",
|
"@here",
|
||||||
s => new MentionNode(s, "here", MentionType.Meta));
|
p => new MentionNode("here", MentionType.Meta));
|
||||||
|
|
||||||
// Capture <@123456> or <@!123456>
|
// Capture <@123456> or <@!123456>
|
||||||
private static readonly IMatcher<Node> UserMentionNodeMatcher = new RegexMatcher<Node>(
|
private static readonly IMatcher<Node> UserMentionNodeMatcher = new RegexMatcher<Node>(
|
||||||
new Regex("<@!?(\\d+)>", DefaultRegexOptions),
|
new Regex("<@!?(\\d+)>", DefaultRegexOptions),
|
||||||
m => new MentionNode(m.Value, m.Groups[1].Value, MentionType.User));
|
m => new MentionNode(m.Groups[1].Value, MentionType.User));
|
||||||
|
|
||||||
// Capture <#123456>
|
// Capture <#123456>
|
||||||
private static readonly IMatcher<Node> ChannelMentionNodeMatcher = new RegexMatcher<Node>(
|
private static readonly IMatcher<Node> ChannelMentionNodeMatcher = new RegexMatcher<Node>(
|
||||||
new Regex("<#(\\d+)>", DefaultRegexOptions),
|
new Regex("<#(\\d+)>", DefaultRegexOptions),
|
||||||
m => new MentionNode(m.Value, m.Groups[1].Value, MentionType.Channel));
|
m => new MentionNode(m.Groups[1].Value, MentionType.Channel));
|
||||||
|
|
||||||
// Capture <@&123456>
|
// Capture <@&123456>
|
||||||
private static readonly IMatcher<Node> RoleMentionNodeMatcher = new RegexMatcher<Node>(
|
private static readonly IMatcher<Node> RoleMentionNodeMatcher = new RegexMatcher<Node>(
|
||||||
new Regex("<@&(\\d+)>", DefaultRegexOptions),
|
new Regex("<@&(\\d+)>", DefaultRegexOptions),
|
||||||
m => new MentionNode(m.Value, m.Groups[1].Value, MentionType.Role));
|
m => new MentionNode(m.Groups[1].Value, MentionType.Role));
|
||||||
|
|
||||||
/* 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.Value, 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.Value, m.Groups[3].Value, m.Groups[2].Value, !m.Groups[1].Value.IsEmpty()));
|
m => new EmojiNode(m.Groups[3].Value, m.Groups[2].Value, !string.IsNullOrWhiteSpace(m.Groups[1].Value)));
|
||||||
|
|
||||||
/* Links */
|
/* Links */
|
||||||
|
|
||||||
// Capture [title](link)
|
// Capture [title](link)
|
||||||
private static readonly IMatcher<Node> TitledLinkNodeMatcher = new RegexMatcher<Node>(
|
private static readonly IMatcher<Node> TitledLinkNodeMatcher = new RegexMatcher<Node>(
|
||||||
new Regex("\\[(.+?)\\]\\((.+?)\\)", DefaultRegexOptions),
|
new Regex("\\[(.+?)\\]\\((.+?)\\)", DefaultRegexOptions),
|
||||||
m => new LinkNode(m.Value, m.Groups[2].Value, m.Groups[1].Value));
|
m => new LinkNode(m.Groups[2].Value, m.Groups[1].Value));
|
||||||
|
|
||||||
// Capture any non-whitespace character after http:// or https:// until the last punctuation character or whitespace
|
// Capture any non-whitespace character after http:// or https:// until the last punctuation character or whitespace
|
||||||
private static readonly IMatcher<Node> AutoLinkNodeMatcher = new RegexMatcher<Node>(
|
private static readonly IMatcher<Node> AutoLinkNodeMatcher = new RegexMatcher<Node>(
|
||||||
new Regex("(https?://\\S*[^\\.,:;\"\'\\s])", DefaultRegexOptions),
|
new Regex("(https?://\\S*[^\\.,:;\"\'\\s])", DefaultRegexOptions),
|
||||||
m => new LinkNode(m.Value, m.Groups[1].Value));
|
m => new LinkNode(m.Groups[1].Value));
|
||||||
|
|
||||||
// Same as auto link but also surrounded by angular brackets
|
// Same as auto link but also surrounded by angular brackets
|
||||||
private static readonly IMatcher<Node> HiddenLinkNodeMatcher = new RegexMatcher<Node>(
|
private static readonly IMatcher<Node> HiddenLinkNodeMatcher = new RegexMatcher<Node>(
|
||||||
new Regex("<(https?://\\S*[^\\.,:;\"\'\\s])>", DefaultRegexOptions),
|
new Regex("<(https?://\\S*[^\\.,:;\"\'\\s])>", DefaultRegexOptions),
|
||||||
m => new LinkNode(m.Value, m.Groups[1].Value));
|
m => new LinkNode(m.Groups[1].Value));
|
||||||
|
|
||||||
/* Text */
|
/* Text */
|
||||||
|
|
||||||
@@ -138,23 +149,36 @@ namespace DiscordChatExporter.Core.Markdown
|
|||||||
// This escapes it from matching for formatting
|
// This escapes it from matching for formatting
|
||||||
private static readonly IMatcher<Node> ShrugTextNodeMatcher = new StringMatcher<Node>(
|
private static readonly IMatcher<Node> ShrugTextNodeMatcher = new StringMatcher<Node>(
|
||||||
@"¯\_(ツ)_/¯",
|
@"¯\_(ツ)_/¯",
|
||||||
s => new TextNode(s));
|
p => new TextNode(p.ToString()));
|
||||||
|
|
||||||
// Capture any "symbol/other" character or surrogate pair preceeded by a backslash
|
// Capture some specific emojis that don't get rendered
|
||||||
|
// This escapes it from matching for emoji
|
||||||
|
private static readonly IMatcher<Node> IgnoredEmojiTextNodeMatcher = new RegexMatcher<Node>(
|
||||||
|
new Regex("(\\u26A7|\\u2640|\\u2642|\\u2695|\\u267E|\\u00A9|\\u00AE|\\u2122)", DefaultRegexOptions),
|
||||||
|
m => new TextNode(m.Groups[1].Value));
|
||||||
|
|
||||||
|
// Capture any "symbol/other" character or surrogate pair preceded by a backslash
|
||||||
// This escapes it from matching for emoji
|
// This escapes it from matching for emoji
|
||||||
private static readonly IMatcher<Node> EscapedSymbolTextNodeMatcher = new RegexMatcher<Node>(
|
private static readonly IMatcher<Node> EscapedSymbolTextNodeMatcher = new RegexMatcher<Node>(
|
||||||
new Regex("\\\\(\\p{So}|\\p{Cs}{2})", DefaultRegexOptions),
|
new Regex("\\\\(\\p{So}|\\p{Cs}{2})", DefaultRegexOptions),
|
||||||
m => new TextNode(m.Value, m.Groups[1].Value));
|
m => new TextNode(m.Groups[1].Value));
|
||||||
|
|
||||||
// Capture any non-whitespace, non latin alphanumeric character preceeded by a backslash
|
// Capture any non-whitespace, non latin alphanumeric character preceded by a backslash
|
||||||
// This escapes it from matching for formatting or other tokens
|
// This escapes it from matching for formatting or other tokens
|
||||||
private static readonly IMatcher<Node> EscapedCharacterTextNodeMatcher = new RegexMatcher<Node>(
|
private static readonly IMatcher<Node> EscapedCharacterTextNodeMatcher = new RegexMatcher<Node>(
|
||||||
new Regex("\\\\([^a-zA-Z0-9\\s])", DefaultRegexOptions),
|
new Regex("\\\\([^a-zA-Z0-9\\s])", DefaultRegexOptions),
|
||||||
m => new TextNode(m.Value, m.Groups[1].Value));
|
m => new TextNode(m.Groups[1].Value));
|
||||||
|
|
||||||
// Combine all matchers into one
|
// Combine all matchers into one
|
||||||
// Matchers that have similar patterns are ordered from most specific to least specific
|
// Matchers that have similar patterns are ordered from most specific to least specific
|
||||||
private static readonly IMatcher<Node> AggregateNodeMatcher = new AggregateMatcher<Node>(
|
private static readonly IMatcher<Node> AggregateNodeMatcher = new AggregateMatcher<Node>(
|
||||||
|
// Escaped text
|
||||||
|
ShrugTextNodeMatcher,
|
||||||
|
IgnoredEmojiTextNodeMatcher,
|
||||||
|
EscapedSymbolTextNodeMatcher,
|
||||||
|
EscapedCharacterTextNodeMatcher,
|
||||||
|
|
||||||
|
// Formatting
|
||||||
ItalicBoldFormattedNodeMatcher,
|
ItalicBoldFormattedNodeMatcher,
|
||||||
ItalicUnderlineFormattedNodeMatcher,
|
ItalicUnderlineFormattedNodeMatcher,
|
||||||
BoldFormattedNodeMatcher,
|
BoldFormattedNodeMatcher,
|
||||||
@@ -163,25 +187,50 @@ namespace DiscordChatExporter.Core.Markdown
|
|||||||
ItalicAltFormattedNodeMatcher,
|
ItalicAltFormattedNodeMatcher,
|
||||||
StrikethroughFormattedNodeMatcher,
|
StrikethroughFormattedNodeMatcher,
|
||||||
SpoilerFormattedNodeMatcher,
|
SpoilerFormattedNodeMatcher,
|
||||||
MultilineCodeBlockNodeMatcher,
|
MultiLineQuoteNodeMatcher,
|
||||||
|
SingleLineQuoteNodeMatcher,
|
||||||
|
|
||||||
|
// Code blocks
|
||||||
|
MultiLineCodeBlockNodeMatcher,
|
||||||
InlineCodeBlockNodeMatcher,
|
InlineCodeBlockNodeMatcher,
|
||||||
|
|
||||||
|
// Mentions
|
||||||
EveryoneMentionNodeMatcher,
|
EveryoneMentionNodeMatcher,
|
||||||
HereMentionNodeMatcher,
|
HereMentionNodeMatcher,
|
||||||
UserMentionNodeMatcher,
|
UserMentionNodeMatcher,
|
||||||
ChannelMentionNodeMatcher,
|
ChannelMentionNodeMatcher,
|
||||||
RoleMentionNodeMatcher,
|
RoleMentionNodeMatcher,
|
||||||
StandardEmojiNodeMatcher,
|
|
||||||
CustomEmojiNodeMatcher,
|
// Links
|
||||||
TitledLinkNodeMatcher,
|
TitledLinkNodeMatcher,
|
||||||
AutoLinkNodeMatcher,
|
AutoLinkNodeMatcher,
|
||||||
HiddenLinkNodeMatcher,
|
HiddenLinkNodeMatcher,
|
||||||
ShrugTextNodeMatcher,
|
|
||||||
EscapedSymbolTextNodeMatcher,
|
|
||||||
EscapedCharacterTextNodeMatcher);
|
|
||||||
|
|
||||||
private static IReadOnlyList<Node> Parse(string input, IMatcher<Node> matcher) =>
|
// Emoji
|
||||||
matcher.MatchAll(input, s => new TextNode(s)).Select(r => r.Value).ToArray();
|
StandardEmojiNodeMatcher,
|
||||||
|
CustomEmojiNodeMatcher);
|
||||||
|
|
||||||
public static IReadOnlyList<Node> Parse(string input) => Parse(input, AggregateNodeMatcher);
|
// Minimal set of matchers for non-multimedia formats (e.g. plain text)
|
||||||
|
private static readonly IMatcher<Node> MinimalAggregateNodeMatcher = new AggregateMatcher<Node>(
|
||||||
|
// Mentions
|
||||||
|
EveryoneMentionNodeMatcher,
|
||||||
|
HereMentionNodeMatcher,
|
||||||
|
UserMentionNodeMatcher,
|
||||||
|
ChannelMentionNodeMatcher,
|
||||||
|
RoleMentionNodeMatcher,
|
||||||
|
|
||||||
|
// Emoji
|
||||||
|
CustomEmojiNodeMatcher);
|
||||||
|
|
||||||
|
private static IReadOnlyList<Node> Parse(StringPart stringPart, IMatcher<Node> matcher) =>
|
||||||
|
matcher.MatchAll(stringPart, p => new TextNode(p.ToString())).Select(r => r.Value).ToArray();
|
||||||
|
|
||||||
|
private static IReadOnlyList<Node> Parse(StringPart stringPart) => Parse(stringPart, AggregateNodeMatcher);
|
||||||
|
|
||||||
|
private static IReadOnlyList<Node> ParseMinimal(StringPart stringPart) => Parse(stringPart, MinimalAggregateNodeMatcher);
|
||||||
|
|
||||||
|
public static IReadOnlyList<Node> Parse(string input) => Parse(new StringPart(input));
|
||||||
|
|
||||||
|
public static IReadOnlyList<Node> ParseMinimal(string input) => ParseMinimal(new StringPart(input));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
namespace DiscordChatExporter.Core.Markdown.Nodes
|
|
||||||
{
|
|
||||||
public class EmojiNode : Node
|
|
||||||
{
|
|
||||||
public string Id { get; }
|
|
||||||
|
|
||||||
public string Name { get; }
|
|
||||||
|
|
||||||
public bool IsAnimated { get; }
|
|
||||||
|
|
||||||
public bool IsCustomEmoji => Id != null;
|
|
||||||
|
|
||||||
public EmojiNode(string source, string id, string name, bool isAnimated)
|
|
||||||
: base(source)
|
|
||||||
{
|
|
||||||
Id = id;
|
|
||||||
Name = name;
|
|
||||||
IsAnimated = isAnimated;
|
|
||||||
}
|
|
||||||
|
|
||||||
public EmojiNode(string source, string name)
|
|
||||||
: this(source, null, name, false)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() => $"<Emoji> {Name}";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
namespace DiscordChatExporter.Core.Markdown.Nodes
|
|
||||||
{
|
|
||||||
public abstract class Node
|
|
||||||
{
|
|
||||||
public string Source { get; }
|
|
||||||
|
|
||||||
protected Node(string source)
|
|
||||||
{
|
|
||||||
Source = source;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
namespace DiscordChatExporter.Core.Markdown.Nodes
|
|
||||||
{
|
|
||||||
public class TextNode : Node
|
|
||||||
{
|
|
||||||
public string Text { get; }
|
|
||||||
|
|
||||||
public TextNode(string source, string text)
|
|
||||||
: base(source)
|
|
||||||
{
|
|
||||||
Text = text;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TextNode(string text) : this(text, text)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() => Text;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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; }
|
||||||
|
|
||||||
|
|||||||
@@ -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,11 +1,8 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<Import Project="../DiscordChatExporter.props" />
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFrameworks>net46;netstandard2.0</TargetFrameworks>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Tyrrrz.Extensions" Version="1.6.0" />
|
<PackageReference Include="Tyrrrz.Extensions" Version="1.6.5" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
@@ -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;
|
||||||
|
|||||||
@@ -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 != null)
|
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)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
PlainText,
|
PlainText,
|
||||||
HtmlDark,
|
HtmlDark,
|
||||||
HtmlLight,
|
HtmlLight,
|
||||||
Csv
|
Csv,
|
||||||
|
Json
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,32 +4,33 @@ 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",
|
||||||
|
ExportFormat.Json => "json",
|
||||||
|
_ => 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 => "TXT",
|
||||||
if (format == ExportFormat.HtmlDark)
|
ExportFormat.HtmlDark => "HTML (Dark)",
|
||||||
return "HTML (Dark)";
|
ExportFormat.HtmlLight => "HTML (Light)",
|
||||||
if (format == ExportFormat.HtmlLight)
|
ExportFormat.Csv => "CSV",
|
||||||
return "HTML (Light)";
|
ExportFormat.Json => "JSON",
|
||||||
if (format == ExportFormat.Csv)
|
_ => throw new ArgumentOutOfRangeException(nameof(format))
|
||||||
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;
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
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;
|
||||||
@@ -26,9 +26,9 @@
|
|||||||
|
|
||||||
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 != null
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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,9 +31,11 @@ namespace DiscordChatExporter.Core.Models
|
|||||||
|
|
||||||
public IReadOnlyList<User> MentionedUsers { get; }
|
public IReadOnlyList<User> MentionedUsers { get; }
|
||||||
|
|
||||||
public Message(string id, string channelId, MessageType type, User author, DateTimeOffset timestamp,
|
public Message(string id, string channelId, MessageType type, User author,
|
||||||
DateTimeOffset? editedTimestamp, string content, IReadOnlyList<Attachment> attachments,
|
DateTimeOffset timestamp, DateTimeOffset? editedTimestamp, bool isPinned,
|
||||||
IReadOnlyList<Embed> embeds, IReadOnlyList<Reaction> reactions, IReadOnlyList<User> mentionedUsers)
|
string content,
|
||||||
|
IReadOnlyList<Attachment> attachments,IReadOnlyList<Embed> embeds, IReadOnlyList<Reaction> reactions,
|
||||||
|
IReadOnlyList<User> mentionedUsers)
|
||||||
{
|
{
|
||||||
Id = id;
|
Id = id;
|
||||||
ChannelId = channelId;
|
ChannelId = channelId;
|
||||||
@@ -39,6 +43,7 @@ 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;
|
||||||
@@ -46,6 +51,6 @@ namespace DiscordChatExporter.Core.Models
|
|||||||
MentionedUsers = mentionedUsers;
|
MentionedUsers = mentionedUsers;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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; }
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ 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; }
|
||||||
|
|
||||||
@@ -14,16 +14,19 @@ 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 User(string id, int discriminator, string name, string avatarHash)
|
public bool IsBot { get; }
|
||||||
|
|
||||||
|
public User(string id, int discriminator, string name, string? avatarHash, bool isBot)
|
||||||
{
|
{
|
||||||
Id = id;
|
Id = id;
|
||||||
Discriminator = discriminator;
|
Discriminator = discriminator;
|
||||||
Name = name;
|
Name = name;
|
||||||
AvatarHash = avatarHash;
|
AvatarHash = avatarHash;
|
||||||
|
IsBot = isBot;
|
||||||
|
|
||||||
FullName = GetFullName(name, discriminator);
|
FullName = GetFullName(name, discriminator);
|
||||||
AvatarUrl = GetAvatarUrl(id, discriminator, avatarHash);
|
AvatarUrl = GetAvatarUrl(id, discriminator, avatarHash);
|
||||||
@@ -36,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 != null)
|
if (!string.IsNullOrWhiteSpace(avatarHash))
|
||||||
{
|
{
|
||||||
// Animated
|
// Animated
|
||||||
if (avatarHash.StartsWith("a_", StringComparison.Ordinal))
|
if (avatarHash.StartsWith("a_", StringComparison.Ordinal))
|
||||||
@@ -53,6 +56,6 @@ namespace DiscordChatExporter.Core.Models
|
|||||||
return $"https://cdn.discordapp.com/embed/avatars/{discriminator % 5}.png";
|
return $"https://cdn.discordapp.com/embed/avatars/{discriminator % 5}.png";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static User CreateUnknownUser(string id) => new User(id, 0, "Unknown", null);
|
public static User CreateUnknownUser(string id) => new User(id, 0, "Unknown", null, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,113 +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)
|
|
||||||
{
|
|
||||||
// Formatted node
|
|
||||||
if (node is FormattedNode formattedNode)
|
|
||||||
{
|
|
||||||
// Recursively get inner text
|
|
||||||
var innerText = FormatMarkdown(formattedNode.Children);
|
|
||||||
|
|
||||||
return $"{formattedNode.Token}{innerText}{formattedNode.Token}";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Non-meta mention node
|
|
||||||
if (node is MentionNode mentionNode && mentionNode.Type != MentionType.Meta)
|
|
||||||
{
|
|
||||||
// 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}";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Custom emoji node
|
|
||||||
if (node is EmojiNode emojiNode && emojiNode.IsCustomEmoji)
|
|
||||||
{
|
|
||||||
return $":{emojiNode.Name}:";
|
|
||||||
}
|
|
||||||
|
|
||||||
// All other nodes - simply return source
|
|
||||||
return node.Source;
|
|
||||||
}
|
|
||||||
|
|
||||||
private string FormatMarkdown(IEnumerable<Node> nodes) => nodes.Select(FormatMarkdown).JoinToString("");
|
|
||||||
|
|
||||||
private string FormatMarkdown(string markdown) => FormatMarkdown(MarkdownParser.Parse(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
|
|
||||||
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);
|
|
||||||
|
|
||||||
// Line break
|
|
||||||
await writer.WriteLineAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task RenderAsync(TextWriter writer)
|
|
||||||
{
|
|
||||||
// Headers
|
|
||||||
await writer.WriteLineAsync("Author;Date;Content;Attachments;");
|
|
||||||
|
|
||||||
// Log
|
|
||||||
foreach (var message in _chatLog.Messages)
|
|
||||||
await RenderMessageAsync(writer, message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,20 +1,16 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<Import Project="../DiscordChatExporter.props" />
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFrameworks>net46;netstandard2.0</TargetFrameworks>
|
|
||||||
</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.0.0" />
|
<PackageReference Include="Scriban" Version="2.1.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
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
|
||||||
|
{
|
||||||
|
private readonly TextWriter _writer;
|
||||||
|
|
||||||
|
public CsvMessageWriter(Stream stream, RenderContext context)
|
||||||
|
: base(stream, context)
|
||||||
|
{
|
||||||
|
_writer = new StreamWriter(stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async ValueTask DisposeAsync()
|
||||||
|
{
|
||||||
|
await _writer.DisposeAsync();
|
||||||
|
await base.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,171 @@
|
|||||||
|
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 TextWriter _writer;
|
||||||
|
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(Stream stream, RenderContext context, string themeName)
|
||||||
|
: base(stream, context)
|
||||||
|
{
|
||||||
|
_writer = new StreamWriter(stream);
|
||||||
|
_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 override async ValueTask DisposeAsync()
|
||||||
|
{
|
||||||
|
await _writer.DisposeAsync();
|
||||||
|
await base.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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,222 @@
|
|||||||
|
using System.IO;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using DiscordChatExporter.Core.Models;
|
||||||
|
using DiscordChatExporter.Core.Rendering.Internal;
|
||||||
|
using DiscordChatExporter.Core.Rendering.Logic;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Core.Rendering.Formatters
|
||||||
|
{
|
||||||
|
public class JsonMessageWriter : MessageWriterBase
|
||||||
|
{
|
||||||
|
private readonly Utf8JsonWriter _writer;
|
||||||
|
|
||||||
|
private long _messageCount;
|
||||||
|
|
||||||
|
public JsonMessageWriter(Stream stream, RenderContext context)
|
||||||
|
: base(stream, context)
|
||||||
|
{
|
||||||
|
_writer = new Utf8JsonWriter(stream, new JsonWriterOptions
|
||||||
|
{
|
||||||
|
Indented = true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task WritePreambleAsync()
|
||||||
|
{
|
||||||
|
// Root object (start)
|
||||||
|
_writer.WriteStartObject();
|
||||||
|
|
||||||
|
// Guild
|
||||||
|
_writer.WriteStartObject("guild");
|
||||||
|
_writer.WriteString("id", Context.Guild.Id);
|
||||||
|
_writer.WriteString("name", Context.Guild.Name);
|
||||||
|
_writer.WriteString("iconUrl", Context.Guild.IconUrl);
|
||||||
|
_writer.WriteEndObject();
|
||||||
|
|
||||||
|
// Channel
|
||||||
|
_writer.WriteStartObject("channel");
|
||||||
|
_writer.WriteString("id", Context.Channel.Id);
|
||||||
|
_writer.WriteString("type", Context.Channel.Type.ToString());
|
||||||
|
_writer.WriteString("name", Context.Channel.Name);
|
||||||
|
_writer.WriteString("topic", Context.Channel.Topic);
|
||||||
|
_writer.WriteEndObject();
|
||||||
|
|
||||||
|
// Date range
|
||||||
|
_writer.WriteStartObject("dateRange");
|
||||||
|
_writer.WriteString("after", Context.After);
|
||||||
|
_writer.WriteString("before", Context.Before);
|
||||||
|
_writer.WriteEndObject();
|
||||||
|
|
||||||
|
// Message array (start)
|
||||||
|
_writer.WriteStartArray("messages");
|
||||||
|
|
||||||
|
await _writer.FlushAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task WriteMessageAsync(Message message)
|
||||||
|
{
|
||||||
|
_writer.WriteStartObject();
|
||||||
|
|
||||||
|
// Metadata
|
||||||
|
_writer.WriteString("id", message.Id);
|
||||||
|
_writer.WriteString("type", message.Type.ToString());
|
||||||
|
_writer.WriteString("timestamp", message.Timestamp);
|
||||||
|
_writer.WriteString("timestampEdited", message.EditedTimestamp);
|
||||||
|
_writer.WriteBoolean("isPinned", message.IsPinned);
|
||||||
|
|
||||||
|
// Content
|
||||||
|
var content = PlainTextRenderingLogic.FormatMessageContent(Context, message);
|
||||||
|
_writer.WriteString("content", content);
|
||||||
|
|
||||||
|
// Author
|
||||||
|
_writer.WriteStartObject("author");
|
||||||
|
_writer.WriteString("id", message.Author.Id);
|
||||||
|
_writer.WriteString("name", message.Author.Name);
|
||||||
|
_writer.WriteString("discriminator", $"{message.Author.Discriminator:0000}");
|
||||||
|
_writer.WriteBoolean("isBot", message.Author.IsBot);
|
||||||
|
_writer.WriteString("avatarUrl", message.Author.AvatarUrl);
|
||||||
|
_writer.WriteEndObject();
|
||||||
|
|
||||||
|
// Attachments
|
||||||
|
_writer.WriteStartArray("attachments");
|
||||||
|
|
||||||
|
foreach (var attachment in message.Attachments)
|
||||||
|
{
|
||||||
|
_writer.WriteStartObject();
|
||||||
|
|
||||||
|
_writer.WriteString("id", attachment.Id);
|
||||||
|
_writer.WriteString("url", attachment.Url);
|
||||||
|
_writer.WriteString("fileName", attachment.FileName);
|
||||||
|
_writer.WriteNumber("fileSizeBytes", (long) attachment.FileSize.Bytes);
|
||||||
|
|
||||||
|
_writer.WriteEndObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
_writer.WriteEndArray();
|
||||||
|
|
||||||
|
// Embeds
|
||||||
|
_writer.WriteStartArray("embeds");
|
||||||
|
|
||||||
|
foreach (var embed in message.Embeds)
|
||||||
|
{
|
||||||
|
_writer.WriteStartObject();
|
||||||
|
|
||||||
|
_writer.WriteString("title", embed.Title);
|
||||||
|
_writer.WriteString("url", embed.Url);
|
||||||
|
_writer.WriteString("timestamp", embed.Timestamp);
|
||||||
|
_writer.WriteString("description", embed.Description);
|
||||||
|
|
||||||
|
// Author
|
||||||
|
if (embed.Author != null)
|
||||||
|
{
|
||||||
|
_writer.WriteStartObject("author");
|
||||||
|
_writer.WriteString("name", embed.Author.Name);
|
||||||
|
_writer.WriteString("url", embed.Author.Url);
|
||||||
|
_writer.WriteString("iconUrl", embed.Author.IconUrl);
|
||||||
|
_writer.WriteEndObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Thumbnail
|
||||||
|
if (embed.Thumbnail != null)
|
||||||
|
{
|
||||||
|
_writer.WriteStartObject("thumbnail");
|
||||||
|
_writer.WriteString("url", embed.Thumbnail.Url);
|
||||||
|
_writer.WriteNumber("width", embed.Thumbnail.Width);
|
||||||
|
_writer.WriteNumber("height", embed.Thumbnail.Height);
|
||||||
|
_writer.WriteEndObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Image
|
||||||
|
if (embed.Image != null)
|
||||||
|
{
|
||||||
|
_writer.WriteStartObject("image");
|
||||||
|
_writer.WriteString("url", embed.Image.Url);
|
||||||
|
_writer.WriteNumber("width", embed.Image.Width);
|
||||||
|
_writer.WriteNumber("height", embed.Image.Height);
|
||||||
|
_writer.WriteEndObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Footer
|
||||||
|
if (embed.Footer != null)
|
||||||
|
{
|
||||||
|
_writer.WriteStartObject("footer");
|
||||||
|
_writer.WriteString("text", embed.Footer.Text);
|
||||||
|
_writer.WriteString("iconUrl", embed.Footer.IconUrl);
|
||||||
|
_writer.WriteEndObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fields
|
||||||
|
_writer.WriteStartArray("fields");
|
||||||
|
|
||||||
|
foreach (var field in embed.Fields)
|
||||||
|
{
|
||||||
|
_writer.WriteStartObject();
|
||||||
|
|
||||||
|
_writer.WriteString("name", field.Name);
|
||||||
|
_writer.WriteString("value", field.Value);
|
||||||
|
_writer.WriteBoolean("isInline", field.IsInline);
|
||||||
|
|
||||||
|
_writer.WriteEndObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
_writer.WriteEndArray();
|
||||||
|
|
||||||
|
_writer.WriteEndObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
_writer.WriteEndArray();
|
||||||
|
|
||||||
|
// Reactions
|
||||||
|
_writer.WriteStartArray("reactions");
|
||||||
|
|
||||||
|
foreach (var reaction in message.Reactions)
|
||||||
|
{
|
||||||
|
_writer.WriteStartObject();
|
||||||
|
|
||||||
|
// Emoji
|
||||||
|
_writer.WriteStartObject("emoji");
|
||||||
|
_writer.WriteString("id", reaction.Emoji.Id);
|
||||||
|
_writer.WriteString("name", reaction.Emoji.Name);
|
||||||
|
_writer.WriteBoolean("isAnimated", reaction.Emoji.IsAnimated);
|
||||||
|
_writer.WriteString("imageUrl", reaction.Emoji.ImageUrl);
|
||||||
|
_writer.WriteEndObject();
|
||||||
|
|
||||||
|
// Count
|
||||||
|
_writer.WriteNumber("count", reaction.Count);
|
||||||
|
|
||||||
|
_writer.WriteEndObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
_writer.WriteEndArray();
|
||||||
|
|
||||||
|
_writer.WriteEndObject();
|
||||||
|
|
||||||
|
_messageCount++;
|
||||||
|
|
||||||
|
// Flush every 100 messages
|
||||||
|
if (_messageCount % 100 == 0)
|
||||||
|
await _writer.FlushAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task WritePostambleAsync()
|
||||||
|
{
|
||||||
|
// Message array (end)
|
||||||
|
_writer.WriteEndArray();
|
||||||
|
|
||||||
|
// Message count
|
||||||
|
_writer.WriteNumber("messageCount", _messageCount);
|
||||||
|
|
||||||
|
// Root object (end)
|
||||||
|
_writer.WriteEndObject();
|
||||||
|
|
||||||
|
await _writer.FlushAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async ValueTask DisposeAsync()
|
||||||
|
{
|
||||||
|
await _writer.DisposeAsync();
|
||||||
|
await base.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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 Stream Stream { get; }
|
||||||
|
|
||||||
|
protected RenderContext Context { get; }
|
||||||
|
|
||||||
|
protected MessageWriterBase(Stream stream, RenderContext context)
|
||||||
|
{
|
||||||
|
Stream = stream;
|
||||||
|
Context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual Task WritePreambleAsync() => Task.CompletedTask;
|
||||||
|
|
||||||
|
public abstract Task WriteMessageAsync(Message message);
|
||||||
|
|
||||||
|
public virtual Task WritePostambleAsync() => Task.CompletedTask;
|
||||||
|
|
||||||
|
public virtual async ValueTask DisposeAsync() => await Stream.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
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 readonly TextWriter _writer;
|
||||||
|
|
||||||
|
private long _messageCount;
|
||||||
|
|
||||||
|
public PlainTextMessageWriter(Stream stream, RenderContext context)
|
||||||
|
: base(stream, context)
|
||||||
|
{
|
||||||
|
_writer = new StreamWriter(stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async ValueTask DisposeAsync()
|
||||||
|
{
|
||||||
|
await _writer.DisposeAsync();
|
||||||
|
await base.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
using DiscordChatExporter.Core.Models;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
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 Scriban.Parsing;
|
|
||||||
using Scriban.Runtime;
|
|
||||||
using Scriban;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
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,198 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Globalization;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using DiscordChatExporter.Core.Markdown;
|
|
||||||
using DiscordChatExporter.Core.Markdown.Nodes;
|
|
||||||
using DiscordChatExporter.Core.Models;
|
|
||||||
using Scriban;
|
|
||||||
using Scriban.Runtime;
|
|
||||||
using Tyrrrz.Extensions;
|
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Rendering
|
|
||||||
{
|
|
||||||
public partial class HtmlChatLogRenderer : IChatLogRenderer
|
|
||||||
{
|
|
||||||
private readonly ChatLog _chatLog;
|
|
||||||
private readonly string _themeName;
|
|
||||||
private readonly string _dateFormat;
|
|
||||||
|
|
||||||
public HtmlChatLogRenderer(ChatLog chatLog, string themeName, string dateFormat)
|
|
||||||
{
|
|
||||||
_chatLog = chatLog;
|
|
||||||
_themeName = themeName;
|
|
||||||
_dateFormat = dateFormat;
|
|
||||||
}
|
|
||||||
|
|
||||||
private string HtmlEncode(string s) => WebUtility.HtmlEncode(s);
|
|
||||||
|
|
||||||
private string FormatDate(DateTimeOffset date) =>
|
|
||||||
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 isTopLevel, bool isSingle)
|
|
||||||
{
|
|
||||||
// Text node
|
|
||||||
if (node is TextNode textNode)
|
|
||||||
{
|
|
||||||
// Return HTML-encoded text
|
|
||||||
return HtmlEncode(textNode.Text);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Formatted node
|
|
||||||
if (node is FormattedNode formattedNode)
|
|
||||||
{
|
|
||||||
// Recursively get inner html
|
|
||||||
var innerHtml = FormatMarkdown(formattedNode.Children, false);
|
|
||||||
|
|
||||||
// Bold
|
|
||||||
if (formattedNode.Formatting == TextFormatting.Bold)
|
|
||||||
return $"<strong>{innerHtml}</strong>";
|
|
||||||
|
|
||||||
// Italic
|
|
||||||
if (formattedNode.Formatting == TextFormatting.Italic)
|
|
||||||
return $"<em>{innerHtml}</em>";
|
|
||||||
|
|
||||||
// Underline
|
|
||||||
if (formattedNode.Formatting == TextFormatting.Underline)
|
|
||||||
return $"<u>{innerHtml}</u>";
|
|
||||||
|
|
||||||
// Strikethrough
|
|
||||||
if (formattedNode.Formatting == TextFormatting.Strikethrough)
|
|
||||||
return $"<s>{innerHtml}</s>";
|
|
||||||
|
|
||||||
// Spoiler
|
|
||||||
if (formattedNode.Formatting == TextFormatting.Spoiler)
|
|
||||||
return $"<span class=\"spoiler\">{innerHtml}</span>";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Inline code block node
|
|
||||||
if (node is InlineCodeBlockNode inlineCodeBlockNode)
|
|
||||||
{
|
|
||||||
return $"<span class=\"pre pre--inline\">{HtmlEncode(inlineCodeBlockNode.Code)}</span>";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Multi-line code block node
|
|
||||||
if (node is MultilineCodeBlockNode multilineCodeBlockNode)
|
|
||||||
{
|
|
||||||
// Set language class for syntax highlighting
|
|
||||||
var languageCssClass = multilineCodeBlockNode.Language != null
|
|
||||||
? "language-" + multilineCodeBlockNode.Language
|
|
||||||
: null;
|
|
||||||
|
|
||||||
return $"<div class=\"pre pre--multiline {languageCssClass}\">{HtmlEncode(multilineCodeBlockNode.Code)}</div>";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mention node
|
|
||||||
if (node is MentionNode mentionNode)
|
|
||||||
{
|
|
||||||
// Meta mention node
|
|
||||||
if (mentionNode.Type == MentionType.Meta)
|
|
||||||
{
|
|
||||||
return $"<span class=\"mention\">@{HtmlEncode(mentionNode.Id)}</span>";
|
|
||||||
}
|
|
||||||
|
|
||||||
// User mention node
|
|
||||||
if (mentionNode.Type == MentionType.User)
|
|
||||||
{
|
|
||||||
var user = _chatLog.Mentionables.GetUser(mentionNode.Id);
|
|
||||||
return $"<span class=\"mention\" title=\"{HtmlEncode(user.FullName)}\">@{HtmlEncode(user.Name)}</span>";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Channel mention node
|
|
||||||
if (mentionNode.Type == MentionType.Channel)
|
|
||||||
{
|
|
||||||
var channel = _chatLog.Mentionables.GetChannel(mentionNode.Id);
|
|
||||||
return $"<span class=\"mention\">#{HtmlEncode(channel.Name)}</span>";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Role mention node
|
|
||||||
if (mentionNode.Type == MentionType.Role)
|
|
||||||
{
|
|
||||||
var role = _chatLog.Mentionables.GetRole(mentionNode.Id);
|
|
||||||
return $"<span class=\"mention\">@{HtmlEncode(role.Name)}</span>";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Emoji node
|
|
||||||
if (node is EmojiNode emojiNode)
|
|
||||||
{
|
|
||||||
// Get emoji image URL
|
|
||||||
var emojiImageUrl = Emoji.GetImageUrl(emojiNode.Id, emojiNode.Name, emojiNode.IsAnimated);
|
|
||||||
|
|
||||||
// Emoji can be jumboable if it's the only top-level node
|
|
||||||
var jumboableCssClass = isTopLevel && isSingle ? "emoji--large" : null;
|
|
||||||
|
|
||||||
return $"<img class=\"emoji {jumboableCssClass}\" alt=\"{emojiNode.Name}\" title=\"{emojiNode.Name}\" src=\"{emojiImageUrl}\" />";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Link node
|
|
||||||
if (node is LinkNode linkNode)
|
|
||||||
{
|
|
||||||
return $"<a href=\"{Uri.EscapeUriString(linkNode.Url)}\">{HtmlEncode(linkNode.Title)}</a>";
|
|
||||||
}
|
|
||||||
|
|
||||||
// All other nodes - simply return source
|
|
||||||
return node.Source;
|
|
||||||
}
|
|
||||||
|
|
||||||
private string FormatMarkdown(IReadOnlyList<Node> nodes, bool isTopLevel)
|
|
||||||
{
|
|
||||||
var isSingle = nodes.Count == 1;
|
|
||||||
return nodes.Select(n => FormatMarkdown(n, isTopLevel, isSingle)).JoinToString("");
|
|
||||||
}
|
|
||||||
|
|
||||||
private string FormatMarkdown(string markdown) => FormatMarkdown(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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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,43 @@
|
|||||||
|
using System;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void WriteString(this Utf8JsonWriter writer, string propertyName, DateTimeOffset? value)
|
||||||
|
{
|
||||||
|
writer.WritePropertyName(propertyName);
|
||||||
|
|
||||||
|
if (value != null)
|
||||||
|
writer.WriteStringValue(value.Value);
|
||||||
|
else
|
||||||
|
writer.WriteNullValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void WriteNumber(this Utf8JsonWriter writer, string propertyName, int? value)
|
||||||
|
{
|
||||||
|
writer.WritePropertyName(propertyName);
|
||||||
|
|
||||||
|
if (value != null)
|
||||||
|
writer.WriteNumberValue(value.Value);
|
||||||
|
else
|
||||||
|
writer.WriteNullValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,160 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using DiscordChatExporter.Core.Markdown;
|
||||||
|
using DiscordChatExporter.Core.Markdown.Ast;
|
||||||
|
using DiscordChatExporter.Core.Models;
|
||||||
|
using Tyrrrz.Extensions;
|
||||||
|
|
||||||
|
namespace DiscordChatExporter.Core.Rendering.Logic
|
||||||
|
{
|
||||||
|
internal static class HtmlRenderingLogic
|
||||||
|
{
|
||||||
|
public static bool CanBeGrouped(Message message1, Message message2)
|
||||||
|
{
|
||||||
|
if (message1.Author.Id != message2.Author.Id)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if ((message2.Timestamp - message1.Timestamp).Duration().TotalMinutes > 7)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string HtmlEncode(string s) => WebUtility.HtmlEncode(s);
|
||||||
|
|
||||||
|
private static string FormatMarkdownNode(RenderContext context, Node node, bool isJumbo)
|
||||||
|
{
|
||||||
|
// Text node
|
||||||
|
if (node is TextNode textNode)
|
||||||
|
{
|
||||||
|
// Return HTML-encoded text
|
||||||
|
return HtmlEncode(textNode.Text);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Formatted node
|
||||||
|
if (node is FormattedNode formattedNode)
|
||||||
|
{
|
||||||
|
// Recursively get inner html
|
||||||
|
var innerHtml = FormatMarkdownNodes(context, formattedNode.Children, false);
|
||||||
|
|
||||||
|
// Bold
|
||||||
|
if (formattedNode.Formatting == TextFormatting.Bold)
|
||||||
|
return $"<strong>{innerHtml}</strong>";
|
||||||
|
|
||||||
|
// Italic
|
||||||
|
if (formattedNode.Formatting == TextFormatting.Italic)
|
||||||
|
return $"<em>{innerHtml}</em>";
|
||||||
|
|
||||||
|
// Underline
|
||||||
|
if (formattedNode.Formatting == TextFormatting.Underline)
|
||||||
|
return $"<u>{innerHtml}</u>";
|
||||||
|
|
||||||
|
// Strikethrough
|
||||||
|
if (formattedNode.Formatting == TextFormatting.Strikethrough)
|
||||||
|
return $"<s>{innerHtml}</s>";
|
||||||
|
|
||||||
|
// Spoiler
|
||||||
|
if (formattedNode.Formatting == TextFormatting.Spoiler)
|
||||||
|
return $"<span class=\"spoiler\">{innerHtml}</span>";
|
||||||
|
|
||||||
|
// Quote
|
||||||
|
if (formattedNode.Formatting == TextFormatting.Quote)
|
||||||
|
return $"<div class=\"quote\">{innerHtml}</div>";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inline code block node
|
||||||
|
if (node is InlineCodeBlockNode inlineCodeBlockNode)
|
||||||
|
{
|
||||||
|
return $"<span class=\"pre pre--inline\">{HtmlEncode(inlineCodeBlockNode.Code)}</span>";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Multi-line code block node
|
||||||
|
if (node is MultiLineCodeBlockNode multilineCodeBlockNode)
|
||||||
|
{
|
||||||
|
// Set CSS class for syntax highlighting
|
||||||
|
var highlightCssClass = !string.IsNullOrWhiteSpace(multilineCodeBlockNode.Language)
|
||||||
|
? $"language-{multilineCodeBlockNode.Language}"
|
||||||
|
: "nohighlight";
|
||||||
|
|
||||||
|
return $"<div class=\"pre pre--multiline {highlightCssClass}\">{HtmlEncode(multilineCodeBlockNode.Code)}</div>";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mention node
|
||||||
|
if (node is MentionNode mentionNode)
|
||||||
|
{
|
||||||
|
// Meta mention node
|
||||||
|
if (mentionNode.Type == MentionType.Meta)
|
||||||
|
{
|
||||||
|
return $"<span class=\"mention\">@{HtmlEncode(mentionNode.Id)}</span>";
|
||||||
|
}
|
||||||
|
|
||||||
|
// User mention node
|
||||||
|
if (mentionNode.Type == MentionType.User)
|
||||||
|
{
|
||||||
|
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>";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Channel mention node
|
||||||
|
if (mentionNode.Type == MentionType.Channel)
|
||||||
|
{
|
||||||
|
var channel = context.MentionableChannels.FirstOrDefault(c => c.Id == mentionNode.Id) ??
|
||||||
|
Channel.CreateDeletedChannel(mentionNode.Id);
|
||||||
|
|
||||||
|
return $"<span class=\"mention\">#{HtmlEncode(channel.Name)}</span>";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Role mention node
|
||||||
|
if (mentionNode.Type == MentionType.Role)
|
||||||
|
{
|
||||||
|
var role = context.MentionableRoles.FirstOrDefault(r => r.Id == mentionNode.Id) ??
|
||||||
|
Role.CreateDeletedRole(mentionNode.Id);
|
||||||
|
|
||||||
|
return $"<span class=\"mention\">@{HtmlEncode(role.Name)}</span>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Emoji node
|
||||||
|
if (node is EmojiNode emojiNode)
|
||||||
|
{
|
||||||
|
// Get emoji image URL
|
||||||
|
var emojiImageUrl = Emoji.GetImageUrl(emojiNode.Id, emojiNode.Name, emojiNode.IsAnimated);
|
||||||
|
|
||||||
|
// Make emoji large if it's jumbo
|
||||||
|
var jumboableCssClass = isJumbo ? "emoji--large" : null;
|
||||||
|
|
||||||
|
return $"<img class=\"emoji {jumboableCssClass}\" alt=\"{emojiNode.Name}\" title=\"{emojiNode.Name}\" src=\"{emojiImageUrl}\" />";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Link node
|
||||||
|
if (node is LinkNode linkNode)
|
||||||
|
{
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
return string.IsNullOrWhiteSpace(linkedMessageId)
|
||||||
|
? $"<a href=\"{Uri.EscapeUriString(linkNode.Url)}\">{HtmlEncode(linkNode.Title)}</a>"
|
||||||
|
: $"<a href=\"{Uri.EscapeUriString(linkNode.Url)}\" onclick=\"scrollToMessage(event, '{linkedMessageId}')\">{HtmlEncode(linkNode.Title)}</a>";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Throw on unexpected nodes
|
||||||
|
throw new InvalidOperationException($"Unexpected node [{node.GetType()}].");
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
var isJumbo = isTopLevel && nodes.All(n => n is EmojiNode || n is TextNode textNode && string.IsNullOrWhiteSpace(textNode.Text));
|
||||||
|
|
||||||
|
return nodes.Select(n => FormatMarkdownNode(context, n, isJumbo)).JoinToString("");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string FormatMarkdown(RenderContext context, string markdown) =>
|
||||||
|
FormatMarkdownNodes(context, MarkdownParser.Parse(markdown), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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,124 @@
|
|||||||
|
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 a stream (it will get disposed by the writer)
|
||||||
|
var stream = File.Create(filePath);
|
||||||
|
|
||||||
|
// Create formatter
|
||||||
|
if (format == ExportFormat.PlainText)
|
||||||
|
return new PlainTextMessageWriter(stream, context);
|
||||||
|
|
||||||
|
if (format == ExportFormat.Csv)
|
||||||
|
return new CsvMessageWriter(stream, context);
|
||||||
|
|
||||||
|
if (format == ExportFormat.HtmlDark)
|
||||||
|
return new HtmlMessageWriter(stream, context, "Dark");
|
||||||
|
|
||||||
|
if (format == ExportFormat.HtmlLight)
|
||||||
|
return new HtmlMessageWriter(stream, context, "Light");
|
||||||
|
|
||||||
|
if (format == ExportFormat.Json)
|
||||||
|
return new JsonMessageWriter(stream, context);
|
||||||
|
|
||||||
|
throw new InvalidOperationException($"Unknown export format [{format}].");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,129 +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)
|
|
||||||
{
|
|
||||||
// Formatted node
|
|
||||||
if (node is FormattedNode formattedNode)
|
|
||||||
{
|
|
||||||
// Recursively get inner text
|
|
||||||
var innerText = FormatMarkdown(formattedNode.Children);
|
|
||||||
|
|
||||||
return $"{formattedNode.Token}{innerText}{formattedNode.Token}";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Non-meta mention node
|
|
||||||
if (node is MentionNode mentionNode && mentionNode.Type != MentionType.Meta)
|
|
||||||
{
|
|
||||||
// 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}";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Custom emoji node
|
|
||||||
if (node is EmojiNode emojiNode && emojiNode.IsCustomEmoji)
|
|
||||||
{
|
|
||||||
return $":{emojiNode.Name}:";
|
|
||||||
}
|
|
||||||
|
|
||||||
// All other nodes - simply return source
|
|
||||||
return node.Source;
|
|
||||||
}
|
|
||||||
|
|
||||||
private string FormatMarkdown(IEnumerable<Node> nodes) => nodes.Select(FormatMarkdown).JoinToString("");
|
|
||||||
|
|
||||||
private string FormatMarkdown(string markdown) => FormatMarkdown(MarkdownParser.Parse(markdown));
|
|
||||||
|
|
||||||
private async Task RenderMessageAsync(TextWriter writer, Message message)
|
|
||||||
{
|
|
||||||
// Timestamp and author
|
|
||||||
await writer.WriteLineAsync($"[{FormatDate(message.Timestamp)}] {message.Author.FullName}");
|
|
||||||
|
|
||||||
// Content
|
|
||||||
await writer.WriteLineAsync(FormatMarkdown(message.Content));
|
|
||||||
|
|
||||||
// Attachments
|
|
||||||
foreach (var attachment in message.Attachments)
|
|
||||||
await writer.WriteLineAsync(attachment.Url);
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
await writer.WriteLineAsync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user