From 5d82aa7001556d2b8e1525a0a2614da87eff4177 Mon Sep 17 00:00:00 2001 From: Brandon Smith Date: Sat, 5 Sep 2015 16:27:21 -0300 Subject: [PATCH] Renamed MessageHelper to Format, added Bold, Italics, Underline and Strikeout methods. --- src/Discord.Net/Format.cs | 74 ++++++++++++++++++++++++++++++++ src/Discord.Net/MessageHelper.cs | 46 -------------------- 2 files changed, 74 insertions(+), 46 deletions(-) create mode 100644 src/Discord.Net/Format.cs delete mode 100644 src/Discord.Net/MessageHelper.cs diff --git a/src/Discord.Net/Format.cs b/src/Discord.Net/Format.cs new file mode 100644 index 000000000..65ce691a4 --- /dev/null +++ b/src/Discord.Net/Format.cs @@ -0,0 +1,74 @@ +using System.Text; + +namespace Discord +{ + public static class Format + { + private static char[] specialChars = new char[] {'_', '*', '~', '\\' }; //Backslash must always be last! + + /// Removes all special formatting characters from the provided text. + private static string Escape(string text) + { + if (text.IndexOfAny(specialChars) >= 0) + { + StringBuilder builder = new StringBuilder(text); + foreach (char c in specialChars) + { + int length = builder.Length; + for (int i = 0; i < length; i++) + { + if (builder[i] == c) + { + builder.Insert(i, '\\'); + length++; + } + } + } + return builder.ToString(); + } + return text; + } + + /// Returns the string used to create a user mention. + public static string User(User user) + => $"<@{user.Id}>"; + /// Returns the string used to create a user mention. + public static string User(string userId) + => $"<@{userId}>"; + + /// Returns the string used to create a channel mention. + public static string Channel(Channel channel) + => $"<#{channel.Id}>"; + /// Returns the string used to create a channel mention. + public static string Channel(string channelId) + => $"<#{channelId}>"; + + /// Returns a markdown-formatted string with no formatting, optionally escaping the contents. + public static string Normal(string text, bool escape = true) + => escape ? Escape(text) : text; + /// Returns a markdown-formatted string with bold formatting, optionally escaping the contents. + public static string Bold(string text, bool escape = true) + => escape ? $"**{Escape(text)}**" : $"**{text}**"; + /// Returns a markdown-formatted string with italics formatting, optionally escaping the contents. + public static string Italics(string text, bool escape = true) + => escape ? $"*{Escape(text)}*" : $"*{text}*"; + /// Returns a markdown-formatted string with underline formatting, optionally escaping the contents. + public static string Underline(string text, bool escape = true) + => escape ? $"_{Escape(text)}_" : $"_{text}_"; + /// Returns a markdown-formatted string with strikeout formatting, optionally escaping the contents. + public static string Strikeout(string text, bool escape = true) + => escape ? $"~~{Escape(text)}~~" : $"~~{text}~~"; + + /// Returns a markdown-formatted string with multiple formatting, optionally escaping the contents. + public static string Text(string text, bool escape = true, bool bold = false, bool italics = false, bool underline = false, bool strikeout = false) + { + string result = text; + if (escape) result = Escape(result); + if (bold) result = Bold(result, false); + if (italics) result = Italics(result, false); + if (underline) result = Underline(result, false); + if (strikeout) result = Strikeout(result, false); + return result; + } + } +} diff --git a/src/Discord.Net/MessageHelper.cs b/src/Discord.Net/MessageHelper.cs deleted file mode 100644 index 06f05350f..000000000 --- a/src/Discord.Net/MessageHelper.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System.Text; - -namespace Discord -{ - public static class MessageHelper - { - private static char[] specialChars = new char[] {'_', '*', '\\' }; //Backslash must always be last! - - /// Removes all special formatting characters from the provided text. - public static string Escape(string text) - { - if (text.IndexOfAny(specialChars) >= 0) - { - StringBuilder builder = new StringBuilder(text); - foreach (char c in specialChars) - { - int length = builder.Length; - for (int i = 0; i < length; i++) - { - if (builder[i] == c) - { - builder.Insert(i, '\\'); - length++; - } - } - } - return builder.ToString(); - } - return text; - } - - /// Returns the string used to create a user mention. - public static string User(User user) - => $"<@{user.Id}>"; - /// Returns the string used to create a user mention. - public static string User(string userId) - => $"<@{userId}>"; - - /// Returns the string used to create a channel mention. - public static string Channel(Channel channel) - => $"<#{channel.Id}>"; - /// Returns the string used to create a channel mention. - public static string Channel(string channelId) - => $"<#{channelId}>"; - } -}