From 5ec29101eb231748c2c492759a41606ffb03287c Mon Sep 17 00:00:00 2001 From: Alex Wilson Date: Fri, 24 Jun 2016 14:03:56 -0400 Subject: [PATCH] Add unicode emoji support to bugdown. Fixes half of #1011. --- zerver/lib/bugdown/__init__.py | 20 ++++++++++++++++++++ zerver/tests/test_bugdown.py | 9 +++++++++ 2 files changed, 29 insertions(+) diff --git a/zerver/lib/bugdown/__init__.py b/zerver/lib/bugdown/__init__.py index 02b72b0538..3fb9c9c67f 100644 --- a/zerver/lib/bugdown/__init__.py +++ b/zerver/lib/bugdown/__init__.py @@ -560,12 +560,19 @@ class Avatar(markdown.inlinepatterns.Pattern): if settings.VOYAGER: path_to_emoji = os.path.join(os.path.dirname(__file__), '..', '..', '..', 'prod-static', 'serve', 'third', 'gemoji', 'images', 'emoji', '*.png') + path_to_unicode_emoji = os.path.join(os.path.dirname(__file__), '..', '..', '..', + 'prod-static', 'serve', 'third', 'gemoji', 'images', + 'emoji', 'unicode', '*.png') else: path_to_emoji = os.path.join(os.path.dirname(__file__), '..', '..', '..', # This should be the root 'static', 'third', 'gemoji', 'images', 'emoji', '*.png') + path_to_unicode_emoji = os.path.join(os.path.dirname(__file__), '..', '..', '..', + # This should be the root + 'static', 'third', 'gemoji', 'images', 'emoji', 'unicode', '*.png') emoji_list = [os.path.splitext(os.path.basename(fn))[0] for fn in glob.glob(path_to_emoji)] +unicode_emoji_list = [os.path.splitext(os.path.basename(fn))[0] for fn in glob.glob(path_to_unicode_emoji)] def make_emoji(emoji_name, src, display_string): @@ -577,6 +584,17 @@ def make_emoji(emoji_name, src, display_string): elt.set("title", display_string) return elt +class UnicodeEmoji(markdown.inlinepatterns.Pattern): + def handleMatch(self, match): + # type: (Match[text_type]) -> Optional[Element] + orig_syntax = match.group('syntax') + name = hex(ord(orig_syntax))[2:] + if name in unicode_emoji_list: + src = '/static/third/gemoji/images/emoji/unicode/%s.png' % (name) + return make_emoji(name, src, orig_syntax) + else: + return None + class Emoji(markdown.inlinepatterns.Pattern): def handleMatch(self, match): # type: (Match[text_type]) -> Optional[Element] @@ -940,6 +958,8 @@ class Bugdown(markdown.Extension): '_begin') md.inlinePatterns.add('usermention', UserMentionPattern(mention.find_mentions), '>backtick') md.inlinePatterns.add('emoji', Emoji(r'(?:[^:\s]+:)(?!\w)'), '_end') + md.inlinePatterns.add('unicodeemoji', UnicodeEmoji(ur'(?[\U0001F300-\U0001F64F\U0001F680-\U0001F6FF\u2600-\u26FF\u2700-\u27BF])(?!\w)'), '_end') + md.inlinePatterns.add('link', AtomicLinkPattern(markdown.inlinepatterns.LINK_RE, md), '>backtick') for (pattern, format_string) in self.getConfig("realm_filters"): diff --git a/zerver/tests/test_bugdown.py b/zerver/tests/test_bugdown.py index 2f2cb801d8..1c04366c26 100644 --- a/zerver/tests/test_bugdown.py +++ b/zerver/tests/test_bugdown.py @@ -331,6 +331,15 @@ class BugdownTest(TestCase): converted = bugdown.convert(":test:", "zulip.com", msg) self.assertEqual(converted, '

:test:

') + def test_unicode_emoji(self): + msg = u'\u2615' # ☕ + converted = bugdown_convert(msg) + self.assertEqual(converted, u'

\u2615

') + + msg = u'\u2615\u2615' # ☕☕ + converted = bugdown_convert(msg) + self.assertEqual(converted, u'

\u2615\u2615

') + def test_realm_patterns(self): realm = get_realm('zulip.com') RealmFilter(realm=realm, pattern=r"#(?P[0-9]{2,8})",