Skip to content

Commit ee81a6b

Browse files
Merge pull request #710 from Crozzers/emoji-support
Add emoji support (#709)
2 parents 28d94df + 4b8e187 commit ee81a6b

8 files changed

Lines changed: 36 additions & 1 deletion

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- [pull #700] Fix XSS from code spans in image alt text (#699)
99
- [pull #701] Allow boolean attribute syntax in `markdown-in-html` extra
1010
- [pull #704] Fix XSS from smuggling spans into image attributes (#702, #703)
11+
- [pull #710] Add emoji support (#709)
1112

1213

1314
## python-markdown2 2.5.5

lib/markdown2.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
* break-on-newline: Alias for the on_newline option in the breaks extra.
4949
* code-friendly: Disable _ and __ for em and strong.
5050
* cuddled-lists: Allow lists to be cuddled to the preceding paragraph.
51+
* emojis: add emoji support using emoji codes
5152
* fenced-code-blocks: Allows a code block to not have to be indented
5253
by fencing it with '```' on a line before and after. Based on
5354
<http://github.github.com/github-flavored-markdown/> with support for
@@ -3503,6 +3504,33 @@ def test(self, text: str):
35033504
)
35043505

35053506

3507+
class Emojis(Extra):
3508+
'''
3509+
Enable emoji support in markdown text using the [emoji library](https://github.com/carpedm20/emoji)
3510+
'''
3511+
name = 'emojis'
3512+
order = (), (Stage.PARAGRAPHS,)
3513+
3514+
def __init__(self, md: Markdown, options: Optional[dict]):
3515+
super().__init__(md, options)
3516+
self.options.setdefault('language', 'alias')
3517+
3518+
def run(self, text):
3519+
try:
3520+
import emoji
3521+
except ImportError:
3522+
raise ImportError('the "emoji" extra requires the "emoji" package to be installed')
3523+
3524+
if self.options:
3525+
return emoji.emojize(text, **self.options)
3526+
return emoji.emojize(text)
3527+
3528+
def test(self, text):
3529+
# emoji identifiers can have all sorts of chars (eg: `:A_button_(blood_type):`)
3530+
# but they all start and end with a colon and don't have spaces in as far as I can see
3531+
return re.search(r':[\S]+:', text)
3532+
3533+
35063534
class FencedCodeBlocks(Extra):
35073535
'''
35083536
Allows a code block to not have to be indented
@@ -4324,6 +4352,7 @@ def test(self, text):
43244352
Alerts.register()
43254353
Breaks.register()
43264354
CodeFriendly.register()
4355+
Emojis.register()
43274356
FencedCodeBlocks.register()
43284357
Latex.register()
43294358
LinkPatterns.register()

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"code_syntax_highlighting": ["pygments>=2.7.3"],
3434
"wavedrom": ["wavedrom"],
3535
"latex": ['latex2mathml; python_version>="3.8.1"'],
36+
"emojis": ["emoji"]
3637
}
3738
# nested listcomp to combine all optional extras into convenient "all" option
3839
extras_require["all"] = [i for v in tuple(extras_require.values()) for i in v]

test/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def setup():
3535
setup()
3636
default_tags = []
3737
warnings = []
38-
for extra_lib in ('pygments', 'wavedrom', 'latex2mathml'):
38+
for extra_lib in ('pygments', 'wavedrom', 'latex2mathml', 'emoji'):
3939
try:
4040
mod = importlib.import_module(extra_lib)
4141
except ImportError:

test/tm-cases/emojis.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>Hello world 😄</p>

test/tm-cases/emojis.opts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"extras": ["emojis"]}

test/tm-cases/emojis.tags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
emoji

test/tm-cases/emojis.text

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world :smile:

0 commit comments

Comments
 (0)