diff --git a/CHANGES.md b/CHANGES.md
index 718fb9ed..157e8341 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -8,6 +8,7 @@
- [pull #700] Fix XSS from code spans in image alt text (#699)
- [pull #701] Allow boolean attribute syntax in `markdown-in-html` extra
- [pull #704] Fix XSS from smuggling spans into image attributes (#702, #703)
+- [pull #710] Add emoji support (#709)
## python-markdown2 2.5.5
diff --git a/lib/markdown2.py b/lib/markdown2.py
index dc698970..8dd1c48e 100755
--- a/lib/markdown2.py
+++ b/lib/markdown2.py
@@ -48,6 +48,7 @@
* break-on-newline: Alias for the on_newline option in the breaks extra.
* code-friendly: Disable _ and __ for em and strong.
* cuddled-lists: Allow lists to be cuddled to the preceding paragraph.
+* emojis: add emoji support using emoji codes
* fenced-code-blocks: Allows a code block to not have to be indented
by fencing it with '```' on a line before and after. Based on
with support for
@@ -3503,6 +3504,33 @@ def test(self, text: str):
)
+class Emojis(Extra):
+ '''
+ Enable emoji support in markdown text using the [emoji library](https://github.com/carpedm20/emoji)
+ '''
+ name = 'emojis'
+ order = (), (Stage.PARAGRAPHS,)
+
+ def __init__(self, md: Markdown, options: Optional[dict]):
+ super().__init__(md, options)
+ self.options.setdefault('language', 'alias')
+
+ def run(self, text):
+ try:
+ import emoji
+ except ImportError:
+ raise ImportError('the "emoji" extra requires the "emoji" package to be installed')
+
+ if self.options:
+ return emoji.emojize(text, **self.options)
+ return emoji.emojize(text)
+
+ def test(self, text):
+ # emoji identifiers can have all sorts of chars (eg: `:A_button_(blood_type):`)
+ # but they all start and end with a colon and don't have spaces in as far as I can see
+ return re.search(r':[\S]+:', text)
+
+
class FencedCodeBlocks(Extra):
'''
Allows a code block to not have to be indented
@@ -4324,6 +4352,7 @@ def test(self, text):
Alerts.register()
Breaks.register()
CodeFriendly.register()
+Emojis.register()
FencedCodeBlocks.register()
Latex.register()
LinkPatterns.register()
diff --git a/setup.py b/setup.py
index 05dba326..bb8da93d 100755
--- a/setup.py
+++ b/setup.py
@@ -33,6 +33,7 @@
"code_syntax_highlighting": ["pygments>=2.7.3"],
"wavedrom": ["wavedrom"],
"latex": ['latex2mathml; python_version>="3.8.1"'],
+ "emojis": ["emoji"]
}
# nested listcomp to combine all optional extras into convenient "all" option
extras_require["all"] = [i for v in tuple(extras_require.values()) for i in v]
diff --git a/test/test.py b/test/test.py
index 995db47a..2874ccc6 100755
--- a/test/test.py
+++ b/test/test.py
@@ -35,7 +35,7 @@ def setup():
setup()
default_tags = []
warnings = []
- for extra_lib in ('pygments', 'wavedrom', 'latex2mathml'):
+ for extra_lib in ('pygments', 'wavedrom', 'latex2mathml', 'emoji'):
try:
mod = importlib.import_module(extra_lib)
except ImportError:
diff --git a/test/tm-cases/emojis.html b/test/tm-cases/emojis.html
new file mode 100644
index 00000000..e07510cf
--- /dev/null
+++ b/test/tm-cases/emojis.html
@@ -0,0 +1 @@
+
Hello world 😄
diff --git a/test/tm-cases/emojis.opts b/test/tm-cases/emojis.opts
new file mode 100644
index 00000000..d9e3951f
--- /dev/null
+++ b/test/tm-cases/emojis.opts
@@ -0,0 +1 @@
+{"extras": ["emojis"]}
\ No newline at end of file
diff --git a/test/tm-cases/emojis.tags b/test/tm-cases/emojis.tags
new file mode 100644
index 00000000..f8d6d82b
--- /dev/null
+++ b/test/tm-cases/emojis.tags
@@ -0,0 +1 @@
+emoji
\ No newline at end of file
diff --git a/test/tm-cases/emojis.text b/test/tm-cases/emojis.text
new file mode 100644
index 00000000..6ff60fe0
--- /dev/null
+++ b/test/tm-cases/emojis.text
@@ -0,0 +1 @@
+Hello world :smile:
\ No newline at end of file