2021-03-22 03:35:45 -04:00
|
|
|
"""
|
|
|
|
TeX extension for qrm
|
|
|
|
---
|
2021-03-28 13:57:03 -04:00
|
|
|
Copyright (C) 2021 classabbyamp, 0x5c
|
2021-03-22 03:35:45 -04:00
|
|
|
|
2021-06-26 20:23:55 -04:00
|
|
|
SPDX-License-Identifier: LiLiQ-Rplus-1.1
|
2021-03-22 03:35:45 -04:00
|
|
|
"""
|
|
|
|
|
2021-03-22 04:43:21 -04:00
|
|
|
|
2021-03-22 03:35:45 -04:00
|
|
|
import aiohttp
|
|
|
|
from io import BytesIO
|
|
|
|
from urllib.parse import urljoin
|
|
|
|
|
|
|
|
import discord
|
|
|
|
import discord.ext.commands as commands
|
|
|
|
|
|
|
|
import common as cmn
|
|
|
|
import data.options as opt
|
|
|
|
|
|
|
|
|
|
|
|
class TexCog(commands.Cog):
|
|
|
|
def __init__(self, bot: commands.Bot):
|
|
|
|
self.bot = bot
|
|
|
|
self.session = aiohttp.ClientSession(connector=bot.qrm.connector)
|
|
|
|
with open(cmn.paths.resources / "template.1.tex") as latex_template:
|
|
|
|
self.template = latex_template.read()
|
|
|
|
|
2021-03-28 09:50:51 -04:00
|
|
|
@commands.command(name="tex", aliases=["latex"], category=cmn.Cats.UTILS)
|
2021-03-22 03:35:45 -04:00
|
|
|
async def tex(self, ctx: commands.Context, *, expr: str):
|
2021-04-09 19:39:46 -04:00
|
|
|
"""Renders a LaTeX expression.
|
|
|
|
|
|
|
|
In paragraph mode by default. To render math, add `$` around math expressions.
|
|
|
|
"""
|
2021-03-22 03:35:45 -04:00
|
|
|
payload = {
|
|
|
|
"format": "png",
|
|
|
|
"code": self.template.replace("#CONTENT#", expr),
|
|
|
|
"quality": 50
|
|
|
|
}
|
|
|
|
|
|
|
|
with ctx.typing():
|
|
|
|
# ask rTeX to render our expression
|
2021-03-22 04:43:21 -04:00
|
|
|
async with self.session.post(urljoin(opt.rtex_instance, "api/v2"), json=payload) as r:
|
2021-03-22 03:35:45 -04:00
|
|
|
if r.status != 200:
|
|
|
|
raise cmn.BotHTTPError(r)
|
|
|
|
|
|
|
|
render_result = await r.json()
|
|
|
|
if render_result["status"] != "success":
|
|
|
|
embed = cmn.embed_factory(ctx)
|
|
|
|
embed.title = "LaTeX Rendering Failed!"
|
2021-04-09 19:39:46 -04:00
|
|
|
embed.description = ("Here are some common reasons:\n"
|
|
|
|
"• Did you forget to use math mode? Surround math expressions with `$`,"
|
|
|
|
" like `$x^3$`.\n"
|
|
|
|
"• Are you using a command from a package? It might not be available.\n"
|
|
|
|
"• Are you including the document headers? We already did that for you.")
|
2021-03-22 03:35:45 -04:00
|
|
|
embed.colour = cmn.colours.bad
|
|
|
|
await ctx.send(embed=embed)
|
|
|
|
return
|
|
|
|
|
|
|
|
# if rendering went well, download the file given in the response
|
2021-03-22 04:43:21 -04:00
|
|
|
async with self.session.get(urljoin(opt.rtex_instance, "api/v2/" + render_result["filename"])) as r:
|
2021-03-22 03:35:45 -04:00
|
|
|
png_buffer = BytesIO(await r.read())
|
|
|
|
|
|
|
|
embed = cmn.embed_factory(ctx)
|
|
|
|
embed.title = "LaTeX Expression"
|
|
|
|
embed.description = "Rendered by [rTeX](https://rtex.probablyaweb.site/)."
|
|
|
|
embed.set_image(url="attachment://tex.png")
|
|
|
|
await ctx.send(file=discord.File(png_buffer, "tex.png"), embed=embed)
|
|
|
|
|
|
|
|
|
|
|
|
def setup(bot: commands.Bot):
|
|
|
|
bot.add_cog(TexCog(bot))
|