Skip to content

Commit

Permalink
Move markdown conversion methods to Utils class
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Aug 11, 2024
1 parent 73f73de commit f059c0c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 48 deletions.
1 change: 1 addition & 0 deletions MCGalaxy/MCGalaxy_.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@
<Compile Include="Modules\Relay\Discord\DiscordBotEvents.cs" />
<Compile Include="Modules\Relay\Discord\DiscordMessages.cs" />
<Compile Include="Modules\Relay\Discord\DiscordPlugin.cs" />
<Compile Include="Modules\Relay\Discord\DiscordUtils.cs" />
<Compile Include="Modules\Relay\Discord\DiscordWebsocket.cs" />
<Compile Include="Modules\Relay\IRC\IRCNickList.cs" />
<Compile Include="Modules\Relay\IRC\IRCPlugin.cs" />
Expand Down
58 changes: 10 additions & 48 deletions MCGalaxy/Modules/Relay/Discord/DiscordBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void LoadReplacements() {
ChatTokens.LoadTokens(lines, (phrase, replacement) =>
{
filter_triggers.Add(phrase);
filter_replacements.Add(MarkdownToSpecial(replacement));
filter_replacements.Add(DiscordUtils.MarkdownToSpecial(replacement));
});
}

Expand Down Expand Up @@ -465,19 +465,8 @@ protected override void DoSendMessage(string channel, string message) {
protected string ConvertMessage(string message) {
message = ConvertMessageCommon(message);
message = Colors.StripUsed(message);
message = EscapeMarkdown(message);
message = SpecialToMarkdown(message);
return message;
}

static readonly string[] markdown_special = { @"\", @"*", @"_", @"~", @"`", @"|", @"-", @"#" };
static readonly string[] markdown_escaped = { @"\\", @"\*", @"\_", @"\~", @"\`", @"\|", @"\-", @"\#" };
static string EscapeMarkdown(string message) {
// don't let user use bold/italic etc markdown
for (int i = 0; i < markdown_special.Length; i++)
{
message = message.Replace(markdown_special[i], markdown_escaped[i]);
}
message = DiscordUtils.EscapeMarkdown(message);
message = DiscordUtils.SpecialToMarkdown(message);
return message;
}

Expand Down Expand Up @@ -546,7 +535,7 @@ static string FormatNick(Player p, Player pl) {
return string.Format(format, p.FormatNick(pl),
// level name must not have _ escaped as the level name is in a code block -
// otherwise the escaped "\_" actually shows as "\_" instead of "_"
pl.level.name.Replace('_', UNDERSCORE),
pl.level.name.Replace('_', DiscordUtils.UNDERSCORE),
flags);
}

Expand All @@ -568,38 +557,11 @@ void AddGameStatus(ChannelSendEmbed embed) {
}


// these characters are chosen specifically to lie within the unspecified unicode range,
// as those characters are "application defined" (EDCX = Escaped Discord Character #X)
// https://en.wikipedia.org/wiki/Private_Use_Areas
const char UNDERSCORE = '\uEDC1'; // _
const char TILDE = '\uEDC2'; // ~
const char STAR = '\uEDC3'; // *
const char GRAVE = '\uEDC4'; // `
const char BAR = '\uEDC5'; // |

public const string UNDERLINE = "\uEDC1\uEDC1"; // __
public const string BOLD = "\uEDC3\uEDC3"; // **
public const string ITALIC = "\uEDC1"; // _
public const string CODE = "\uEDC4"; // `
public const string SPOILER = "\uEDC5\uEDC5"; // ||
public const string STRIKETHROUGH = "\uEDC2\uEDC2"; // ~~

static string MarkdownToSpecial(string input) {
return input
.Replace('_', UNDERSCORE)
.Replace('~', TILDE)
.Replace('*', STAR)
.Replace('`', GRAVE)
.Replace('|', BAR);
}

static string SpecialToMarkdown(string input) {
return input
.Replace(UNDERSCORE, '_')
.Replace(TILDE, '~')
.Replace(STAR, '*')
.Replace(GRAVE, '`')
.Replace(BAR, '|');
}
public const string UNDERLINE = DiscordUtils.UNDERLINE;
public const string BOLD = DiscordUtils.BOLD;
public const string ITALIC = DiscordUtils.ITALIC;
public const string CODE = DiscordUtils.CODE;
public const string SPOILER = DiscordUtils.SPOILER;
public const string STRIKETHROUGH = DiscordUtils.STRIKETHROUGH;
}
}
70 changes: 70 additions & 0 deletions MCGalaxy/Modules/Relay/Discord/DiscordUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright 2015 MCGalaxy
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
https://opensource.org/license/ecl-2-0/
https://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;

namespace MCGalaxy.Modules.Relay.Discord
{
public static class DiscordUtils
{
static readonly string[] markdown_special = { @"\", @"*", @"_", @"~", @"`", @"|", @"-", @"#" };
static readonly string[] markdown_escaped = { @"\\", @"\*", @"\_", @"\~", @"\`", @"\|", @"\-", @"\#" };
public static string EscapeMarkdown(string message) {
// don't let user use bold/italic etc markdown
for (int i = 0; i < markdown_special.Length; i++)
{
message = message.Replace(markdown_special[i], markdown_escaped[i]);
}
return message;
}


// these characters are chosen specifically to lie within the unspecified unicode range,
// as those characters are "application defined" (EDCX = Escaped Discord Character #X)
// https://en.wikipedia.org/wiki/Private_Use_Areas
public const char UNDERSCORE = '\uEDC1'; // _
public const char TILDE = '\uEDC2'; // ~
public const char STAR = '\uEDC3'; // *
public const char GRAVE = '\uEDC4'; // `
public const char BAR = '\uEDC5'; // |

public const string UNDERLINE = "\uEDC1\uEDC1"; // __
public const string BOLD = "\uEDC3\uEDC3"; // **
public const string ITALIC = "\uEDC1"; // _
public const string CODE = "\uEDC4"; // `
public const string SPOILER = "\uEDC5\uEDC5"; // ||
public const string STRIKETHROUGH = "\uEDC2\uEDC2"; // ~~

public static string MarkdownToSpecial(string input) {
return input
.Replace('_', UNDERSCORE)
.Replace('~', TILDE)
.Replace('*', STAR)
.Replace('`', GRAVE)
.Replace('|', BAR);
}

public static string SpecialToMarkdown(string input) {
return input
.Replace(UNDERSCORE, '_')
.Replace(TILDE, '~')
.Replace(STAR, '*')
.Replace(GRAVE, '`')
.Replace(BAR, '|');
}
}
}

0 comments on commit f059c0c

Please sign in to comment.