This commit is contained in:
Tyrrrz
2021-07-17 23:53:13 +03:00
parent e1726683f8
commit 650c55bbd1
47 changed files with 280 additions and 266 deletions

View File

@@ -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}";
}
}

View File

@@ -1,10 +0,0 @@
namespace DiscordChatExporter.Core.Markdown.Ast
{
internal enum MentionType
{
Meta,
User,
Channel,
Role
}
}

View File

@@ -1,6 +1,6 @@
using DiscordChatExporter.Core.Utils;
namespace DiscordChatExporter.Core.Markdown.Ast
namespace DiscordChatExporter.Core.Markdown
{
internal class EmojiNode : MarkdownNode
{

View File

@@ -1,6 +1,6 @@
using System.Collections.Generic;
namespace DiscordChatExporter.Core.Markdown.Ast
namespace DiscordChatExporter.Core.Markdown
{
internal class FormattedNode : MarkdownNode
{

View File

@@ -1,4 +1,4 @@
namespace DiscordChatExporter.Core.Markdown.Ast
namespace DiscordChatExporter.Core.Markdown
{
internal class InlineCodeBlockNode : MarkdownNode
{

View File

@@ -1,4 +1,4 @@
namespace DiscordChatExporter.Core.Markdown.Ast
namespace DiscordChatExporter.Core.Markdown
{
internal class LinkNode : MarkdownNode
{

View File

@@ -1,4 +1,4 @@
namespace DiscordChatExporter.Core.Markdown.Ast
namespace DiscordChatExporter.Core.Markdown
{
internal abstract class MarkdownNode
{

View File

@@ -0,0 +1,10 @@
namespace DiscordChatExporter.Core.Markdown
{
internal enum MentionKind
{
Meta,
User,
Channel,
Role
}
}

View 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}";
}
}

View File

@@ -1,4 +1,4 @@
namespace DiscordChatExporter.Core.Markdown.Ast
namespace DiscordChatExporter.Core.Markdown
{
internal class MultiLineCodeBlockNode : MarkdownNode
{

View File

@@ -1,6 +1,6 @@
using System.Collections.Generic;
namespace DiscordChatExporter.Core.Markdown.Matching
namespace DiscordChatExporter.Core.Markdown.Parsing
{
internal class AggregateMatcher<T> : IMatcher<T>
{

View File

@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
namespace DiscordChatExporter.Core.Markdown.Matching
namespace DiscordChatExporter.Core.Markdown.Parsing
{
internal interface IMatcher<T>
{

View File

@@ -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));
}
}

View File

@@ -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
{

View File

@@ -1,4 +1,4 @@
namespace DiscordChatExporter.Core.Markdown.Matching
namespace DiscordChatExporter.Core.Markdown.Parsing
{
internal class ParsedMatch<T>
{

View File

@@ -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>
{

View File

@@ -1,6 +1,6 @@
using System;
namespace DiscordChatExporter.Core.Markdown.Matching
namespace DiscordChatExporter.Core.Markdown.Parsing
{
internal class StringMatcher<T> : IMatcher<T>
{

View File

@@ -1,6 +1,6 @@
using System.Text.RegularExpressions;
namespace DiscordChatExporter.Core.Markdown.Matching
namespace DiscordChatExporter.Core.Markdown.Parsing
{
internal readonly struct StringPart
{

View File

@@ -1,4 +1,4 @@
namespace DiscordChatExporter.Core.Markdown.Ast
namespace DiscordChatExporter.Core.Markdown
{
internal enum TextFormatting
{

View File

@@ -1,4 +1,4 @@
namespace DiscordChatExporter.Core.Markdown.Ast
namespace DiscordChatExporter.Core.Markdown
{
internal class TextNode : MarkdownNode
{