feat(seautils): use markdownify to convert rfc html documents to markdown
This commit is contained in:
parent
a641cae640
commit
28246121a6
4 changed files with 34 additions and 18 deletions
|
@ -9,5 +9,5 @@
|
|||
"disabled": false,
|
||||
"min_bot_version": "3.5.0",
|
||||
"min_python_version": [3, 8, 0],
|
||||
"requirements": ["beautifulsoup4"]
|
||||
"requirements": ["beautifulsoup4", "markdownify"]
|
||||
}
|
||||
|
|
|
@ -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}."))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue