feat(seautils): use markdownify to convert rfc html documents to markdown
Some checks failed
Actions / Build Documentation (MkDocs) (push) Successful in 27s
Actions / Lint Code (Ruff & Pylint) (push) Failing after 40s

This commit is contained in:
Seaswimmer 2024-05-28 20:29:39 -04:00
parent a641cae640
commit 28246121a6
Signed by: cswimr
GPG key ID: 5D671B5D03D65A7F
4 changed files with 34 additions and 18 deletions

View file

@ -17,6 +17,7 @@ import yaml
from bs4 import BeautifulSoup
from discord import Color, Embed, app_commands
from discord.utils import CachedSlotProperty, cached_property
from markdownify import MarkdownConverter
from redbot.core import commands
from redbot.core.bot import Red
from redbot.core.dev_commands import cleanup_code
@ -24,6 +25,9 @@ from redbot.core.utils import chat_formatting as cf
from redbot.core.utils.views import SimpleMenu
def md(soup: BeautifulSoup, **options) -> Any | str:
return MarkdownConverter(**options).convert_soup(soup)
class SeaUtils(commands.Cog):
"""A collection of random utilities."""
@ -188,21 +192,6 @@ class SeaUtils(commands.Cog):
except (FileNotFoundError):
await ctx.maybe_send_embed(message=cf.error("Neither `dig` nor `nslookup` are installed on the system. Unable to resolve DNS query."))
async def get_results(self, ctx: commands.Context, soup: BeautifulSoup) -> list:
pre_tags = soup.find_all('pre')
content = []
for pre_tag in pre_tags:
if await ctx.embed_requested():
embed = Embed(
title="RFC Document",
description=pre_tag.text,
color=await ctx.embed_color()
)
content.append(embed)
else:
content.append(pre_tag.text)
return content
@commands.command()
async def rfc(self, ctx: commands.Context, number: int) -> None:
"""Retrieve the text of an RFC document."""
@ -212,7 +201,18 @@ class SeaUtils(commands.Cog):
if response.status == 200:
html = await response.text()
soup = BeautifulSoup(html, 'html.parser')
content = await self.get_results(ctx, soup)
pre_tags = soup.find_all('pre')
content = []
for pre_tag in pre_tags:
if await ctx.embed_requested():
embed = Embed(
title="RFC Document",
description=md(pre_tag),
color=await ctx.embed_color()
)
content.append(embed)
else:
content.append(md(pre_tag))
await SimpleMenu(pages=content, disable_after_timeout=True, timeout=300).start(ctx)
else:
await ctx.maybe_send_embed(content=cf.error(f"An error occurred while fetching RFC {number}. Status code: {response.status}."))