Skip to content

Commit

Permalink
Merge pull request #215 from cvzi/fix_delimiters
Browse files Browse the repository at this point in the history
Fix delimiters
  • Loading branch information
TahirJalilov authored Jul 11, 2022
2 parents cf923b2 + dbda4a8 commit c398e0c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
7 changes: 4 additions & 3 deletions emoji/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ def emojize(
Python is fun 👍
>>> print(emoji.emojize("Python is fun :thumbs_up:"))
Python is fun 👍
>>> print(emoji.emojize("Python is fun __thumbs_up__", delimiters = ("__", "__")))
>>> print(emoji.emojize("Python is fun {thumbs_up}", delimiters = ("{", "}")))
Python is fun 👍
>>> print(emoji.emojize("Python is fun :red_heart:", variant="text_type"))
Python is fun ❤
>>> print(emoji.emojize("Python is fun :red_heart:", variant="emoji_type"))
Python is fun ❤️ # red heart, not black heart
:param string: String contains emoji names.
:param delimiters: (optional) Use delimiters other than _DEFAULT_DELIMITER
:param delimiters: (optional) Use delimiters other than _DEFAULT_DELIMITER. Each delimiter
should contain at least one character that is not part of a-zA-Z0-9 and ``_-–&.’”“()!?#*+,/\``
:param variant: (optional) Choose variation selector between "base"(None), VS-15 ("text_type") and VS-16 ("emoji_type")
:param language: Choose language of emoji name: language code 'es', 'de', etc. or 'alias'
to use English aliases
Expand Down Expand Up @@ -78,7 +79,7 @@ def emojize(
language_pack = unicode_codes.get_emoji_unicode_dict(language)

pattern = re.compile(u'(%s[\\w\\-&.’”“()!#*+?–,/]+%s)' %
delimiters, flags=re.UNICODE)
(re.escape(delimiters[0]), re.escape(delimiters[1])), flags=re.UNICODE)

def replace(match):
mg = match.group(1)[len(delimiters[0]):-len(delimiters[1])]
Expand Down
14 changes: 8 additions & 6 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,20 +222,22 @@ def test_demojize_complicated_string():

def test_demojize_delimiters():
for e in [u'\U000026BD', u'\U0001f44d', u'\U0001F3C8']:
for d in [(":", ":"), ("a", "b"), ("!", "!!"), ("123", "456"), (u"😁", u"👌")]:
for d in [(":", ":"), ("}", "}"), ("!$", "!!$"), ("[123", "456]"), (u"😁", u"👌"), ("[", "]")]:
s = emoji.demojize(e, delimiters=d)
assert s.startswith(d[0])
assert s.endswith(d[1])

text = u"Example of a text with an emoji%sin a sentence"
text = u"Example with an emoji%sin a sentence and %s %s %s %s multiple emoji %s%s%s%s%s in a row"
for e in [u'\U000026BD', u'\U0001f44d', u'\U0001F3C8']:
for d in [(":", ":"), ("!", "-!-"), ("-", "-"), (":", "::"), ("::", "::"), (u"😁", u"👌")]:
text_with_unicode = text % e
for d in [(":", ":"), ("{", "}"), ("!$", "$!"), (":", "::"), ("::", "::"), (u"😁", u"👌"), ("[", "]")]:
print("delimiter: %s" % (d, ))
text_with_unicode = text % ((e, ) * 10)
demojized_text = emoji.demojize(text_with_unicode, delimiters=d)
assert text_with_unicode != demojized_text
assert text_with_unicode != demojized_text, (text_with_unicode, demojized_text)
assert e not in demojized_text
assert emoji.emojize(demojized_text, delimiters=d) == text_with_unicode
text_with_emoji = text % emoji.demojize(e, delimiters=d)
de = emoji.demojize(e, delimiters=d)
text_with_emoji = text % ((de, ) * 10)
assert demojized_text == text_with_emoji
assert emoji.emojize(text_with_emoji, delimiters=d) == text_with_unicode

Expand Down

0 comments on commit c398e0c

Please sign in to comment.