diff --git a/DisCatSharp/Formatter.cs b/DisCatSharp/Formatter.cs index 9bad80487..395cb82c1 100644 --- a/DisCatSharp/Formatter.cs +++ b/DisCatSharp/Formatter.cs @@ -1,206 +1,206 @@ // This file is part of the DisCatSharp project, based off DSharpPlus. // // Copyright (c) 2021-2022 AITSYS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. using System; using System.Globalization; using System.Text.RegularExpressions; using DisCatSharp.Entities; using DisCatSharp.Enums; namespace DisCatSharp; /// /// Contains markdown formatting helpers. /// public static class Formatter { /// /// Gets the md sanitize regex. /// private static Regex s_mdSanitizeRegex { get; } = new(@"([`\*_~<>\[\]\(\)""@\!\&#:\|])", RegexOptions.ECMAScript); /// /// Gets the md strip regex. /// private static Regex s_mdStripRegex { get; } = new(@"([`\*_~\[\]\(\)""\|]|<@\!?\d+>|<#\d+>|<@\&\d+>|<:[a-zA-Z0-9_\-]:\d+>)", RegexOptions.ECMAScript); /// /// Creates a block of code. /// /// Contents of the block. /// Language to use for highlighting. /// Formatted block of code. - public static string BlockCode(string content, string language = "") + public static string BlockCode(this string content, string language = "") => $"```{language}\n{content}\n```"; /// /// Creates inline code snippet. /// /// Contents of the snippet. /// Formatted inline code snippet. - public static string InlineCode(string content) + public static string InlineCode(this string content) => $"`{content}`"; /// /// Creates a rendered timestamp. /// /// The time from now. /// The format to render the timestamp in. Defaults to relative. /// A formatted timestamp. - public static string Timestamp(TimeSpan time, TimestampFormat format = TimestampFormat.RelativeTime) + public static string Timestamp(this TimeSpan time, TimestampFormat format = TimestampFormat.RelativeTime) => Timestamp(DateTimeOffset.UtcNow + time, format); /// /// Creates a rendered timestamp. /// /// Timestamp to format. /// The format to render the timestamp in. Defaults to relative. /// A formatted timestamp. - public static string Timestamp(DateTimeOffset time, TimestampFormat format = TimestampFormat.RelativeTime) + public static string Timestamp(this DateTimeOffset time, TimestampFormat format = TimestampFormat.RelativeTime) => $""; /// /// Creates a rendered timestamp. /// /// The time from now. /// The format to render the timestamp in. Defaults to relative. /// A formatted timestamp relative to now. - public static string Timestamp(DateTime time, TimestampFormat format = TimestampFormat.RelativeTime) + public static string Timestamp(this DateTime time, TimestampFormat format = TimestampFormat.RelativeTime) => Timestamp(time.ToUniversalTime() - DateTime.UtcNow, format); /// /// Creates bold text. /// /// Text to embolden. /// Formatted text. - public static string Bold(string content) + public static string Bold(this string content) => $"**{content}**"; /// /// Creates italicized text. /// /// Text to italicize. /// Formatted text. - public static string Italic(string content) + public static string Italic(this string content) => $"*{content}*"; /// /// Creates spoiler from text. /// /// Text to spoiler. /// Formatted text. - public static string Spoiler(string content) + public static string Spoiler(this string content) => $"||{content}||"; /// /// Creates underlined text. /// /// Text to underline. /// Formatted text. - public static string Underline(string content) + public static string Underline(this string content) => $"__{content}__"; /// /// Creates strikethrough text. /// /// Text to strikethrough. /// Formatted text. - public static string Strike(string content) + public static string Strike(this string content) => $"~~{content}~~"; /// /// Creates a URL that won't create a link preview. /// /// Url to prevent from being previewed. /// Formatted url. - public static string EmbedlessUrl(Uri url) + public static string EmbedlessUrl(this Uri url) => $"<{url}>"; /// /// Creates a masked link. This link will display as specified text, and alternatively provided alt text. This can only be used in embeds. /// /// Text to display the link as. /// Url that the link will lead to. /// Alt text to display on hover. /// Formatted url. - public static string MaskedUrl(string text, Uri url, string altText = "") + public static string MaskedUrl(this string text, Uri url, string altText = "") => $"[{text}]({url}{(!string.IsNullOrWhiteSpace(altText) ? $" \"{altText}\"" : "")})"; /// /// Escapes all markdown formatting from specified text. /// /// Text to sanitize. /// Sanitized text. - public static string Sanitize(string text) + public static string Sanitize(this string text) => s_mdSanitizeRegex.Replace(text, m => $"\\{m.Groups[1].Value}"); /// /// Removes all markdown formatting from specified text. /// /// Text to strip of formatting. /// Formatting-stripped text. - public static string Strip(string text) + public static string Strip(this string text) => s_mdStripRegex.Replace(text, m => string.Empty); /// /// Creates a mention for specified user or member. Can optionally specify to resolve nicknames. /// /// User to create mention for. /// Whether the mention should resolve nicknames or not. /// Formatted mention. - public static string Mention(DiscordUser user, bool nickname = false) + public static string Mention(this DiscordUser user, bool nickname = false) => nickname ? $"<@!{user.Id.ToString(CultureInfo.InvariantCulture)}>" : $"<@{user.Id.ToString(CultureInfo.InvariantCulture)}>"; /// /// Creates a mention for specified channel. /// /// Channel to mention. /// Formatted mention. - public static string Mention(DiscordChannel channel) + public static string Mention(this DiscordChannel channel) => $"<#{channel.Id.ToString(CultureInfo.InvariantCulture)}>"; /// /// Creates a mention for specified role. /// /// Role to mention. /// Formatted mention. - public static string Mention(DiscordRole role) + public static string Mention(this DiscordRole role) => $"<@&{role.Id.ToString(CultureInfo.InvariantCulture)}>"; /// /// Creates a custom emoji string. /// /// Emoji to display. /// Formatted emoji. - public static string Emoji(DiscordEmoji emoji) + public static string Emoji(this DiscordEmoji emoji) => $"<:{emoji.Name}:{emoji.Id.ToString(CultureInfo.InvariantCulture)}>"; /// /// Creates a url for using attachments in embeds. This can only be used as an Image URL, Thumbnail URL, Author icon URL or Footer icon URL. /// /// Name of attached image to display /// - public static string AttachedImageUrl(string filename) + public static string AttachedImageUrl(this string filename) => $"attachment://{filename}"; }