From b2cf11fe9da7a7af9da7c868a9fa73a4f37a94b2 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Sun, 28 May 2017 23:18:54 -0400 Subject: [PATCH] [commands] Add escape_markdown parameter for clean_content. --- discord/ext/commands/converter.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/discord/ext/commands/converter.py b/discord/ext/commands/converter.py index 62aa8e81d..b109765bb 100644 --- a/discord/ext/commands/converter.py +++ b/discord/ext/commands/converter.py @@ -368,10 +368,13 @@ class clean_content(Converter): Whether to clean channel mentions. use_nicknames: bool Whether to use nicknames when transforming mentions. + escape_markdown: bool + Whether to also escape special markdown characters. """ - def __init__(self, *, fix_channel_mentions=False, use_nicknames=True): + def __init__(self, *, fix_channel_mentions=False, use_nicknames=True, escape_markdown=False): self.fix_channel_mentions = fix_channel_mentions self.use_nicknames = use_nicknames + self.escape_markdown = escape_markdown @asyncio.coroutine def convert(self, ctx, argument): @@ -416,6 +419,18 @@ class clean_content(Converter): pattern = re.compile('|'.join(transformations.keys())) result = pattern.sub(repl, argument) + if self.escape_markdown: + transformations = { + re.escape(c): '\\' + c + for c in ('*', '`', '_', '~', '\\') + } + + def replace(obj): + return transformations.get(re.escape(obj.group(0)), '') + + pattern = re.compile('|'.join(transformations.keys())) + result = pattern.sub(replace, result) + transformations = { '@everyone': '@\u200beveryone', '@here': '@\u200bhere'