Skip to content

Commit

Permalink
Clarify emoji terminology (emoji "shortcode")
Browse files Browse the repository at this point in the history
  • Loading branch information
mlaily committed Jan 16, 2020
1 parent aecdf21 commit 8702318
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 54 deletions.
8 changes: 4 additions & 4 deletions src/Markdig.Tests/Specs/EmojiSpecs.generated.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated: 2019-04-15 05:20:50
// Generated: 2020-01-13 21:08:58

// --------------------------------
// Emoji
Expand All @@ -18,7 +18,7 @@ public class TestExtensionsEmoji
//
// ## Emoji
//
// Emoji and smiley can be converted to their respective unicode characters:
// Emoji shortcodes and smileys can be converted to their respective unicode characters:
[Test]
public void ExtensionsEmoji_Example001()
{
Expand Down Expand Up @@ -52,7 +52,7 @@ public void ExtensionsEmoji_Example002()
TestParser.TestSpec("These are not:) an emoji with a:) x:angry:x", "<p>These are not:) an emoji with a:) x:angry:x</p>", "emojis|advanced+emojis");
}

// Emoji can be followed by close punctuation (or any other characters):
// Emojis can be followed by close punctuation (or any other characters):
[Test]
public void ExtensionsEmoji_Example003()
{
Expand All @@ -69,7 +69,7 @@ public void ExtensionsEmoji_Example003()
TestParser.TestSpec("We all need :), it makes us :muscle:. (and :ok_hand:).", "<p>We all need 😃, it makes us 💪. (and 👌).</p>", "emojis|advanced+emojis");
}

// Sentences can end with Emoji:
// Sentences can end with emojis:
[Test]
public void ExtensionsEmoji_Example004()
{
Expand Down
6 changes: 3 additions & 3 deletions src/Markdig.Tests/Specs/EmojiSpecs.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This section describes the different extensions supported:

## Emoji

Emoji and smiley can be converted to their respective unicode characters:
Emoji shortcodes and smileys can be converted to their respective unicode characters:

```````````````````````````````` example
This is a test with a :) and a :angry: smiley
Expand All @@ -20,15 +20,15 @@ These are not:) an emoji with a:) x:angry:x
<p>These are not:) an emoji with a:) x:angry:x</p>
````````````````````````````````

Emoji can be followed by close punctuation (or any other characters):
Emojis can be followed by close punctuation (or any other characters):

```````````````````````````````` example
We all need :), it makes us :muscle:. (and :ok_hand:).
.
<p>We all need 😃, it makes us 💪. (and 👌).</p>
````````````````````````````````

Sentences can end with Emoji:
Sentences can end with emojis:

```````````````````````````````` example
This is a sentence :ok_hand:
Expand Down
8 changes: 4 additions & 4 deletions src/Markdig.Tests/TestCustomEmojis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public void TestCustomSmiley(string input, string expected)
[TestCase(":/", "<p>😕</p>\n")] // default smiley still works
public void TestOverrideDefaultWithCustomEmoji(string input, string expected)
{
var emojiToUnicode = EmojiMapping.GetDefaultEmojiToUnicode();
var smileyToEmoji = EmojiMapping.GetDefaultSmileyToEmoji();
var emojiToUnicode = EmojiMapping.GetDefaultEmojiShortcodeToUnicode();
var smileyToEmoji = EmojiMapping.GetDefaultSmileyToEmojiShortcode();

emojiToUnicode[":smiley:"] = "♥";

Expand All @@ -80,8 +80,8 @@ public void TestOverrideDefaultWithCustomEmoji(string input, string expected)
[TestCase(":/", "<p>😕</p>\n")] // default smiley still works
public void TestOverrideDefaultWithCustomSmiley(string input, string expected)
{
var emojiToUnicode = EmojiMapping.GetDefaultEmojiToUnicode();
var smileyToEmoji = EmojiMapping.GetDefaultSmileyToEmoji();
var emojiToUnicode = EmojiMapping.GetDefaultEmojiShortcodeToUnicode();
var smileyToEmoji = EmojiMapping.GetDefaultSmileyToEmojiShortcode();

emojiToUnicode[":testheart:"] = "♥";
smileyToEmoji["hello"] = ":testheart:";
Expand Down
2 changes: 1 addition & 1 deletion src/Markdig/Extensions/Emoji/EmojiExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace Markdig.Extensions.Emoji
{
/// <summary>
/// Extension to allow emoji and smiley replacement.
/// Extension to allow emoji shortcodes and smileys replacement.
/// </summary>
/// <seealso cref="Markdig.IMarkdownExtension" />
public class EmojiExtension : IMarkdownExtension
Expand Down
6 changes: 3 additions & 3 deletions src/Markdig/Extensions/Emoji/EmojiInline.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

Expand All @@ -8,7 +8,7 @@
namespace Markdig.Extensions.Emoji
{
/// <summary>
/// An emoji inline
/// An emoji inline.
/// </summary>
/// <seealso cref="Markdig.Syntax.Inlines.Inline" />
public class EmojiInline : LiteralInline
Expand All @@ -32,7 +32,7 @@ public EmojiInline(string content)
}

/// <summary>
/// Gets or sets the original match string (either an emoji or a text smiley)
/// Gets or sets the original match string (either an emoji shortcode or a text smiley)
/// </summary>
public string Match { get; set; }
}
Expand Down
64 changes: 32 additions & 32 deletions src/Markdig/Extensions/Emoji/EmojiMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@
namespace Markdig.Extensions.Emoji
{
/// <summary>
/// An emoji and smiley mapping, to be used by <see cref="EmojiParser"/>.
/// An emoji shortcodes and smileys mapping, to be used by <see cref="EmojiParser"/>.
/// </summary>
public class EmojiMapping
{
/// <summary>
/// The default emojis and smileys mapping.
/// The default emoji shortcodes and smileys mapping.
/// </summary>
public static readonly EmojiMapping DefaultEmojiAndSmileyMapping = new EmojiMapping();
public static readonly EmojiMapping DefaultEmojisAndSmileysMapping = new EmojiMapping();

/// <summary>
/// The default emojis mapping, without smileys.
/// The default emoji shortcodes mapping, without smileys.
/// </summary>
public static readonly EmojiMapping DefaultEmojiOnlyMapping = new EmojiMapping(enableSmiley: false);

private static readonly Dictionary<string, string> _emptyDictionary = new Dictionary<string, string>();
public static readonly EmojiMapping DefaultEmojisOnlyMapping = new EmojiMapping(enableSmileys: false);

internal CompactPrefixTree<string> PrefixTree { get; }

Expand All @@ -32,10 +30,10 @@ public class EmojiMapping
#region Emojis and Smileys

/// <summary>
/// Returns a new instance of the default emoji to unicode dictionary.
/// Returns a new instance of the default emoji shortcode to emoji unicode dictionary.
/// It can be used to create a customized <see cref="EmojiMapping"/>.
/// </summary>
public static IDictionary<string, string> GetDefaultEmojiToUnicode()
public static IDictionary<string, string> GetDefaultEmojiShortcodeToUnicode()
{
return new Dictionary<string, string>()
{
Expand Down Expand Up @@ -1649,10 +1647,10 @@ public static IDictionary<string, string> GetDefaultEmojiToUnicode()
}

/// <summary>
/// Gets a new instance of the default smiley to emoji dictionary.
/// Gets a new instance of the default smiley to emoji shortcode dictionary.
/// It can be used to create a customized <see cref="EmojiMapping"/>.
/// </summary>
public static IDictionary<string, string> GetDefaultSmileyToEmoji()
public static IDictionary<string, string> GetDefaultSmileyToEmojiShortcode()
{
return new Dictionary<string, string>()
{
Expand Down Expand Up @@ -1736,25 +1734,27 @@ public static IDictionary<string, string> GetDefaultSmileyToEmoji()
#endregion

/// <summary>
/// Constructs a mapping for the default emojis and smileys.
/// Constructs a mapping for the default emoji shortcodes and smileys.
/// </summary>
public EmojiMapping(bool enableSmiley = true)
: this(GetDefaultEmojiToUnicode(), enableSmiley ? GetDefaultSmileyToEmoji() : _emptyDictionary) { }
public EmojiMapping(bool enableSmileys = true)
: this(GetDefaultEmojiShortcodeToUnicode(),
enableSmileys ? GetDefaultSmileyToEmojiShortcode() : new Dictionary<string, string>())
{ }

/// <summary>
/// Constructs a mapping from a dictionary of emojis to unicode, and a dictionary of smileys to emojis.
/// Constructs a mapping from a dictionary of emoji shortcodes to unicode, and a dictionary of smileys to emoji shortcodes.
/// </summary>
public EmojiMapping(IDictionary<string, string> emojiToUnicode, IDictionary<string, string> smileyToEmoji)
public EmojiMapping(IDictionary<string, string> shortcodeToUnicode, IDictionary<string, string> smileyToShortcode)
{
if (emojiToUnicode == null)
throw new ArgumentNullException(nameof(emojiToUnicode));
if (shortcodeToUnicode == null)
throw new ArgumentNullException(nameof(shortcodeToUnicode));

if (smileyToEmoji == null)
throw new ArgumentNullException(nameof(smileyToEmoji));
if (smileyToShortcode == null)
throw new ArgumentNullException(nameof(smileyToShortcode));

// Build Emoji and Smiley CompactPrefixTree
// Build emojis and smileys CompactPrefixTree

int jointCount = emojiToUnicode.Count + smileyToEmoji.Count;
int jointCount = shortcodeToUnicode.Count + smileyToShortcode.Count;

// Count * 2 seems to be a good fit for the data set
PrefixTree = new CompactPrefixTree<string>(jointCount, jointCount * 2, jointCount * 2);
Expand All @@ -1765,27 +1765,27 @@ public EmojiMapping(IDictionary<string, string> emojiToUnicode, IDictionary<stri

var firstChars = new HashSet<char>();

foreach (var emoji in emojiToUnicode)
foreach (var shortcode in shortcodeToUnicode)
{
if (string.IsNullOrEmpty(emoji.Key))
throw new ArgumentException("The dictionaries cannot contain null or empty keys", nameof(emojiToUnicode));
if (string.IsNullOrEmpty(shortcode.Key))
throw new ArgumentException("The dictionaries cannot contain null or empty keys", nameof(shortcodeToUnicode));

firstChars.Add(emoji.Key[0]);
PrefixTree.Add(emoji);
firstChars.Add(shortcode.Key[0]);
PrefixTree.Add(shortcode);
}

foreach (var smiley in smileyToEmoji)
foreach (var smiley in smileyToShortcode)
{
if (string.IsNullOrEmpty(smiley.Key))
throw new ArgumentException("The dictionaries cannot contain null or empty keys", nameof(smileyToEmoji));
throw new ArgumentException("The dictionaries cannot contain null or empty keys", nameof(smileyToShortcode));

if (!emojiToUnicode.TryGetValue(smiley.Value, out string unicode))
throw new ArgumentException(string.Format("Invalid smiley target: {0} is not present in the emoji dictionary", smiley.Value));
if (!shortcodeToUnicode.TryGetValue(smiley.Value, out string unicode))
throw new ArgumentException(string.Format("Invalid smiley target: {0} is not present in the emoji shortcodes dictionary", smiley.Value));

firstChars.Add(smiley.Key[0]);

if (!PrefixTree.TryAdd(smiley.Key, unicode))
throw new ArgumentException(string.Format("Smiley {0} is already present in the Emoji dictionary", smiley.Key));
throw new ArgumentException(string.Format("Smiley {0} is already present in the emoji mapping", smiley.Key));
}

OpeningCharacters = new List<char>(firstChars).ToArray();
Expand Down
4 changes: 2 additions & 2 deletions src/Markdig/Extensions/Emoji/EmojiParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Markdig.Extensions.Emoji
{
/// <summary>
/// The inline parser used for emoji.
/// The inline parser used for emojis.
/// </summary>
/// <seealso cref="InlineParser" />
public class EmojiParser : InlineParser
Expand All @@ -33,7 +33,7 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
return false;
}

// Try to match an emoji
// Try to match an emoji shortcode or smiley
if (!_emojiMapping.PrefixTree.TryMatchLongest(slice.Text, slice.Start, slice.Length, out KeyValuePair<string, string> match))
{
return false;
Expand Down
10 changes: 5 additions & 5 deletions src/Markdig/MarkdownExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,23 +421,23 @@ public static MarkdownPipelineBuilder UseGenericAttributes(this MarkdownPipeline
}

/// <summary>
/// Uses the emoji and smiley extension.
/// Uses the emojis and smileys extension.
/// </summary>
/// <param name="pipeline">The pipeline.</param>
/// <param name="enableSmiley">Enable smiley in addition to Emoji, <c>true</c> by default.</param>
/// <param name="enableSmileys">Enable smileys in addition to emoji shortcodes, <c>true</c> by default.</param>
/// <returns>The modified pipeline</returns>
public static MarkdownPipelineBuilder UseEmojiAndSmiley(this MarkdownPipelineBuilder pipeline, bool enableSmiley = true)
public static MarkdownPipelineBuilder UseEmojiAndSmiley(this MarkdownPipelineBuilder pipeline, bool enableSmileys = true)
{
if (!pipeline.Extensions.Contains<EmojiExtension>())
{
var emojiMapping = enableSmiley ? EmojiMapping.DefaultEmojiAndSmileyMapping : EmojiMapping.DefaultEmojiOnlyMapping;
var emojiMapping = enableSmileys ? EmojiMapping.DefaultEmojisAndSmileysMapping : EmojiMapping.DefaultEmojisOnlyMapping;
pipeline.Extensions.Add(new EmojiExtension(emojiMapping));
}
return pipeline;
}

/// <summary>
/// Uses the emoji and smiley extension.
/// Uses the emojis and smileys extension.
/// </summary>
/// <param name="pipeline">The pipeline.</param>
/// <param name="customEmojiMapping">Enable customization of the emojis and smileys mapping.</param>
Expand Down

0 comments on commit 8702318

Please sign in to comment.