|
@@ -1,34 +1,24 @@ |
|
|
using System.Text; |
|
|
using System.Text; |
|
|
|
|
|
using System.Text.RegularExpressions; |
|
|
|
|
|
|
|
|
namespace Discord |
|
|
namespace Discord |
|
|
{ |
|
|
{ |
|
|
public static class Format |
|
|
public static class Format |
|
|
{ |
|
|
{ |
|
|
private static char[] specialChars = new char[] { '_', '*', '~', '\\' }; //Backslash must always be last! |
|
|
|
|
|
|
|
|
private static readonly Regex _escapeRegex; |
|
|
|
|
|
private static readonly MatchEvaluator _escapeEvaluator; |
|
|
|
|
|
|
|
|
/// <summary> Removes all special formatting characters from the provided text. </summary> |
|
|
|
|
|
private static string Escape(string text) |
|
|
|
|
|
|
|
|
static Format() |
|
|
{ |
|
|
{ |
|
|
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; |
|
|
|
|
|
|
|
|
const string innerPattern = "[_*]|~~"; |
|
|
|
|
|
_escapeRegex = new Regex($@"(?:^|\W)(?:{innerPattern})|(?:{innerPattern})(?:\W|$)|\\", RegexOptions.Compiled); |
|
|
|
|
|
_escapeEvaluator = new MatchEvaluator(e => '\\' + e.Value); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary> Removes all special formatting characters from the provided text. </summary> |
|
|
|
|
|
private static string Escape(string text) |
|
|
|
|
|
=> _escapeRegex.Replace(text, _escapeEvaluator); |
|
|
|
|
|
|
|
|
/// <summary> Returns a markdown-formatted string with no formatting, optionally escaping the contents. </summary> |
|
|
/// <summary> Returns a markdown-formatted string with no formatting, optionally escaping the contents. </summary> |
|
|
public static string Normal(string text, bool escape = true) |
|
|
public static string Normal(string text, bool escape = true) |
|
|
=> escape ? Escape(text) : text; |
|
|
=> escape ? Escape(text) : text; |
|
|