/// <summary> /// This will replace shortnames with their ascii equivalent, e.g. :wink: -> ;). /// This is useful for systems that don't support unicode or images. /// </summary> /// <param name="str"></param> /// <returns>A string with ascii replacements.</returns> public static string ShortnameToAscii(string str) { if (str != null) { str = Regex.Replace(str, IGNORE_PATTERN + "|" + SHORTNAME_PATTERN, match => { var shortname = match.Value; // check if the emoji exists in our dictionary if (SHORTNAMES.ContainsKey(shortname)) { var emoji = GetEmoji(SHORTNAMES[shortname]); if (emoji != null) { var ascii = emoji[ASCII_INDEX]; if (ascii != null) { return(ascii); } } } // we didn't find a replacement so just return the entire match return(match.Value); }, RegexOptions.IgnoreCase); } return(str); }
/// <summary> /// Takes input containing emoji shortnames and converts it to emoji images. /// </summary> /// <param name="str"></param> /// <param name="ascii"><c>true</c> to also convert ascii emoji to images.</param> /// <param name="unicodeAlt"><c>true</c> to use the unicode char instead of the shortname as the alt attribute (makes copy and pasting the resulting text better).</param> /// <param name="sprite"><c>true</c> to enable sprite mode instead of individual images.</param> /// <returns>A string with appropriate html for rendering emoji.</returns> public static string ShortnameToImage(string str, bool ascii = false, bool unicodeAlt = true, bool sprite = false) { if (ascii) { str = AsciiToShortname(str); } if (str != null) { str = Regex.Replace(str, IGNORE_PATTERN + "|" + SHORTNAME_PATTERN, match => { var shortname = match.Value; // check if the emoji exists in our dictionary if (SHORTNAMES.ContainsKey(shortname)) { var codepoint = SHORTNAMES[shortname]; var emoji = GetEmoji(codepoint); if (emoji != null) { string title = ImageTitleTag ? $@" title=""{shortname}""" : ""; string alt = unicodeAlt ? ToUnicode(codepoint) : shortname; if (sprite) { string category = codepoint.IndexOf("-1f3f") >= 0 ? "diversity" : emoji[CATEGORY_INDEX]; return($@"<span class=""emojione emojione-{EmojiSize}-{category} _{codepoint}""{title}>{alt}</span>"); } else { var path = DefaultPath != ImagePath ? ImagePath : DefaultPath + EmojiSize + "/"; return($@"<img class=""emojione"" alt=""{alt}""{title} src=""{path}{codepoint}{FileExtension}"" />"); } } } // we didn't find a replacement so just return the entire match return(match.Value); }, RegexOptions.IgnoreCase); } return(str); }
/// <summary> /// Converts shortname emojis to unicode, useful for sending emojis back to mobile devices. /// </summary> /// <param name="str">The input string</param> /// <param name="ascii"><c>true</c> to also convert ascii emoji in the inpur string to unicode.</param> /// <returns>A string with unicode replacements</returns> public static string ShortnameToUnicode(string str, bool ascii = false) { if (str != null) { str = Regex.Replace(str, IGNORE_PATTERN + "|" + SHORTNAME_PATTERN, match => { var shortname = match.Value; // check if the emoji exists in our dictionary if (SHORTNAMES.ContainsKey(shortname)) { // convert codepoint to unicode char return(ToUnicode(SHORTNAMES[shortname])); } // we didn't find a replacement so just return the entire match return(match.Value); }, RegexOptions.IgnoreCase); } if (ascii) { str = AsciiToUnicode(str); } return(str); }