Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add header, hyperlink and subtext utilities #6102

Merged
merged 6 commits into from
Sep 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 67 additions & 1 deletion redbot/core/utils/chat_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import math
import textwrap
from io import BytesIO
from typing import Iterator, List, Optional, Sequence, SupportsInt, Union
from typing import Iterator, List, Literal, Optional, Sequence, SupportsInt, Union

import discord
from babel.lists import format_list as babel_list
Expand All @@ -21,11 +21,14 @@
"question",
"bold",
"box",
"header",
"hyperlink",
"inline",
"italics",
"spoiler",
"pagify",
"strikethrough",
"subtext",
"underline",
"quote",
"escape",
Expand All @@ -39,6 +42,69 @@
_ = Translator("UtilsChatFormatting", __file__)


def hyperlink(text: str, url: str) -> str:
"""Create hyperlink markdown with text and a URL.
Parameters
----------
text : str
The text which will contain the link.
url : str
The URL used for the hyperlink.
Returns
-------
str
The new message.
"""
return f"[{text}]({url})"


def header(text: str, size: Literal["small", "medium", "large"]) -> str:
"""Formats a header.
Parameters
----------
text : str
The text for the header.
url : Literal['small', 'medium', 'large']
The size of the header ('small', 'medium' or 'large')
Returns
-------
str
The new message.
"""
if size == "small":
multiplier = 3
elif size == "medium":
multiplier = 2
elif size == "large":
multiplier = 1
else:
raise ValueError(f"Invalid size '{size}'")
return "#" * multiplier + " " + text


def subtext(text: str) -> str:
"""Formats subtext from the given text.
Parameters
----------
text : str
The text to format as subtext.
Returns
-------
str
The new message.
"""
return "-# " + text


def error(text: str) -> str:
"""Get text prefixed with an error emoji.
Expand Down
Loading