mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-04-22 08:44:10 +00:00
Add remove_markdown helper function
This commit is contained in:
parent
4aee7fed5d
commit
31ee3fafc1
@ -676,11 +676,16 @@ class clean_content(Converter):
|
||||
Whether to use nicknames when transforming mentions.
|
||||
escape_markdown: :class:`bool`
|
||||
Whether to also escape special markdown characters.
|
||||
remove_markdown: :class:`bool`
|
||||
Whether to also remove special markdown characters. This option is not supported with ``escape_markdown``
|
||||
|
||||
.. versionadded:: 1.7
|
||||
"""
|
||||
def __init__(self, *, fix_channel_mentions=False, use_nicknames=True, escape_markdown=False):
|
||||
def __init__(self, *, fix_channel_mentions=False, use_nicknames=True, escape_markdown=False, remove_markdown=False):
|
||||
self.fix_channel_mentions = fix_channel_mentions
|
||||
self.use_nicknames = use_nicknames
|
||||
self.escape_markdown = escape_markdown
|
||||
self.remove_markdown = remove_markdown
|
||||
|
||||
async def convert(self, ctx, argument):
|
||||
message = ctx.message
|
||||
@ -731,6 +736,8 @@ class clean_content(Converter):
|
||||
|
||||
if self.escape_markdown:
|
||||
result = discord.utils.escape_markdown(result)
|
||||
elif self.remove_markdown:
|
||||
result = discord.utils.remove_markdown(result)
|
||||
|
||||
# Completely ensure no mentions escape:
|
||||
return discord.utils.escape_mentions(result)
|
||||
|
@ -808,9 +808,9 @@ class Message(Hashable):
|
||||
|
||||
.. note::
|
||||
|
||||
This *does not* escape markdown. If you want to escape
|
||||
markdown then use :func:`utils.escape_markdown` along
|
||||
with this function.
|
||||
This *does not* affect markdown. If you want to escape
|
||||
or remove markdown then use :func:`utils.escape_markdown` or :func:`utils.remove_markdown`
|
||||
respectively, along with this function.
|
||||
"""
|
||||
|
||||
transformations = {
|
||||
|
@ -484,6 +484,43 @@ _MARKDOWN_ESCAPE_COMMON = r'^>(?:>>)?\s|\[.+\]\(.+\)'
|
||||
|
||||
_MARKDOWN_ESCAPE_REGEX = re.compile(r'(?P<markdown>%s|%s)' % (_MARKDOWN_ESCAPE_SUBREGEX, _MARKDOWN_ESCAPE_COMMON), re.MULTILINE)
|
||||
|
||||
_URL_REGEX = r'(?P<url><[^: >]+:\/[^ >]+>|(?:https?|steam):\/\/[^\s<]+[^<.,:;\"\'\]\s])'
|
||||
|
||||
_MARKDOWN_STOCK_REGEX = r'(?P<markdown>[_\\~|\*`]|%s)' % _MARKDOWN_ESCAPE_COMMON
|
||||
|
||||
def remove_markdown(text, *, ignore_links=True):
|
||||
"""A helper function that removes markdown characters.
|
||||
|
||||
.. versionadded:: 1.7
|
||||
|
||||
.. note::
|
||||
This function is not markdown aware and may remove meaning from the original text. For example,
|
||||
if the input contains ``10 * 5`` then it will be converted into ``10 5``.
|
||||
|
||||
Parameters
|
||||
-----------
|
||||
text: :class:`str`
|
||||
The text to remove markdown from.
|
||||
ignore_links: :class:`bool`
|
||||
Whether to leave links alone when removing markdown. For example,
|
||||
if a URL in the text contains characters such as ``_`` then it will
|
||||
be left alone. Defaults to ``True``.
|
||||
|
||||
Returns
|
||||
--------
|
||||
:class:`str`
|
||||
The text with the markdown special characters removed.
|
||||
"""
|
||||
|
||||
def replacement(match):
|
||||
groupdict = match.groupdict()
|
||||
return groupdict.get('url', '')
|
||||
|
||||
regex = _MARKDOWN_STOCK_REGEX
|
||||
if ignore_links:
|
||||
regex = '(?:%s|%s)' % (_URL_REGEX, regex)
|
||||
return re.sub(regex, replacement, text, 0, re.MULTILINE)
|
||||
|
||||
def escape_markdown(text, *, as_needed=False, ignore_links=True):
|
||||
r"""A helper function that escapes Discord's markdown.
|
||||
|
||||
@ -510,7 +547,6 @@ def escape_markdown(text, *, as_needed=False, ignore_links=True):
|
||||
"""
|
||||
|
||||
if not as_needed:
|
||||
url_regex = r'(?P<url><[^: >]+:\/[^ >]+>|(?:https?|steam):\/\/[^\s<]+[^<.,:;\"\'\]\s])'
|
||||
def replacement(match):
|
||||
groupdict = match.groupdict()
|
||||
is_url = groupdict.get('url')
|
||||
@ -518,9 +554,9 @@ def escape_markdown(text, *, as_needed=False, ignore_links=True):
|
||||
return is_url
|
||||
return '\\' + groupdict['markdown']
|
||||
|
||||
regex = r'(?P<markdown>[_\\~|\*`]|%s)' % _MARKDOWN_ESCAPE_COMMON
|
||||
regex = _MARKDOWN_STOCK_REGEX
|
||||
if ignore_links:
|
||||
regex = '(?:%s|%s)' % (url_regex, regex)
|
||||
regex = '(?:%s|%s)' % (_URL_REGEX, regex)
|
||||
return re.sub(regex, replacement, text, 0, re.MULTILINE)
|
||||
else:
|
||||
text = re.sub(r'\\', r'\\\\', text)
|
||||
|
@ -929,6 +929,8 @@ Utility Functions
|
||||
|
||||
.. autofunction:: discord.utils.oauth_url
|
||||
|
||||
.. autofunction:: discord.utils.remove_markdown
|
||||
|
||||
.. autofunction:: discord.utils.escape_markdown
|
||||
|
||||
.. autofunction:: discord.utils.escape_mentions
|
||||
|
Loading…
x
Reference in New Issue
Block a user