mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-03-01 12:43:43 +00:00
Refactor
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
namespace DiscordChatExporter.Core.Markdown.Ast
|
||||
{
|
||||
internal class MentionNode : MarkdownNode
|
||||
{
|
||||
public string Id { get; }
|
||||
|
||||
public MentionType Type { get; }
|
||||
|
||||
public MentionNode(string id, MentionType type)
|
||||
{
|
||||
Id = id;
|
||||
Type = type;
|
||||
}
|
||||
|
||||
public override string ToString() => $"<{Type} mention> {Id}";
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace DiscordChatExporter.Core.Markdown.Ast
|
||||
{
|
||||
internal enum MentionType
|
||||
{
|
||||
Meta,
|
||||
User,
|
||||
Channel,
|
||||
Role
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
using DiscordChatExporter.Core.Utils;
|
||||
|
||||
namespace DiscordChatExporter.Core.Markdown.Ast
|
||||
namespace DiscordChatExporter.Core.Markdown
|
||||
{
|
||||
internal class EmojiNode : MarkdownNode
|
||||
{
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace DiscordChatExporter.Core.Markdown.Ast
|
||||
namespace DiscordChatExporter.Core.Markdown
|
||||
{
|
||||
internal class FormattedNode : MarkdownNode
|
||||
{
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace DiscordChatExporter.Core.Markdown.Ast
|
||||
namespace DiscordChatExporter.Core.Markdown
|
||||
{
|
||||
internal class InlineCodeBlockNode : MarkdownNode
|
||||
{
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace DiscordChatExporter.Core.Markdown.Ast
|
||||
namespace DiscordChatExporter.Core.Markdown
|
||||
{
|
||||
internal class LinkNode : MarkdownNode
|
||||
{
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace DiscordChatExporter.Core.Markdown.Ast
|
||||
namespace DiscordChatExporter.Core.Markdown
|
||||
{
|
||||
internal abstract class MarkdownNode
|
||||
{
|
||||
10
DiscordChatExporter.Core/Markdown/MentionKind.cs
Normal file
10
DiscordChatExporter.Core/Markdown/MentionKind.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace DiscordChatExporter.Core.Markdown
|
||||
{
|
||||
internal enum MentionKind
|
||||
{
|
||||
Meta,
|
||||
User,
|
||||
Channel,
|
||||
Role
|
||||
}
|
||||
}
|
||||
17
DiscordChatExporter.Core/Markdown/MentionNode.cs
Normal file
17
DiscordChatExporter.Core/Markdown/MentionNode.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace DiscordChatExporter.Core.Markdown
|
||||
{
|
||||
internal class MentionNode : MarkdownNode
|
||||
{
|
||||
public string Id { get; }
|
||||
|
||||
public MentionKind Kind { get; }
|
||||
|
||||
public MentionNode(string id, MentionKind kind)
|
||||
{
|
||||
Id = id;
|
||||
Kind = kind;
|
||||
}
|
||||
|
||||
public override string ToString() => $"<{Kind} mention> {Id}";
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace DiscordChatExporter.Core.Markdown.Ast
|
||||
namespace DiscordChatExporter.Core.Markdown
|
||||
{
|
||||
internal class MultiLineCodeBlockNode : MarkdownNode
|
||||
{
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace DiscordChatExporter.Core.Markdown.Matching
|
||||
namespace DiscordChatExporter.Core.Markdown.Parsing
|
||||
{
|
||||
internal class AggregateMatcher<T> : IMatcher<T>
|
||||
{
|
||||
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace DiscordChatExporter.Core.Markdown.Matching
|
||||
namespace DiscordChatExporter.Core.Markdown.Parsing
|
||||
{
|
||||
internal interface IMatcher<T>
|
||||
{
|
||||
@@ -1,11 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using DiscordChatExporter.Core.Markdown.Ast;
|
||||
using DiscordChatExporter.Core.Markdown.Matching;
|
||||
using DiscordChatExporter.Core.Utils;
|
||||
|
||||
namespace DiscordChatExporter.Core.Markdown
|
||||
namespace DiscordChatExporter.Core.Markdown.Parsing
|
||||
{
|
||||
// The following parsing logic is meant to replicate Discord's markdown grammar as close as possible
|
||||
internal static partial class MarkdownParser
|
||||
@@ -120,31 +118,31 @@ namespace DiscordChatExporter.Core.Markdown
|
||||
// Capture @everyone
|
||||
private static readonly IMatcher<MarkdownNode> EveryoneMentionNodeMatcher = new StringMatcher<MarkdownNode>(
|
||||
"@everyone",
|
||||
_ => new MentionNode("everyone", MentionType.Meta)
|
||||
_ => new MentionNode("everyone", MentionKind.Meta)
|
||||
);
|
||||
|
||||
// Capture @here
|
||||
private static readonly IMatcher<MarkdownNode> HereMentionNodeMatcher = new StringMatcher<MarkdownNode>(
|
||||
"@here",
|
||||
_ => new MentionNode("here", MentionType.Meta)
|
||||
_ => new MentionNode("here", MentionKind.Meta)
|
||||
);
|
||||
|
||||
// Capture <@123456> or <@!123456>
|
||||
private static readonly IMatcher<MarkdownNode> UserMentionNodeMatcher = new RegexMatcher<MarkdownNode>(
|
||||
new Regex("<@!?(\\d+)>", DefaultRegexOptions),
|
||||
(_, m) => new MentionNode(m.Groups[1].Value, MentionType.User)
|
||||
(_, m) => new MentionNode(m.Groups[1].Value, MentionKind.User)
|
||||
);
|
||||
|
||||
// Capture <#123456>
|
||||
private static readonly IMatcher<MarkdownNode> ChannelMentionNodeMatcher = new RegexMatcher<MarkdownNode>(
|
||||
new Regex("<#(\\d+)>", DefaultRegexOptions),
|
||||
(_, m) => new MentionNode(m.Groups[1].Value, MentionType.Channel)
|
||||
(_, m) => new MentionNode(m.Groups[1].Value, MentionKind.Channel)
|
||||
);
|
||||
|
||||
// Capture <@&123456>
|
||||
private static readonly IMatcher<MarkdownNode> RoleMentionNodeMatcher = new RegexMatcher<MarkdownNode>(
|
||||
new Regex("<@&(\\d+)>", DefaultRegexOptions),
|
||||
(_, m) => new MentionNode(m.Groups[1].Value, MentionType.Role)
|
||||
(_, m) => new MentionNode(m.Groups[1].Value, MentionKind.Role)
|
||||
);
|
||||
|
||||
/* Emojis */
|
||||
@@ -293,12 +291,16 @@ namespace DiscordChatExporter.Core.Markdown
|
||||
|
||||
internal static partial class MarkdownParser
|
||||
{
|
||||
private static IReadOnlyList<MarkdownNode> Parse(StringPart stringPart) => Parse(stringPart, AggregateNodeMatcher);
|
||||
private static IReadOnlyList<MarkdownNode> Parse(StringPart stringPart) =>
|
||||
Parse(stringPart, AggregateNodeMatcher);
|
||||
|
||||
private static IReadOnlyList<MarkdownNode> ParseMinimal(StringPart stringPart) => Parse(stringPart, MinimalAggregateNodeMatcher);
|
||||
private static IReadOnlyList<MarkdownNode> ParseMinimal(StringPart stringPart) =>
|
||||
Parse(stringPart, MinimalAggregateNodeMatcher);
|
||||
|
||||
public static IReadOnlyList<MarkdownNode> Parse(string input) => Parse(new StringPart(input));
|
||||
public static IReadOnlyList<MarkdownNode> Parse(string input) =>
|
||||
Parse(new StringPart(input));
|
||||
|
||||
public static IReadOnlyList<MarkdownNode> ParseMinimal(string input) => ParseMinimal(new StringPart(input));
|
||||
public static IReadOnlyList<MarkdownNode> ParseMinimal(string input) =>
|
||||
ParseMinimal(new StringPart(input));
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using DiscordChatExporter.Core.Markdown.Ast;
|
||||
|
||||
namespace DiscordChatExporter.Core.Markdown
|
||||
namespace DiscordChatExporter.Core.Markdown.Parsing
|
||||
{
|
||||
internal abstract class MarkdownVisitor
|
||||
{
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace DiscordChatExporter.Core.Markdown.Matching
|
||||
namespace DiscordChatExporter.Core.Markdown.Parsing
|
||||
{
|
||||
internal class ParsedMatch<T>
|
||||
{
|
||||
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace DiscordChatExporter.Core.Markdown.Matching
|
||||
namespace DiscordChatExporter.Core.Markdown.Parsing
|
||||
{
|
||||
internal class RegexMatcher<T> : IMatcher<T>
|
||||
{
|
||||
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
|
||||
namespace DiscordChatExporter.Core.Markdown.Matching
|
||||
namespace DiscordChatExporter.Core.Markdown.Parsing
|
||||
{
|
||||
internal class StringMatcher<T> : IMatcher<T>
|
||||
{
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace DiscordChatExporter.Core.Markdown.Matching
|
||||
namespace DiscordChatExporter.Core.Markdown.Parsing
|
||||
{
|
||||
internal readonly struct StringPart
|
||||
{
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace DiscordChatExporter.Core.Markdown.Ast
|
||||
namespace DiscordChatExporter.Core.Markdown
|
||||
{
|
||||
internal enum TextFormatting
|
||||
{
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace DiscordChatExporter.Core.Markdown.Ast
|
||||
namespace DiscordChatExporter.Core.Markdown
|
||||
{
|
||||
internal class TextNode : MarkdownNode
|
||||
{
|
||||
Reference in New Issue
Block a user