Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
<http://github.github.com/github-flavored-markdown/> with support for
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -4324,6 +4352,7 @@ def test(self, text):
Alerts.register()
Breaks.register()
CodeFriendly.register()
Emojis.register()
FencedCodeBlocks.register()
Latex.register()
LinkPatterns.register()
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions test/tm-cases/emojis.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Hello world 😄</p>
1 change: 1 addition & 0 deletions test/tm-cases/emojis.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"extras": ["emojis"]}
1 change: 1 addition & 0 deletions test/tm-cases/emojis.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
emoji
1 change: 1 addition & 0 deletions test/tm-cases/emojis.text
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world :smile:
Loading