2023-10-12 18:37:25 +03:00
|
|
|
|
using System.Net;
|
|
|
|
|
using Remora.Discord.Extensions.Formatting;
|
|
|
|
|
|
2024-05-16 18:34:26 +03:00
|
|
|
|
namespace TeamOctolings.Octobot.Extensions;
|
2023-10-12 18:37:25 +03:00
|
|
|
|
|
|
|
|
|
public static class StringExtensions
|
|
|
|
|
{
|
2023-10-12 19:51:51 +03:00
|
|
|
|
private const string ZeroWidthSpace = "";
|
|
|
|
|
|
2023-10-12 18:37:25 +03:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sanitizes a string for use in <see cref="Markdown.BlockCode(string)" /> by inserting zero-width spaces in between
|
|
|
|
|
/// symbols used to format the string with block code.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s">The string to sanitize.</param>
|
|
|
|
|
/// <returns>The sanitized string that can be safely used in <see cref="Markdown.BlockCode(string)" />.</returns>
|
|
|
|
|
private static string SanitizeForBlockCode(this string s)
|
|
|
|
|
{
|
2023-10-12 19:51:51 +03:00
|
|
|
|
return s.Replace("```", $"{ZeroWidthSpace}`{ZeroWidthSpace}`{ZeroWidthSpace}`{ZeroWidthSpace}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sanitizes a string for use in <see cref="Markdown.BlockCode(string, string)" /> when "language" is "diff" by
|
|
|
|
|
/// prepending a zero-width space before the input string to prevent Discord from applying syntax highlighting.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>This does not call <see cref="SanitizeForBlockCode"/>, you have to do so yourself if needed.</remarks>
|
|
|
|
|
/// <param name="s">The string to sanitize.</param>
|
|
|
|
|
/// <returns>The sanitized string that can be safely used in <see cref="Markdown.BlockCode(string, string)" /> with "diff" as the language.</returns>
|
|
|
|
|
public static string SanitizeForDiffBlock(this string s)
|
|
|
|
|
{
|
|
|
|
|
return $"{ZeroWidthSpace}{s}";
|
2023-10-12 18:37:25 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sanitizes a string (see <see cref="SanitizeForBlockCode" />) and formats the string to use Markdown Block Code
|
|
|
|
|
/// formatting with a specified
|
|
|
|
|
/// language for syntax highlighting.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s">The string to sanitize and format.</param>
|
|
|
|
|
/// <param name="language"></param>
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// The sanitized string formatted to use Markdown Block Code with a specified
|
|
|
|
|
/// language for syntax highlighting.
|
|
|
|
|
/// </returns>
|
|
|
|
|
public static string InBlockCode(this string s, string language = "")
|
|
|
|
|
{
|
|
|
|
|
s = s.SanitizeForBlockCode();
|
|
|
|
|
return
|
2023-12-04 18:18:56 +03:00
|
|
|
|
$"```{language}\n{s.SanitizeForBlockCode()}{(s.EndsWith('`') || string.IsNullOrWhiteSpace(s) ? " " : "")}```";
|
2023-10-12 18:37:25 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string Localized(this string key)
|
|
|
|
|
{
|
|
|
|
|
return Messages.ResourceManager.GetString(key, Messages.Culture) ?? key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Encodes a string to allow its transmission in request headers.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>Used when encountering "Request headers must contain only ASCII characters".</remarks>
|
|
|
|
|
/// <param name="s">The string to encode.</param>
|
|
|
|
|
/// <returns>An encoded string with spaces kept intact.</returns>
|
|
|
|
|
public static string EncodeHeader(this string s)
|
|
|
|
|
{
|
|
|
|
|
return WebUtility.UrlEncode(s).Replace('+', ' ');
|
|
|
|
|
}
|
|
|
|
|
}
|