From 7aef03c957db007b401b6691a5c799358dafc71f Mon Sep 17 00:00:00 2001 From: jack1142 <6032823+jack1142@users.noreply.github.com> Date: Mon, 31 Aug 2020 22:27:14 +0200 Subject: [PATCH] Support code blocks (#146) LET USERS BE THE TESTERS! CHAOS! BLOOD FOR THE BLOOD GOD! --- latex/latex.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/latex/latex.py b/latex/latex.py index df0cbab..9616839 100644 --- a/latex/latex.py +++ b/latex/latex.py @@ -8,6 +8,8 @@ from redbot.core import commands log = logging.getLogger("red.aikaterna.latex") +START_CODE_BLOCK_RE = re.compile(r"^((```(la)?tex)(?=\s)|(```))") + class Latex(commands.Cog): """LaTeX expressions via an image.""" @@ -20,11 +22,21 @@ class Latex(commands.Cog): self.bot = bot self.session = aiohttp.ClientSession() + @staticmethod + def cleanup_code_block(content): + # remove ```latex\n```/```tex\n```/`````` + if content.starswith("```") and content.endswith("```"): + return START_CODE_BLOCK_RE.sub("", content)[:-3] + + # remove `foo` + return content.strip("` \n") + @commands.guild_only() @commands.command() async def latex(self, ctx, *, equation): """Takes a LaTeX expression and makes it pretty.""" base_url = "https://latex.codecogs.com/gif.latex?%5Cbg_white%20%5CLARGE%20" + equation = self.cleanup_code_block(equation) url = f"{base_url}{equation}" try: