From 7acea29cdbda5c7069659a1fcf7108707f5b69e6 Mon Sep 17 00:00:00 2001 From: Michael H Date: Tue, 14 Aug 2018 02:31:42 -0400 Subject: [PATCH] [Dev] Friendlier code-block strip for [p]eval(#2017) not relying on newlines where they aren't required by discord, retaining py highlight support. Resolves #1928. --- redbot/core/dev_commands.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/redbot/core/dev_commands.py b/redbot/core/dev_commands.py index 739b001f8..09868f92a 100644 --- a/redbot/core/dev_commands.py +++ b/redbot/core/dev_commands.py @@ -3,6 +3,7 @@ import inspect import io import textwrap import traceback +import re from contextlib import redirect_stdout from copy import copy @@ -21,6 +22,8 @@ https://github.com/Rapptz/RoboDanny/blob/master/cogs/repl.py _ = Translator("Dev", __file__) +START_CODE_BLOCK_RE = re.compile(r"^((```py)(?=\s)|(```))") + class Dev: """Various development focused utilities.""" @@ -34,7 +37,7 @@ class Dev: """Automatically removes code blocks from the code.""" # remove ```py\n``` if content.startswith("```") and content.endswith("```"): - return "\n".join(content.split("\n")[1:-1]) + return START_CODE_BLOCK_RE.sub("", content)[:-3] # remove `foo` return content.strip("` \n")